/** * 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())); } }