-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractProvider.php
181 lines (154 loc) · 4.92 KB
/
AbstractProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/**
* SocialConnect project
* @author: Patsura Dmitry https://github.com/ovr <[email protected]>
*/
declare(strict_types=1);
namespace SocialConnect\OAuth2;
use Psr\Http\Message\RequestInterface;
use SocialConnect\OAuth2\Exception\InvalidState;
use SocialConnect\OAuth2\Exception\Unauthorized;
use SocialConnect\OAuth2\Exception\UnknownAuthorization;
use SocialConnect\OAuth2\Exception\UnknownState;
use SocialConnect\Provider\AbstractBaseProvider;
use SocialConnect\Provider\Exception\InvalidAccessToken;
use SocialConnect\Provider\Exception\InvalidResponse;
abstract class AbstractProvider extends AbstractBaseProvider
{
/**
* HTTP method for access token request
*
* @var string
*/
protected $requestHttpMethod = 'POST';
/**
* @return string
*/
abstract public function getAuthorizeUri();
/**
* @return string
*/
abstract public function getRequestTokenUri();
/**
* {@inheritdoc}
*/
public function getAuthUrlParameters(): array
{
$parameters = parent::getAuthUrlParameters();
// special parameters only required for OAuth2
$parameters['client_id'] = $this->consumer->getKey();
$parameters['redirect_uri'] = $this->getRedirectUrl();
$parameters['response_type'] = 'code';
return $parameters;
}
/**
* {@inheritdoc}
*/
public function makeAuthUrl(): string
{
$urlParameters = $this->getAuthUrlParameters();
if (!$this->getBoolOption('stateless', false)) {
$this->session->set(
'oauth2_state',
$urlParameters['state'] = $this->generateState()
);
}
if (count($this->scope) > 0) {
$urlParameters['scope'] = $this->getScopeInline();
}
return $this->getAuthorizeUri() . '?' . http_build_query($urlParameters);
}
/**
* Parse access token from response's $body
*
* @param string $body
* @return AccessToken
* @throws InvalidAccessToken
*/
public function parseToken(string $body)
{
if (empty($body)) {
throw new InvalidAccessToken('Provider response with empty body');
}
$token = json_decode($body, true);
if ($token) {
if (!is_array($token)) {
throw new InvalidAccessToken('Response must be array');
}
return new AccessToken($token);
}
throw new InvalidAccessToken('Server response with not valid/empty JSON');
}
/**
* @param string $code
* @return RequestInterface
*/
protected function makeAccessTokenRequest(string $code): RequestInterface
{
$parameters = [
'client_id' => $this->consumer->getKey(),
'client_secret' => $this->consumer->getSecret(),
'code' => $code,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->getRedirectUrl()
];
return $this->httpStack->createRequest($this->requestHttpMethod, $this->getRequestTokenUri())
->withHeader('Content-Type', 'application/x-www-form-urlencoded')
->withBody($this->httpStack->createStream(http_build_query($parameters, '', '&')))
;
}
/**
* @param string $code
* @return AccessToken
* @throws InvalidAccessToken
* @throws InvalidResponse
* @throws \Psr\Http\Client\ClientExceptionInterface
*/
public function getAccessToken(string $code): AccessToken
{
$response = $this->executeRequest(
$this->makeAccessTokenRequest($code)
);
return $this->parseToken($response->getBody()->getContents());
}
/**
* @param array $parameters
* @return AccessToken
* @throws InvalidAccessToken
* @throws InvalidResponse
* @throws InvalidState
* @throws Unauthorized
* @throws UnknownAuthorization
* @throws UnknownState
* @throws \Psr\Http\Client\ClientExceptionInterface
*/
public function getAccessTokenByRequestParameters(array $parameters)
{
if (isset($parameters['error']) && $parameters['error'] === 'access_denied') {
throw new Unauthorized();
}
if (!isset($parameters['code'])) {
throw new Unauthorized('Unknown code');
}
if (!$this->getBoolOption('stateless', false)) {
$state = $this->session->get('oauth2_state');
if (!$state) {
throw new UnknownAuthorization();
}
if (!isset($parameters['state'])) {
throw new UnknownState();
}
if ($state !== $parameters['state']) {
throw new InvalidState();
}
}
return $this->getAccessToken($parameters['code']);
}
/**
* {@inheritDoc}
*/
public function createAccessToken(array $information)
{
return new AccessToken($information);
}
}