Example #1
0
 /**
  * @param ResourceServerInterface $resourceServer
  * @param array $urlQuery
  * @return \Eva\EvaOAuth\OAuth2\Token\AccessTokenInterface
  */
 public function getAccessToken(ResourceServerInterface $resourceServer, array $urlQuery = [])
 {
     $urlQuery = $urlQuery ?: $_GET;
     $code = empty($urlQuery['code']) ? '' : $urlQuery['code'];
     $state = empty($urlQuery['state']) ? '' : $urlQuery['state'];
     $options = $this->options;
     if (!$code) {
         throw new InvalidArgumentException("No authorization code found");
     }
     //TODO: Valid state to void attach
     $parameters = ['grant_type' => 'authorization_code', 'code' => $code, 'client_id' => $options['client_id'], 'client_secret' => $options['client_secret'], 'redirect_uri' => $options['redirect_uri']];
     if ($state) {
         $parameters['state'] = $state;
     }
     $httpClient = $this->httpClient;
     $method = $resourceServer->getAccessTokenMethod();
     $httpClientOptions = $method == ResourceServerInterface::METHOD_GET ? ['query' => $parameters] : ['body' => $parameters];
     $request = $httpClient->createRequest($method, $resourceServer->getAccessTokenUrl(), $httpClientOptions);
     try {
         $this->getEmitter()->emit('beforeGetAccessToken', new BeforeGetAccessToken($request, $resourceServer));
         /** @var Response $response */
         $response = $httpClient->send($request);
         return AccessToken::factory($response, $resourceServer);
     } catch (RequestException $e) {
         throw new \Eva\EvaOAuth\Exception\RequestException('Get access token failed', $e->getRequest(), $e->getResponse());
     }
 }
Example #2
0
 /**
  * @param Response $response
  * @param ResourceServerInterface $resourceServer
  * @return AccessTokenInterface
  */
 public static function factory(Response $response, ResourceServerInterface $resourceServer)
 {
     $rawToken = ResponseParser::parse($response, $resourceServer->getAccessTokenFormat());
     $tokenValue = empty($rawToken['access_token']) ? '' : $rawToken['access_token'];
     $token = new static($tokenValue);
     $token->setResponse($response);
     foreach ($rawToken as $key => $value) {
         $token->{$key} = $value;
     }
     return $token;
 }
Example #3
0
 /**
  * @param AccessToken $token
  * @param ResourceServerInterface $resourceServer
  * @return \Eva\EvaOAuth\User\UserInterface
  */
 public static function getUser(AccessToken $token, ResourceServerInterface $resourceServer)
 {
     return $resourceServer->getUser($token);
 }