|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2009 - 2019, Cake Development Corporation (https://www.cakedc.com) |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under The MIT License |
|
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright 2009 - 2019, Cake Development Corporation (https://www.cakedc.com) |
|
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace CakeDC\OAuth2\Client\Provider; |
|
12
|
|
|
|
|
13
|
|
|
use League\OAuth2\Client\Exception\HostedDomainException; |
|
14
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
|
15
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
|
16
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
|
17
|
|
|
use League\OAuth2\Client\Tool\BearerAuthorizationTrait; |
|
18
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
19
|
|
|
|
|
20
|
|
|
class Cognito extends AbstractProvider |
|
21
|
|
|
{ |
|
22
|
|
|
use BearerAuthorizationTrait; |
|
23
|
|
|
|
|
24
|
|
|
const BASE_COGNITO_URL = 'https://%s.auth.%s.amazoncognito.com%s'; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var array List of scopes that will be used for authentication. |
|
27
|
|
|
* |
|
28
|
|
|
* Valid scopes: phone, email, openid, aws.cognito.signin.user.admin, profile |
|
29
|
|
|
* Defaults to email, openid |
|
30
|
|
|
* |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $scopes = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string If set, it will replace default AWS Cognito urls. |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $hostedDomain; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string If set, it will be added to AWS Cognito urls. |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $cognitoDomain; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string If set, it will be added to AWS Cognito urls. |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $region; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param array $options |
|
51
|
|
|
* @param array $collaborators |
|
52
|
|
|
* |
|
53
|
|
|
* @throws \InvalidArgumentException |
|
54
|
|
|
*/ |
|
55
|
34 |
|
public function __construct($options = [], array $collaborators = []) |
|
56
|
|
|
{ |
|
57
|
34 |
|
parent::__construct($options, $collaborators); |
|
58
|
|
|
|
|
59
|
34 |
|
if (!empty($options['hostedDomain'])) { |
|
60
|
4 |
|
$this->hostedDomain = $options['hostedDomain']; |
|
61
|
34 |
|
} elseif (!empty($options['cognitoDomain']) && !empty($options['region'])) { |
|
62
|
34 |
|
$this->cognitoDomain = $options['cognitoDomain']; |
|
63
|
34 |
|
$this->region = $options['region']; |
|
64
|
|
|
} else { |
|
65
|
6 |
|
throw new \InvalidArgumentException( |
|
66
|
6 |
|
'Neither "cognitoDomain" and "region" nor "hostedDomain" options are set. Please set one of them.' |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
34 |
|
if (!empty($options['scope'])) { |
|
71
|
2 |
|
$this->scopes = explode($this->getScopeSeparator(), $options['scope']); |
|
72
|
|
|
} |
|
73
|
34 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
2 |
|
public function getScopes() |
|
79
|
|
|
{ |
|
80
|
2 |
|
return $this->scopes; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return mixed |
|
85
|
|
|
*/ |
|
86
|
4 |
|
public function getRegion() |
|
87
|
|
|
{ |
|
88
|
4 |
|
return $this->region; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param $region |
|
93
|
|
|
*/ |
|
94
|
2 |
|
public function setRegion($region) |
|
95
|
|
|
{ |
|
96
|
2 |
|
$this->region = $region; |
|
97
|
2 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
4 |
|
public function getHostedDomain() |
|
103
|
|
|
{ |
|
104
|
4 |
|
return $this->hostedDomain; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param string $hostedDomain |
|
109
|
|
|
*/ |
|
110
|
2 |
|
public function setHostedDomain($hostedDomain) |
|
111
|
|
|
{ |
|
112
|
2 |
|
$this->hostedDomain = $hostedDomain; |
|
113
|
2 |
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
4 |
|
public function getCognitoDomain() |
|
119
|
|
|
{ |
|
120
|
4 |
|
return $this->cognitoDomain; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param string $cognitoDomain |
|
125
|
|
|
*/ |
|
126
|
2 |
|
public function setCognitoDomain($cognitoDomain) |
|
127
|
|
|
{ |
|
128
|
2 |
|
$this->cognitoDomain = $cognitoDomain; |
|
129
|
2 |
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Returns the url for given action |
|
133
|
|
|
* |
|
134
|
|
|
* @param $action |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
16 |
|
private function getCognitoUrl($action) |
|
138
|
|
|
{ |
|
139
|
16 |
|
return !empty($this->hostedDomain) ? $this->hostedDomain . $action : |
|
140
|
16 |
|
sprintf(self::BASE_COGNITO_URL, $this->cognitoDomain, $this->region, $action); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
6 |
|
public function getBaseAuthorizationUrl() |
|
147
|
|
|
{ |
|
148
|
6 |
|
return $this->getCognitoUrl('/authorize'); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param array $params |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
10 |
|
public function getBaseAccessTokenUrl(array $params) |
|
156
|
|
|
{ |
|
157
|
10 |
|
return $this->getCognitoUrl('/token'); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param AccessToken $token |
|
162
|
|
|
* @return string |
|
163
|
|
|
*/ |
|
164
|
2 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
|
165
|
|
|
{ |
|
166
|
2 |
|
return $this->getCognitoUrl('/oauth2/userInfo'); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param array $options |
|
171
|
|
|
* @return array |
|
172
|
|
|
*/ |
|
173
|
6 |
|
protected function getAuthorizationParameters(array $options) |
|
174
|
|
|
{ |
|
175
|
6 |
|
$scopes = array_merge($this->getDefaultScopes(), $this->scopes); |
|
176
|
|
|
|
|
177
|
6 |
|
if (!empty($options['scope'])) { |
|
178
|
2 |
|
$scopes = array_merge($scopes, $options['scope']); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
6 |
|
$options['scope'] = array_unique($scopes); |
|
182
|
|
|
|
|
183
|
6 |
|
return parent::getAuthorizationParameters($options); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @return array |
|
188
|
|
|
*/ |
|
189
|
6 |
|
protected function getDefaultScopes() |
|
190
|
|
|
{ |
|
191
|
6 |
|
return ['openid', 'email']; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return string |
|
196
|
|
|
*/ |
|
197
|
8 |
|
protected function getScopeSeparator() |
|
198
|
|
|
{ |
|
199
|
8 |
|
return ' '; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @param ResponseInterface $response |
|
204
|
|
|
* @param array|string $data |
|
205
|
|
|
* @throws IdentityProviderException |
|
206
|
|
|
*/ |
|
207
|
8 |
|
protected function checkResponse(ResponseInterface $response, $data) |
|
208
|
|
|
{ |
|
209
|
8 |
|
if (empty($data['error'])) { |
|
210
|
6 |
|
return; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
2 |
|
$code = 0; |
|
214
|
2 |
|
$error = $data['error']; |
|
215
|
|
|
|
|
216
|
2 |
|
throw new IdentityProviderException($error, $code, $data); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @param array $response |
|
221
|
|
|
* @param AccessToken $token |
|
222
|
|
|
* @return CognitoUser|\League\OAuth2\Client\Provider\ResourceOwnerInterface |
|
223
|
|
|
*/ |
|
224
|
2 |
|
protected function createResourceOwner(array $response, AccessToken $token) |
|
225
|
|
|
{ |
|
226
|
2 |
|
$user = new CognitoUser($response); |
|
227
|
|
|
|
|
228
|
2 |
|
return $user; |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|