1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Generation 2 OAuth2 client |
5
|
|
|
* |
6
|
|
|
* @package OAuth2-Generation2 |
7
|
|
|
* @author Manuele Vaccari <[email protected]> |
8
|
|
|
* @copyright Copyright (c) 2017-2020 Manuele Vaccari <[email protected]> |
9
|
|
|
* @license https://github.com/D3strukt0r/oauth2-generation-2/blob/master/LICENSE.txt GNU General Public License v3.0 |
10
|
|
|
* @link https://github.com/D3strukt0r/oauth2-generation-2 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace D3strukt0r\OAuth2\Client\Provider; |
14
|
|
|
|
15
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
16
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
17
|
|
|
use League\OAuth2\Client\Provider\ResourceOwnerInterface; |
18
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
19
|
|
|
use League\OAuth2\Client\Tool\BearerAuthorizationTrait; |
20
|
|
|
use Psr\Http\Message\ResponseInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The Class in which all user information will be stored. |
24
|
|
|
*/ |
25
|
|
|
class Generation2Provider extends AbstractProvider |
26
|
|
|
{ |
27
|
|
|
use BearerAuthorizationTrait; |
28
|
|
|
|
29
|
|
|
public const ACCESS_TOKEN_RESOURCE_OWNER_ID = 'id'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Default host. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $host = 'https://account.generation-2.org'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Gets host. |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
2 |
|
public function getHost() |
44
|
|
|
{ |
45
|
2 |
|
return $this->host; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Sets host. Can be used for example when you testing the service-account in localhost. |
50
|
|
|
* |
51
|
|
|
* @param string $host The domain for accessing the user data |
52
|
|
|
* |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
1 |
|
public function setHost($host) |
56
|
|
|
{ |
57
|
1 |
|
$this->host = $host; |
58
|
|
|
|
59
|
1 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns the base URL for authorizing a client. |
64
|
|
|
* |
65
|
|
|
* Eg. https://oauth.service.com/authorize |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
3 |
|
public function getBaseAuthorizationUrl() |
70
|
|
|
{ |
71
|
3 |
|
return $this->host.'/oauth/authorize'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the base URL for requesting an access token. |
76
|
|
|
* |
77
|
|
|
* Eg. https://oauth.service.com/token |
78
|
|
|
* |
79
|
|
|
* @param array $params Special parameters |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
6 |
|
public function getBaseAccessTokenUrl(array $params) |
84
|
|
|
{ |
85
|
6 |
|
return $this->host.'/oauth/token'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the URL for requesting the resource owner's details. |
90
|
|
|
* |
91
|
|
|
* @param AccessToken $token The received access token from the server |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
4 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
96
|
|
|
{ |
97
|
4 |
|
return $this->host.'/oauth/resource'; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns the default scopes used by this provider. |
102
|
|
|
* |
103
|
|
|
* This should only be the scopes that are required to request the details |
104
|
|
|
* of the resource owner, rather than all the available scopes. |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
2 |
|
protected function getDefaultScopes() |
109
|
|
|
{ |
110
|
2 |
|
return []; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the string used to separate scopes. |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
3 |
|
protected function getScopeSeparator() |
119
|
|
|
{ |
120
|
3 |
|
return ' '; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Checks a provider response for errors. |
125
|
|
|
* |
126
|
|
|
* @param ResponseInterface $response The response from the server |
127
|
|
|
* @param array|string $data Parsed response data |
128
|
|
|
* |
129
|
|
|
* @throws IdentityProviderException |
130
|
|
|
*/ |
131
|
5 |
|
protected function checkResponse(ResponseInterface $response, $data) |
132
|
|
|
{ |
133
|
5 |
|
if ($response->getStatusCode() >= 400) { |
134
|
|
|
$errorMessage = isset($data['message']) ? $data['message'] : $response->getReasonPhrase(); |
135
|
5 |
|
} elseif (isset($data['error'])) { |
136
|
|
|
$errorMessage = isset($data['error']) ? $data['error'] : $response->getReasonPhrase(); |
137
|
|
|
} |
138
|
|
|
|
139
|
5 |
|
if (isset($errorMessage)) { |
140
|
|
|
throw new IdentityProviderException($errorMessage, $response->getStatusCode(), $response->getBody()); |
141
|
|
|
} |
142
|
5 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Generates a resource owner object from a successful resource owner |
146
|
|
|
* details request. |
147
|
|
|
* |
148
|
|
|
* @param array $response Response data from server |
149
|
|
|
* @param AccessToken $token The used access token |
150
|
|
|
* |
151
|
|
|
* @return ResourceOwnerInterface |
152
|
|
|
*/ |
153
|
4 |
|
protected function createResourceOwner(array $response, AccessToken $token) |
154
|
|
|
{ |
155
|
4 |
|
return new Generation2ResourceOwner($response); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|