public function createAction() { $applicationConfig = $this->getServiceLocator()->get('config'); $config = $applicationConfig['zf-oauth2-doctrine']['default']; $console = $this->getServiceLocator()->get('console'); $objectManager = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default'); // Make sure that we are running in a console and the user has not tricked our // application into running this action from a public web server. $request = $this->getRequest(); if (!$request instanceof ConsoleRequest) { throw new RuntimeException('You can only use this action from a console.'); } $client = $objectManager->getRepository($config['mapping']['Client']['entity'])->find($this->getRequest()->getParam('id')); if (!$client) { $console->write("Client not found", Color::RED); return; } // Get the subject $subject = Prompt\Line::prompt("The subject, usually a user_id. Not required: ", true, 255); // Get public key path $publicKeyPath = ''; while (!file_exists($publicKeyPath)) { $publicKeyPath = Prompt\Line::prompt("Public key path: ", false, 255); } $publicKey = file_get_contents($publicKeyPath); $jwt = new Entity\Jwt(); $jwt->setClient($client); $jwt->setSubject($subject); $jwt->setPublicKey($publicKey); $objectManager->persist($jwt); $objectManager->flush(); $console->write("JWT created\n", Color::GREEN); }
/** * Edit a Jwt key * @return array|Response|ViewModel */ public function manageKeyAction() { $jwt_id = (int) $this->params()->fromRoute('jwt_id', false); $client_id = (int) $this->params()->fromRoute('client_id', 0); $jwt = $this->jwtService->findByClientId($client_id); if ($jwt instanceof Jwt and $jwt_id == 0) { $this->flashMessenger()->addErrorMessage('You have a pre-existing public key for this client, either delete the key then add a new one or edit the current key.'); return $this->redirect()->toRoute('zf-oauth-doctrine-gui/clients'); } $prg = $this->prg(); if ($prg instanceof Response) { return $prg; } elseif ($prg === false) { if ($jwt_id != 0) { $jwtObject = $this->jwtService->find($jwt_id); $this->jwtForm->bind($jwtObject); } else { $jwtObject = new Jwt(); $clientObject = $this->clientService->find($client_id); $jwtObject->setClient($clientObject); $this->jwtForm->bind($jwtObject); } return new ViewModel(array('form' => $this->jwtForm, 'jwt_id' => $jwt_id)); } $this->jwtForm->setData($prg); if (!$this->jwtForm->isValid()) { return new ViewModel(array('form' => $this->jwtForm, 'jwt_id' => $jwt_id)); } $jwtObject = $this->jwtForm->getData(); $jwtObject = $this->jwtService->update($jwtObject); if (!$jwtObject instanceof Jwt) { $this->flashMessenger()->addErrorMessage('Unable to save the jwt object'); return $this->redirect()->toRoute('zf-oauth-doctrine-gui/clients'); } $this->flashMessenger()->addSuccessMessage('Client updated'); return $this->redirect()->toRoute('zf-oauth-doctrine-gui/clients'); }
protected function setUp() { $this->setApplicationConfig(include __DIR__ . '/../asset/orm.config.php'); parent::setUp(); $serviceManager = $this->getApplication()->getServiceManager(); $objectManager = $serviceManager->get('doctrine.entitymanager.orm_default'); try { $objectManager->getRepository('ZF\\OAuth2\\Doctrine\\Entity\\Scope')->findAll(); } catch (Exception $e) { $bcrypt = new Bcrypt(); $bcrypt->setCost(14); // Create database $tool = new SchemaTool($objectManager); $res = $tool->createSchema($objectManager->getMetadataFactory()->getAllMetadata()); // Fixtures $scope = new Entity\Scope(); $scope->setScope('clientscope1'); $scope2 = new Entity\Scope(); $scope2->setScope('supportedscope1'); $scope3 = new Entity\Scope(); $scope3->setScope('supportedscope2'); $scope4 = new Entity\Scope(); $scope4->setScope('supportedscope3'); $scope5 = new Entity\Scope(); $scope5->setScope('defaultscope1'); $scope5->setIsDefault(true); $scope6 = new Entity\Scope(); $scope6->setScope('defaultscope2'); $scope6->setIsDefault(true); $objectManager->persist($scope); $objectManager->persist($scope2); $objectManager->persist($scope3); $objectManager->persist($scope4); $objectManager->persist($scope5); $objectManager->persist($scope6); $user = new User(); $user->setUsername('oauth_test_user'); $user->setPassword($bcrypt->create('testpass')); $user->setProfile('profile'); $user->setCountry('US'); $user->setPhoneNumber('phone'); $user->setEmail('doctrine@zfcampus'); $user2 = new User(); $objectManager->persist($user); $objectManager->persist($user2); $client = new Entity\Client(); $client->setClientId('oauth_test_client'); $client->setSecret($bcrypt->create('testpass')); $client->setGrantType(array('implicit')); $client->setUser($user); $client->addScope($scope); $scope->addClient($client); $client2 = new Entity\Client(); $client2->setClientId('oauth_test_client2'); $client2->setSecret($bcrypt->create('testpass')); $client2->setGrantType(array('implicit')); $client2->setUser($user2); $client3 = new Entity\Client(); $client3->setClientId('oauth_test_client3'); $client3->setUser($user2); $objectManager->persist($client); $objectManager->persist($client2); $objectManager->persist($client3); $accessToken = new Entity\AccessToken(); $accessToken->setClient($client); $accessToken->setExpires(DateTime::createFromFormat('Y-m-d', '2020-01-01')); $accessToken->setAccessToken('testtoken'); $accessToken->setUser($user); $objectManager->persist($accessToken); $authorizationCode = new Entity\AuthorizationCode(); $authorizationCode->setAuthorizationCode('testtoken'); $authorizationCode->setClient($client); $authorizationCode->setRedirectUri('http://redirect'); $authorizationCode->setExpires(DateTime::createFromFormat('Y-m-d', '2020-01-01')); $authorizationCode->setUser($user); $objectManager->persist($authorizationCode); $refreshToken = new Entity\RefreshToken(); $refreshToken->setClient($client); $refreshToken->setExpires(DateTime::createFromFormat('Y-m-d', '2020-01-01')); $refreshToken->setRefreshToken('testtoken'); $refreshToken->setUser($user); $objectManager->persist($refreshToken); $jwt = new Entity\Jwt(); $jwt->setClient($client); $jwt->setSubject('test_subject'); $jwt->setPublicKey("-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCvfF+Cw8nzsc9Twam37SYpAW3+\nlRGUle/hYnd9obfBvDHKBvgb1WfGCblwjwImGL9u0rEIW2sspkwBEsGGFFBmSaqq\nfvEER7Yr++VIidOUHkas3cHO1TVoERO3s0THOobw0OzghPnMJL6ayelYOESwfnqR\nWfuEMSaWaW0G38QPzwIDAQAB\n-----END PUBLIC KEY-----\n"); $objectManager->persist($jwt); $publicKey = new Entity\PublicKey(); $publicKey->setPublicKey("-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCvfF+Cw8nzsc9Twam37SYpAW3+\nlRGUle/hYnd9obfBvDHKBvgb1WfGCblwjwImGL9u0rEIW2sspkwBEsGGFFBmSaqq\nfvEER7Yr++VIidOUHkas3cHO1TVoERO3s0THOobw0OzghPnMJL6ayelYOESwfnqR\nWfuEMSaWaW0G38QPzwIDAQAB\n-----END PUBLIC KEY-----\n"); $publicKey->setPrivateKey("-----BEGIN RSA PRIVATE KEY-----\nMIICXAIBAAKBgQCvfF+Cw8nzsc9Twam37SYpAW3+lRGUle/hYnd9obfBvDHKBvgb\n1WfGCblwjwImGL9u0rEIW2sspkwBEsGGFFBmSaqqfvEER7Yr++VIidOUHkas3cHO\n1TVoERO3s0THOobw0OzghPnMJL6ayelYOESwfnqRWfuEMSaWaW0G38QPzwIDAQAB\nAoGAYHtBB+QdZJ6eHq6bYURBdsoSb6YFxGurN3+rsqb3IM0XkrvCLYtnQrqV+gym\nYcu5dHTiYHXitum3X9+wBseka692RYcYuQbBIeT64H91kiFKLBy1vy/g8cmUyI0X\nTmabVBnFgS6JGL26C3zC71k3xmd0OQAEpAKg/vYaz2gTwAECQQDYiaEcS29aFsxm\nvT3/IvNV17nGvH5sJAuOkKzf6P6TyE2NmAqSjqngm0wSwRdlARcWM+v6H2R/0qdF\n6azDItuBAkEAz3eCWygU7pLOtw4VfrX1ppWBIw6qLNF2lKdKPnFqFk5c3GK9ek2G\ntTn6NI3LT5NnKu2/YFTR4tr4hgBbdJfTTwJAWWQfxZ2Cn49P3I39PQmBqQuAnwGL\nszsCJl2lcF4wUnPbSDvfCXepu5aAxjE+Zi0YCctvfHdfNsGQ2nTIJFqMgQJBAL5L\nD/YsvYZWgeTFtlGS9M7nMpvFR7H0LqALEb5UqMns9p/usX0MvxJbK3Qo2uMSgP6P\nM4pYQmuiDXJbwYcf+2ECQCB3s5z9niG6oxVicCfK/l6VJNPifhtr8N48jO0ejWeB\n1OYsqgH36dp0vjhmtUZip0ikLOxdOueHeOZEjwlt2l8=\n-----END RSA PRIVATE KEY-----\n"); $publicKey->setEncryptionAlgorithm('rsa'); $publicKey->setClient($client); $objectManager->persist($publicKey); $objectManager->flush(); } }