Skip to content

Commit c3b5e51

Browse files
authored
Merge pull request #549 from tezrik/master
Add provider Yandex
2 parents a60c57d + 602f1a1 commit c3b5e51

3 files changed

Lines changed: 93 additions & 2 deletions

File tree

src/OAuth/OAuth2/Service/AbstractService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ public function request($path, $method = 'GET', $body = null, array $extraHeader
152152
)
153153
);
154154
}
155-
156155
// add the token where it may be needed
157156
if (static::AUTHORIZATION_METHOD_HEADER_OAUTH === $this->getAuthorizationMethod()) {
158157
$extraHeaders = array_merge(['Authorization' => 'OAuth ' . $token->getAccessToken()], $extraHeaders);
@@ -164,14 +163,15 @@ public function request($path, $method = 'GET', $body = null, array $extraHeader
164163
$uri->addToQuery('apikey', $token->getAccessToken());
165164
} elseif (static::AUTHORIZATION_METHOD_QUERY_STRING_V4 === $this->getAuthorizationMethod()) {
166165
$uri->addToQuery('auth', $token->getAccessToken());
166+
} elseif (static::AUTHORIZATION_METHOD_QUERY_STRING_V5 === $this->getAuthorizationMethod()) {
167+
$uri->addToQuery('oauth_token', $token->getAccessToken());
167168
} elseif (static::AUTHORIZATION_METHOD_HEADER_BEARER === $this->getAuthorizationMethod()) {
168169
$extraHeaders = array_merge(['Authorization' => 'Bearer ' . $token->getAccessToken()], $extraHeaders);
169170
} elseif (static::AUTHORIZATION_METHOD_HEADER_TOKEN === $this->getAuthorizationMethod()) {
170171
$extraHeaders = array_merge(['Authorization' => 'token ' . $token->getAccessToken()], $extraHeaders);
171172
}
172173

173174
$extraHeaders = array_merge($this->getExtraApiHeaders(), $extraHeaders);
174-
175175
return $this->httpClient->retrieveResponse($uri, $body, $extraHeaders, $method);
176176
}
177177

src/OAuth/OAuth2/Service/ServiceInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ interface ServiceInterface extends BaseServiceInterface
2020
const AUTHORIZATION_METHOD_QUERY_STRING_V3 = 4;
2121
const AUTHORIZATION_METHOD_QUERY_STRING_V4 = 5;
2222
const AUTHORIZATION_METHOD_HEADER_TOKEN = 6;
23+
const AUTHORIZATION_METHOD_QUERY_STRING_V5 = 7;
24+
2325

2426
/**
2527
* Retrieves and stores/returns the OAuth2 access token after a successful authorization.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace OAuth\OAuth2\Service;
4+
5+
use OAuth\OAuth2\Token\StdOAuth2Token;
6+
use OAuth\Common\Http\Exception\TokenResponseException;
7+
use OAuth\Common\Http\Uri\Uri;
8+
use OAuth\Common\Consumer\CredentialsInterface;
9+
use OAuth\Common\Http\Client\ClientInterface;
10+
use OAuth\Common\Storage\TokenStorageInterface;
11+
use OAuth\Common\Http\Uri\UriInterface;
12+
13+
class Yandex extends AbstractService
14+
{
15+
/**
16+
* Defined scopes
17+
*
18+
* @link https://tech.yandex.ru/money/doc/dg/concepts/protocol-rights-docpage/
19+
*/
20+
const SCOPE_ACCOUNT_INFO = 'account-info';
21+
22+
public function __construct(
23+
CredentialsInterface $credentials,
24+
ClientInterface $httpClient,
25+
TokenStorageInterface $storage,
26+
$scopes = array(),
27+
UriInterface $baseApiUri = null
28+
) {
29+
parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
30+
31+
if (null === $baseApiUri) {
32+
$this->baseApiUri = new Uri('https://login.yandex.ru/');
33+
}
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function getAuthorizationEndpoint()
40+
{
41+
return new Uri('https://oauth.yandex.ru/authorize');
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function getAccessTokenEndpoint()
48+
{
49+
return new Uri('https://oauth.yandex.ru/token');
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
protected function parseAccessTokenResponse($responseBody)
56+
{
57+
$data = json_decode($responseBody, true);
58+
if (null === $data || !is_array($data)) {
59+
throw new TokenResponseException('Unable to parse response.');
60+
} elseif (isset($data['error'])) {
61+
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
62+
}
63+
64+
$token = new StdOAuth2Token();
65+
$token->setAccessToken($data['access_token']);
66+
$token->setLifeTime($data['expires_in']);
67+
68+
if (isset($data['refresh_token'])) {
69+
$token->setRefreshToken($data['refresh_token']);
70+
unset($data['refresh_token']);
71+
}
72+
73+
unset($data['access_token']);
74+
unset($data['expires_in']);
75+
$token->setExtraParams($data);
76+
77+
return $token;
78+
}
79+
80+
/**
81+
* {@inheritdoc}
82+
*/
83+
protected function getAuthorizationMethod()
84+
{
85+
return static::AUTHORIZATION_METHOD_QUERY_STRING_V5;
86+
}
87+
88+
89+
}

0 commit comments

Comments
 (0)