/** * @param AccessTokenInterface $token * @param array $options */ public function __construct(AccessTokenInterface $token, array $options = []) { $options = array_merge($options, ['emitter' => EventsManager::getEmitter()]); parent::__construct($options); if ($token instanceof OAuth2AccessTokenInterface) { $this->getEmitter()->on('before', function (BeforeEvent $event) use($token) { /** @var \Eva\EvaOAuth\OAuth2\Token\AccessToken $token */ $event->getRequest()->setHeader('Authorization', $token->getTokenType() . ' ' . $token->getTokenValue()); }); } else { $signatureMethod = isset($options['signature_method']) ? $options['signature_method'] : SignatureInterface::METHOD_HMAC_SHA1; $signatureClasses = [SignatureInterface::METHOD_PLAINTEXT => 'Eva\\EvaOAuth\\OAuth1\\Signature\\PlainText', SignatureInterface::METHOD_HMAC_SHA1 => 'Eva\\EvaOAuth\\OAuth1\\Signature\\Hmac', SignatureInterface::METHOD_RSA_SHA1 => 'Eva\\EvaOAuth\\OAuth1\\Signature\\Rsa']; if (false === isset($signatureClasses[$signatureMethod])) { throw new InvalidArgumentException(sprintf('Signature method %s not able to process', $signatureMethod)); } $signatureClass = $signatureClasses[$signatureMethod]; $this->getEmitter()->on('before', function (BeforeEvent $event) use($token, $signatureClass) { /** @var Request $request */ $request = $event->getRequest(); /** @var \Eva\EvaOAuth\OAuth1\Token\AccessToken $token */ $httpMethod = strtoupper($request->getMethod()); $url = Url::fromString($request->getUrl()); $parameters = ['oauth_consumer_key' => $token->getConsumerKey(), 'oauth_signature_method' => SignatureInterface::METHOD_HMAC_SHA1, 'oauth_timestamp' => (string) time(), 'oauth_nonce' => strtolower(Text::generateRandomString(32)), 'oauth_token' => $token->getTokenValue(), 'oauth_version' => '1.0']; $signature = (string) new $signatureClass($token->getConsumerSecret(), Text::buildBaseString($httpMethod, $url, $parameters), $token->getTokenSecret()); $parameters['oauth_signature'] = $signature; $event->getRequest()->setHeader('Authorization', Text::buildHeaderString($parameters)); }); } }
/** * @param AuthorizationServerInterface $authServer * @return string */ public function getAuthorizeUrl(AuthorizationServerInterface $authServer) { $options = $this->options; $authorizeQuery = ['response_type' => 'code', 'client_id' => $options['client_id'], 'redirect_uri' => $options['redirect_uri'], 'state' => Text::generateRandomString()]; if ($options['scope']) { $authorizeQuery['scope'] = $options['scope']; } return $authServer->getAuthorizeUrl() . '?' . http_build_query($authorizeQuery); }
public function testBaseString() { $this->assertEquals('1Gv6XVo5dKoJ5IyyZxusyQDxk1U=', (string) new Hmac(Text::buildBaseString('post', 'https://api.twitter.com/oauth/request_token', ['oauth_consumer_key' => 'X6vZ7YDHiod0hUyTQj0Gw', 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_timestamp' => '1428979350', 'oauth_nonce' => 'ddb73c89364451560652f53bcd8f14f7', 'oauth_version' => '1.0']), '8Ap6YGs9BchvEFAOn6iw43jsjMKE48y3SDfacPyFTuI')); }
/** * @param ServiceProviderInterface $serviceProvider * @param array $urlQuery * @param RequestToken $requestToken * @return AccessToken */ public function getAccessToken(ServiceProviderInterface $serviceProvider, array $urlQuery = [], RequestToken $requestToken = null) { $urlQuery = $urlQuery ?: $_GET; $tokenValue = empty($urlQuery['oauth_token']) ? '' : $urlQuery['oauth_token']; $tokenVerify = empty($urlQuery['oauth_verifier']) ? '' : $urlQuery['oauth_verifier']; if (!$tokenValue || !$tokenVerify) { throw new InvalidArgumentException(sprintf('No oauth_token or oauth_verifier input')); } /** @var RequestToken $requestToken */ $requestToken = $requestToken ?: $this->getStorage()->fetch(md5($tokenValue)); if (!$requestToken) { throw new InvalidArgumentException(sprintf('No request token found in storage')); } if ($tokenValue != $requestToken->getTokenValue()) { throw new VerifyException(sprintf('Request token not match')); } $options = $this->options; $httpMethod = $serviceProvider->getAccessTokenMethod(); $url = $serviceProvider->getAccessTokenUrl(); $parameters = ['oauth_consumer_key' => $options['consumer_key'], 'oauth_signature_method' => $this->signatureMethod, 'oauth_timestamp' => (string) time(), 'oauth_nonce' => Text::generateRandomString(32), 'oauth_token' => $tokenValue, 'oauth_version' => '1.0', 'oauth_verifier' => $tokenVerify, 'oauth_callback' => $options['callback']]; $baseString = Text::buildBaseString($httpMethod, $url, $parameters); $signatureClass = $this->getSignatureClass(); $signature = (string) new $signatureClass($baseString, $options['consumer_secret'], $requestToken->getTokenSecret()); $parameters['oauth_signature'] = $signature; $httpClient = self::getHttpClient(); $httpClientOptions = ['headers' => ['X-EvaOAuth-Debug-BaseString' => $baseString, 'Authorization' => Text::buildHeaderString($parameters)], 'body' => ['oauth_verifier' => $tokenVerify]]; $request = $httpClient->createRequest($httpMethod, $url, $httpClientOptions); try { $this->getEmitter()->emit('beforeGetAccessToken', new BeforeGetAccessToken($request, $serviceProvider, $this)); /** @var Response $response */ $response = $httpClient->send($request); return AccessToken::factory($response, $serviceProvider, $options); } catch (RequestException $e) { throw new \Eva\EvaOAuth\Exception\RequestException('Get access token failed', $e->getRequest(), $e->getResponse()); } }
public function testBaseString() { $this->assertEquals('POST&http%3A%2F%2Ffoo&callback%3Dhttp%253A%252F%252Fbar', Text::buildBaseString('post', 'http://foo', ['callback' => 'http://bar'])); $this->assertEquals('POST&url&foo%3Dbar', Text::buildBaseString('post', 'url', ['foo' => 'bar'])); $this->assertEquals('POST&https%3A%2F%2Fapi.twitter.com%2Foauth%2Frequest_token&oauth_consumer_key%3DX6vZ7YDHiod0hUyTQj0Gw%26oauth_nonce%3Dddb73c89364451560652f53bcd8f14f7%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1428979350%26oauth_version%3D1.0', Text::buildBaseString('post', 'https://api.twitter.com/oauth/request_token', ['oauth_consumer_key' => 'X6vZ7YDHiod0hUyTQj0Gw', 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_timestamp' => '1428979350', 'oauth_nonce' => 'ddb73c89364451560652f53bcd8f14f7', 'oauth_version' => '1.0'])); }