public function testToArray() { $token = new AccessToken('test_value', ['expires_in' => 1234, 'refresh_token' => 'test_refresh_token', 'scope' => 'test_scope', 'foo' => 'test_foo', 'bar' => 'test_bar']); $this->assertEquals('test_scope', $token->getScope()); $this->assertEquals(['foo' => 'test_foo', 'bar' => 'test_bar'], $token->getExtra()); $tokenArray = $token->toArray(); $this->assertEquals('test_value', $tokenArray['tokenValue']); $this->assertEquals('test_refresh_token', $tokenArray['refreshToken']); $this->assertEquals('test_scope', $tokenArray['scope']); $this->assertEquals(['foo' => 'test_foo', 'bar' => 'test_bar'], $tokenArray['extra']); }
/** * @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()); } }