1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* OpenID OAuth2 client |
5
|
|
|
* |
6
|
|
|
* @package OAuth2-OpenID |
7
|
|
|
* @author Manuele Vaccari <[email protected]> |
8
|
|
|
* @copyright Copyright (c) 2017-2020 Manuele Vaccari <[email protected]> |
9
|
|
|
* @license https://github.com/D3strukt0r/oauth2-openid/blob/master/LICENSE.txt GNU General Public License v3.0 |
10
|
|
|
* @link https://github.com/D3strukt0r/oauth2-openid |
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 OpenIDProvider 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://openid.manuele-vaccari.ch'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Gets host. |
40
|
|
|
* |
41
|
|
|
* @return string returns the host |
42
|
|
|
*/ |
43
|
2 |
|
public function getHost(): string |
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 returns the class itself, for doing multiple things at once |
54
|
|
|
*/ |
55
|
1 |
|
public function setHost(string $host): self |
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 returns the URL for authorization |
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 returns the URL to retrieve the access token |
82
|
|
|
*/ |
83
|
5 |
|
public function getBaseAccessTokenUrl(array $params) |
84
|
|
|
{ |
85
|
5 |
|
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 returns the URL to retrieve the resources |
94
|
|
|
*/ |
95
|
1 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
96
|
|
|
{ |
97
|
1 |
|
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 returns the default scopes |
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 returns the separator required for the scopes |
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
|
4 |
|
protected function checkResponse(ResponseInterface $response, $data) |
132
|
|
|
{ |
133
|
4 |
|
$errorMessage = null; |
134
|
4 |
|
if ($response->getStatusCode() >= 400) { |
135
|
1 |
|
$errorMessage = isset($data['message']) ? $data['message'] : $response->getReasonPhrase(); |
136
|
3 |
|
} elseif (isset($data['error'])) { |
137
|
1 |
|
$errorMessage = isset($data['error']) ? $data['error'] : $response->getReasonPhrase(); |
138
|
|
|
} |
139
|
|
|
|
140
|
4 |
|
if (null !== $errorMessage) { |
141
|
2 |
|
throw new IdentityProviderException($errorMessage, $response->getStatusCode(), $response->getBody()); |
142
|
|
|
} |
143
|
2 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Generates a resource owner object from a successful resource owner |
147
|
|
|
* details request. |
148
|
|
|
* |
149
|
|
|
* @param array $response Response data from server |
150
|
|
|
* @param AccessToken $token The used access token |
151
|
|
|
* |
152
|
|
|
* @return ResourceOwnerInterface returns the resources |
153
|
|
|
*/ |
154
|
1 |
|
protected function createResourceOwner(array $response, AccessToken $token) |
155
|
|
|
{ |
156
|
1 |
|
return new OpenIDResourceOwner($response); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|