|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is based on the Generic Provider from the league/oauth2-client library |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that is distributed with the league/oauth2-client library. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace JPBernius\OAuth2\Client\Provider; |
|
10
|
|
|
|
|
11
|
|
|
use GuzzleHttp\Client; |
|
12
|
|
|
use League\OAuth2\Client\Grant\AbstractGrant; |
|
13
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
|
14
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
|
15
|
|
|
use League\OAuth2\Client\Provider\GenericResourceOwner; |
|
16
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
|
17
|
|
|
use League\OAuth2\Client\Tool\BearerAuthorizationTrait; |
|
18
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* OAuth 2.0 service provider for interaction with doo.net API using Bearer token authentication. |
|
22
|
|
|
*/ |
|
23
|
|
|
class DooProvider extends AbstractProvider |
|
24
|
|
|
{ |
|
25
|
|
|
use BearerAuthorizationTrait; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $apiUrl = 'https://api.doo.net/v1/'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
private $uriAuthorize = 'oauth'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
private $uriAccessToken = 'oauth'; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
private $uriResourceOwnerDetails = 'organizers/current'; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var array|null |
|
49
|
|
|
*/ |
|
50
|
|
|
private $scopes = null; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
private $scopeSeparator; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var string |
|
59
|
|
|
*/ |
|
60
|
|
|
private $responseError = 'error'; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
private $responseCode; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var string |
|
69
|
|
|
*/ |
|
70
|
|
|
private $responseResourceOwnerId = 'id'; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param array $options |
|
74
|
|
|
* @param array $collaborators |
|
75
|
|
|
*/ |
|
76
|
18 |
|
public function __construct(array $options = [], array $collaborators = []) |
|
77
|
|
|
{ |
|
78
|
18 |
|
$possible = $this->getConfigurableOptions(); |
|
79
|
18 |
|
$configured = array_intersect_key($options, array_flip($possible)); |
|
80
|
|
|
|
|
81
|
18 |
|
foreach ($configured as $key => $value) { |
|
82
|
|
|
$this->$key = $value; |
|
83
|
12 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
// Remove all options that are only used locally |
|
86
|
18 |
|
$options = array_diff_key($options, $configured); |
|
87
|
|
|
|
|
88
|
18 |
|
parent::__construct($options, $collaborators); |
|
89
|
|
|
|
|
90
|
18 |
|
$this->setHttpClient(new Client(array_intersect_key( |
|
91
|
18 |
|
$options, |
|
92
|
18 |
|
array_flip(['verify', 'timeout']) |
|
93
|
12 |
|
))); |
|
94
|
18 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns all options that can be configured. |
|
98
|
|
|
* |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
18 |
|
protected function getConfigurableOptions() |
|
102
|
|
|
{ |
|
103
|
|
|
return [ |
|
104
|
18 |
|
'uriAuthorize', |
|
105
|
12 |
|
'uriAccessToken', |
|
106
|
12 |
|
'uriResourceOwnerDetails', |
|
107
|
12 |
|
'scopeSeparator', |
|
108
|
12 |
|
'responseError', |
|
109
|
12 |
|
'responseCode', |
|
110
|
12 |
|
'scopes', |
|
111
|
|
|
'apiUrl' |
|
112
|
12 |
|
]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
18 |
|
public function getBaseUri() |
|
116
|
|
|
{ |
|
117
|
18 |
|
return $this->apiUrl; |
|
118
|
2 |
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @inheritdoc |
|
122
|
|
|
*/ |
|
123
|
6 |
|
public function getBaseAuthorizationUrl() |
|
124
|
|
|
{ |
|
125
|
6 |
|
return $this->getBaseUri() . $this->uriAuthorize; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @inheritdoc |
|
130
|
|
|
*/ |
|
131
|
12 |
|
public function getBaseAccessTokenUrl(array $params) |
|
132
|
|
|
{ |
|
133
|
12 |
|
return $this->getBaseUri() . $this->uriAccessToken; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @inheritdoc |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->getBaseUri() . $this->uriResourceOwnerDetails; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @inheritdoc |
|
146
|
|
|
*/ |
|
147
|
3 |
|
public function getDefaultScopes() |
|
148
|
|
|
{ |
|
149
|
3 |
|
return $this->scopes; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @inheritdoc |
|
154
|
|
|
*/ |
|
155
|
3 |
|
protected function getScopeSeparator() |
|
156
|
|
|
{ |
|
157
|
3 |
|
return $this->scopeSeparator ?: parent::getScopeSeparator(); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @inheritdoc |
|
162
|
|
|
*/ |
|
163
|
9 |
|
protected function checkResponse(ResponseInterface $response, $data) |
|
164
|
|
|
{ |
|
165
|
9 |
|
if (!empty($data[$this->responseError])) { |
|
166
|
|
|
$error = $data[$this->responseError]; |
|
167
|
|
|
$code = $this->responseCode ? $data[$this->responseCode] : 0; |
|
168
|
|
|
throw new IdentityProviderException($error, $code, $data); |
|
169
|
|
|
} |
|
170
|
9 |
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @inheritdoc |
|
174
|
|
|
*/ |
|
175
|
|
|
protected function createResourceOwner(array $response, AccessToken $token) |
|
176
|
|
|
{ |
|
177
|
|
|
return new GenericResourceOwner($response, $this->responseResourceOwnerId); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param array $response |
|
182
|
|
|
* @param AbstractGrant $grant |
|
183
|
|
|
* @return AccessToken |
|
184
|
|
|
*/ |
|
185
|
9 |
|
protected function createAccessToken(array $response, AbstractGrant $grant) |
|
186
|
|
|
{ |
|
187
|
9 |
|
if (isset($response['data'])) { |
|
188
|
6 |
|
$data = $response['data']; |
|
189
|
6 |
|
return new AccessToken($data); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
3 |
|
throw new IdentityProviderException( |
|
193
|
3 |
|
'Bad Request', |
|
194
|
3 |
|
400, |
|
195
|
1 |
|
'Data provided for issuing access_token is not correct' |
|
196
|
2 |
|
); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Returns the default headers used by this provider. |
|
201
|
|
|
* |
|
202
|
|
|
* Typically this is used to set 'Accept' or 'Content-Type' headers. |
|
203
|
|
|
* |
|
204
|
|
|
* @return array |
|
205
|
|
|
*/ |
|
206
|
9 |
|
protected function getDefaultHeaders() |
|
207
|
|
|
{ |
|
208
|
|
|
return [ |
|
209
|
3 |
|
'Accept' => 'application/hal+json' |
|
210
|
6 |
|
]; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Builds request options used for requesting an access token. |
|
215
|
|
|
* |
|
216
|
|
|
* @param array $params |
|
217
|
|
|
* @return array |
|
218
|
|
|
*/ |
|
219
|
9 |
|
protected function getAccessTokenOptions(array $params) |
|
220
|
|
|
{ |
|
221
|
9 |
|
$options = ['headers' => ['content-type' => 'application/json']]; |
|
222
|
|
|
|
|
223
|
9 |
|
if ($this->getAccessTokenMethod() === self::METHOD_POST) { |
|
224
|
9 |
|
$options['body'] = $this->getAccessTokenBody($params); |
|
225
|
6 |
|
} |
|
226
|
|
|
|
|
227
|
9 |
|
return $options; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Returns the request body for requesting an access token. |
|
232
|
|
|
* |
|
233
|
|
|
* @param array $params |
|
234
|
|
|
* @return string |
|
235
|
|
|
*/ |
|
236
|
9 |
|
protected function getAccessTokenBody(array $params) |
|
237
|
|
|
{ |
|
238
|
9 |
|
return json_encode($params); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
3 |
|
public function getBearerAuthorizationHeader() |
|
242
|
|
|
{ |
|
243
|
3 |
|
$token = $this->getAccessToken('client_credentials')->getToken(); |
|
244
|
3 |
|
return $this->getAuthorizationHeaders($token); |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|