Exemplo n.º 1
0
 public function getGitHubAuthorize(HttpFoundation\Request $request)
 {
     $code = $request->get('code');
     $state = $request->get('state');
     if (is_null($code) || is_null($state)) {
         return new HttpFoundation\Response('Invalid GitHub Request Params', 400);
     }
     $user = $this->github->getUserFromOAuth($code, $state);
     //Check to see if we already have this user. If so then set their
     //ID so we update the user instead of creating a new one.
     $dbUser = $this->userRepo->getAll(['githubId' => $user->githubId], 1);
     if (!empty($dbUser)) {
         $user->id = $dbUser[0]->id;
     }
     if ($this->userRepo->save($user)) {
         //Add an access token to the user for this one time so that
         //they have something to use to contact our service again.
         $token = $this->tokenGenerator->createAccessToken('codemana', $user->id, 'user', true);
         return new HttpFoundation\JsonResponse(['user' => $user, 'token' => $token]);
         //TODO: The user no longer comes with any repositories. The front end is expected to fetch those separately.
     }
     return new HttpFoundation\Response('Failed Login', 500);
 }
Exemplo n.º 2
0
 /**
  * Validates a request and takes a scope value that could result
  * in a user id being put into the request if it's valid. The
  * passThrough flag will allow the request to continue when it
  * would otherwise fail with a 401 response.
  *
  * @param HttpFoundation\Request $request
  * @param string $scope
  * @param bool $passThrough
  * @return null|HttpFoundation\Response
  */
 public function validateRequest(HttpFoundation\Request $request, $scope, $passThrough = false)
 {
     $this->log->addDebug(print_r($request, true), ['namespace' => 'Alerts\\Controllers\\OAuth2', 'method' => 'validateRequest', 'type' => 'request', 'scope' => $scope]);
     $bridgeRequest = HttpFoundationBridge\Request::createFromRequest($request);
     if ($this->server->verifyResourceRequest($bridgeRequest, null, $scope)) {
         //Put the user into the request if we're validating at the user scope
         if ($scope === 'user') {
             $token = $this->server->getAccessTokenData($bridgeRequest);
             $request->request->set('user', $this->usersRepo->getById($token['user_id']));
         } else {
             //Set the user to null which should make any
             //searches relying on this being valid to fail.
             $request->request->set('user', null);
         }
         return null;
         //If the request shouldn't hard fail. This should only have a few specific use cases.
     } elseif ($passThrough) {
         $this->log->addInfo('OAuth Pass Through', ['namespace' => 'Alerts\\Controllers\\OAuth2', 'method' => 'validateRequest', 'type' => 'request', 'scope' => $scope, 'passThrough' => true]);
         return null;
     }
     $this->log->addInfo('Failed to validate request', ['namespace' => 'Alerts\\Controllers\\OAuth2', 'method' => 'validateRequest', 'scope' => $scope]);
     return new HttpFoundation\Response('Not Authorized', 401);
 }