1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ChrisHemmings\OAuth2\Client\Provider; |
4
|
|
|
|
5
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
6
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
7
|
|
|
use League\OAuth2\Client\Provider\ResourceOwnerInterface; |
8
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
9
|
|
|
use League\OAuth2\Client\Tool\BearerAuthorizationTrait; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
|
12
|
|
|
class Drupal extends AbstractProvider |
13
|
|
|
{ |
14
|
|
|
use BearerAuthorizationTrait; |
15
|
|
|
|
16
|
|
|
protected $baseUrl; |
17
|
|
|
|
18
|
21 |
|
public function getBaseUrl() |
19
|
|
|
{ |
20
|
21 |
|
return $this->baseUrl; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Get provider url to run authorization |
25
|
|
|
* |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
6 |
|
public function getBaseAuthorizationUrl() |
29
|
|
|
{ |
30
|
6 |
|
return $this->getBaseUrl() . '/oauth2/authorize'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get provider url to fetch token |
35
|
|
|
* |
36
|
|
|
* @param AccessToken $token |
|
|
|
|
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
12 |
|
public function getBaseAccessTokenUrl(array $params) |
41
|
|
|
{ |
42
|
12 |
|
return $this->getBaseUrl() . '/oauth2/token'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get provider url to fetch user details |
47
|
|
|
* |
48
|
|
|
* @param AccessToken $token |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
3 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
53
|
|
|
{ |
54
|
3 |
|
return $this->getBaseUrl() . '/oauth2/userInfo'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @Override |
59
|
|
|
* Requests resource owner details. |
60
|
|
|
* |
61
|
|
|
* @param AccessToken $token |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
3 |
|
protected function fetchResourceOwnerDetails(AccessToken $token) |
65
|
|
|
{ |
66
|
3 |
|
$url = $this->getResourceOwnerDetailsUrl($token); |
67
|
3 |
|
$request = $this->getAuthenticatedRequest(self::METHOD_POST, $url, $token); |
68
|
|
|
|
69
|
3 |
|
return $this->getResponse($request); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the default scopes used by this provider. |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
6 |
|
protected function getDefaultScopes() |
78
|
|
|
{ |
79
|
6 |
|
return ['openid', 'email', 'profile']; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns the string that should be used to separate scopes when building |
84
|
|
|
* the URL for requesting an access token. |
85
|
|
|
* |
86
|
|
|
* @return string Scope separator, defaults to ' ' |
87
|
|
|
*/ |
88
|
6 |
|
protected function getScopeSeparator() |
89
|
|
|
{ |
90
|
6 |
|
return ' '; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Check a provider response for errors. |
95
|
|
|
* |
96
|
|
|
* @param ResponseInterface $response |
97
|
|
|
* @param array|string $data |
98
|
|
|
* |
99
|
|
|
* @throws IdentityProviderException |
100
|
|
|
*/ |
101
|
9 |
|
protected function checkResponse(ResponseInterface $response, $data) |
102
|
|
|
{ |
103
|
9 |
|
if ($response->getStatusCode() >= 400) { |
104
|
3 |
|
throw new IdentityProviderException( |
105
|
3 |
|
$data['error'] ?: $response->getReasonPhrase(), |
106
|
3 |
|
$response->getStatusCode(), |
107
|
|
|
$response |
|
|
|
|
108
|
2 |
|
); |
109
|
|
|
} |
110
|
6 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Generate a user object from a successful user details request. |
114
|
|
|
* |
115
|
|
|
* @param array $response |
116
|
|
|
* @param AccessToken $token |
117
|
|
|
* |
118
|
|
|
* @return League\OAuth2\Client\Provider\ResourceOwnerInterface |
119
|
|
|
*/ |
120
|
3 |
|
protected function createResourceOwner(array $response, AccessToken $token) |
121
|
|
|
{ |
122
|
3 |
|
return new DrupalResourceOwner($response); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.