public function post() { $request = $this->getSlim()->request(); // Do the validation - TODO!!! //$this->statementValidator->validateRequest($request); //$this->statementValidator->validatePutRequest($request); if ($this->userService->loggedIn()) { // Authorization is always requested $this->oAuthService->authorizePost($request); $redirectUri = $this->oAuthService->getRedirectUri(); $this->getSlim()->response->headers->set('Location', $redirectUri); Resource::response(Resource::STATUS_FOUND); } else { // Unauthorized Resource::response(Resource::STATUS_UNAUTHORIZED); } }
public function post() { $request = $this->getSlim()->request(); // Do the validation - TODO!!! //$this->statementValidator->validateRequest($request); //$this->statementValidator->validatePutRequest($request); // Authorization is always requested try { $this->userService->loginPost($request); $redirectUrl = $this->getSlim()->url; $redirectUrl->getPath()->remove('login'); $redirectUrl->getPath()->append('authorize'); $this->getSlim()->response->headers->set('Location', $redirectUrl); Resource::response(Resource::STATUS_FOUND); } catch (\Exception $e) { $view = new LoginView(['service' => $this->userService]); $view = $view->renderGet(); Resource::response(Resource::STATUS_UNAUTHORIZED, $view); } }
protected function execute(InputInterface $input, OutputInterface $output) { $userService = new UserService($this->getSlim()); $helper = $this->getHelper('question'); if (null === $input->getOption('email')) { $question = new Question('Please enter an e-mail: ', 'untitled'); $email = $helper->ask($input, $output, $question); } else { $email = $input->getOption('email'); } if (null === $input->getOption('password')) { $question = new Question('Please enter a password: '******''); $password = $helper->ask($input, $output, $question); } else { $password = $input->getOption('password'); } $userService->fetchAvailablePermissions(); $permissionsDictionary = []; foreach ($userService->getCursor() as $permission) { $permissionsDictionary[$permission->getName()] = $permission; } if (null === $input->getOption('permissions')) { $question = new ChoiceQuestion('Please select which permissions you would like to enable (defaults to super). Separate multiple values with commas (without spaces). If you select super, all other permissions are also inherited: ', array_keys($permissionsDictionary), '0'); $question->setMultiselect(true); $selectedPermissionNames = $helper->ask($input, $output, $question); } else { $selectedPermissionNames = explode(',', $input->getOption('permissions')); } $selectedPermissions = []; foreach ($selectedPermissionNames as $selectedPermissionName) { $selectedPermissions[] = $permissionsDictionary[$selectedPermissionName]; } $user = $userService->addUser($email, $password, $selectedPermissions); $text = json_encode($user, JSON_PRETTY_PRINT); $output->writeln('<info>User successfully created!</info>'); $output->writeln('<info>Info:</info>'); $output->writeln($text); }
/** * Tries to create a new access token. */ public function accessTokenPost($request) { $body = $request->getBody(); $body = json_decode($body, true); // Some clients escape the JSON - handle them if (is_string($body)) { $body = json_decode($body, true); } if (json_last_error() !== JSON_ERROR_NONE) { throw new \Exception('Invalid JSON posted. Cannot continue!', Resource::STATUS_BAD_REQUEST); } $requestParams = new Set($body); if ($requestParams->get('user')['email'] === null) { throw new \Exception('Invalid request, user.email property not present!', Resource::STATUS_BAD_REQUEST); } $currentDate = new \DateTime(); $defaultParams = new Set(['user' => ['password' => 'password', 'permissions' => ['all']], 'scopes' => ['all'], 'name' => 'Token for ' . $requestParams->get('user')['email'], 'description' => 'Token generated at ' . Util\Date::dateTimeToISO8601($currentDate), 'expiresAt' => null]); $params = new Set(array_replace_recursive($defaultParams->all(), $requestParams->all())); $scopeDocuments = []; $scopes = $params->get('scopes'); foreach ($scopes as $scope) { $scopeDocument = $this->getScopeByName($scope); $scopeDocuments[] = $scopeDocument; } $permissionDocuments = []; $permissions = $params->get('user')['permissions']; foreach ($permissions as $permission) { $permissionDocument = $this->getScopeByName($permission); $permissionDocuments[] = $permissionDocument; } if (is_numeric($params->get('expiresAt'))) { $expiresAt = $params->get('expiresAt'); } else { if (null === $params->get('expiresAt')) { $expiresAt = null; } else { $expiresAt = new \DateTime($params->get('expiresAt')); $expiresAt = $expiresAt->getTimestamp(); } } $userService = new UserService($this->getSlim()); $user = $userService->addUser($params->get('user')['email'], $params->get('user')['password'], $permissionDocuments); $user->save(); $this->addToken($params->get('name'), $params->get('description'), $expiresAt, $user, $scopeDocuments); return $this; }
protected function execute(InputInterface $input, OutputInterface $output) { $basicAuthService = new BasicAuthService($this->getSlim()); if (null === $input->getOption('name')) { $helper = $this->getHelper('question'); $question = new Question('Please enter a name: ', 'untitled'); $name = $helper->ask($input, $output, $question); } else { $name = $input->getOption('name'); } if (null === $input->getOption('description')) { $question = new Question('Please enter a description: ', ''); $description = $helper->ask($input, $output, $question); } else { $description = $input->getOption('description'); } if (null === $input->getOption('expiration')) { $question = new Question('Please enter the expiration timestamp for the token (blank == indefinite): '); $expiresAt = $helper->ask($input, $output, $question); } else { $expiresAt = $input->getOption('expiration'); } $userService = new UserService($this->getSlim()); $userService->fetchAll(); $users = []; foreach ($userService->getCursor() as $user) { $users[$user->getEmail()] = $user; } if (null === $input->getOption('email')) { $question = new Question('Please enter enter the e-mail of the associated user: '******''); $question->setAutocompleterValues(array_keys($users)); $email = $helper->ask($input, $output, $question); $user = $users[$email]; } else { $email = $input->getOption('email'); if (!isset($users[$email])) { throw new Exception('Invalid e-mail provided! User does not exist!'); } $user = $users[$email]; } $userService->fetchAvailablePermissions(); $scopesDictionary = []; foreach ($userService->getCursor() as $scope) { $scopesDictionary[$scope->getName()] = $scope; } if (null === $input->getOption('scopes')) { $question = new ChoiceQuestion('Please select which scopes you would like to enable (defaults to super). Separate multiple values with commas (without spaces). If you select super, all other permissions are also inherited: ', array_keys($scopesDictionary), '0'); $question->setMultiselect(true); $selectedScopeNames = $helper->ask($input, $output, $question); $selectedScopes = []; foreach ($selectedScopeNames as $selectedScopeName) { $selectedScopes[] = $scopesDictionary[$selectedScopeName]; } } else { $selectedScopeNames = explode(',', $input->getOption('scopes')); } $selectedScopes = []; foreach ($selectedScopeNames as $selectedScopeName) { $selectedScopes[] = $scopesDictionary[$selectedScopeName]; } $token = $basicAuthService->addToken($name, $description, $expiresAt, $user, $selectedScopes); if (null !== $input->getOption('key')) { $token->setKey($input->getOption('key')); $token->save(); } if (null !== $input->getOption('secret')) { $token->setSecret($input->getOption('secret')); $token->save(); } $text = json_encode($token, JSON_PRETTY_PRINT); $output->writeln('<info>Basic token successfully created!</info>'); $output->writeln('<info>Info:</info>'); $output->writeln($text); }