/**
  * Performs an authentication attempt
  *
  * @return \Zend\Authentication\Result
  * @throws \Zend\Authentication\Adapter\Exception\ExceptionInterface If authentication cannot be performed
  */
 public function authenticate()
 {
     try {
         $service = $this->service;
         $session = $service->getSession();
         $me = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
         $res = new OAuth2Result($this->authenticationService, Result::SUCCESS, $me->getId());
         $res->setUserProfile($this->createUserProfile($session, $me));
         //TODO
         //$res->setAccessToken($token->accessToken);
         //$res->setRefreshToken($token->refreshToken);
         //$res->setExpiresIn($token->expires);
         return $res;
     } catch (FacebookRequestException $ex) {
         // When Facebook returns an error
         return new Result($this->authenticationService, Result::FAILURE_CREDENTIAL_INVALID, array(), array("Facebook request exception - " . $ex->getErrorType() . " - " . $ex->getMessage()));
     } catch (\Exception $ex) {
         // When validation fails or other local issues
         return new Result($this->authenticationService, Result::FAILURE, array(), array("General exception - " . $ex->getMessage()));
     }
 }
Esempio n. 2
0
 /**
  * Performs an authentication attempt
  *
  * @return \Zend\Authentication\Result
  * @throws \Zend\Authentication\Adapter\Exception\ExceptionInterface If authentication cannot be performed
  */
 public function authenticate()
 {
     $code = $this->getMvcEvent()->getRequest()->getQuery('code');
     if (!$code) {
         return new Result($this->authenticationService, Result::FAILURE_CREDENTIAL_INVALID, array(), array("No 'code' available"));
     }
     try {
         $provider = $this->getProvider();
         $token = $provider->getAccessToken('authorization_code', ['code' => $code]);
         $userProfile = $provider->getUserDetails($token);
         $res = new OAuth2Result($this->authenticationService, Result::SUCCESS, $userProfile->uid);
         $res->setUserProfile($this->createUserProfile($userProfile));
         $res->setAccessToken($token->accessToken);
         $res->setRefreshToken($token->refreshToken);
         $res->setExpiresIn($token->expires);
         return $res;
     } catch (\Exception $e) {
         return new Result($this->authenticationService, Result::FAILURE_CREDENTIAL_INVALID, $code, array($e->getMessage()));
     }
 }