25
25
*/
26
26
abstract class OAuth2Provider extends OAuthProvider implements OAuth2Interface{
27
27
28
- /**
29
- * Specifies the authentication method:
30
- * - OAuth2Interface::AUTH_METHOD_HEADER (Bearer, OAuth, ...)
31
- * - OAuth2Interface::AUTH_METHOD_QUERY (access_token, ...)
32
- */
33
- protected int $ authMethod = self ::AUTH_METHOD_HEADER ;
34
-
35
- /**
36
- * The name of the authentication header in case of OAuth2Interface::AUTH_METHOD_HEADER
37
- */
38
- protected string $ authMethodHeader = 'Bearer ' ;
39
-
40
- /**
41
- * The name of the authentication query parameter in case of OAuth2Interface::AUTH_METHOD_QUERY
42
- */
43
- protected string $ authMethodQuery = 'access_token ' ;
44
-
45
- /**
46
- * The delimiter string for scopes
47
- */
48
- protected string $ scopesDelimiter = ' ' ;
49
-
50
28
/**
51
29
* An optional refresh token endpoint in case the provider supports TokenRefresh.
52
30
* If the provider supports token refresh and $refreshTokenURL is null, $accessTokenURL will be used instead.
@@ -61,17 +39,12 @@ abstract class OAuth2Provider extends OAuthProvider implements OAuth2Interface{
61
39
*/
62
40
protected string |null $ clientCredentialsTokenURL = null ;
63
41
64
- /**
65
- * Default scopes to apply if none were provided via the $scopes parameter in OAuth2Provider::getAuthURL()
66
- */
67
- protected array $ defaultScopes = [];
68
-
69
42
/**
70
43
* @inheritDoc
71
44
*/
72
45
public function getAuthURL (array |null $ params = null , array |null $ scopes = null ):UriInterface {
73
46
$ params ??= [];
74
- $ scopes ??= $ this -> defaultScopes ;
47
+ $ scopes ??= $ this :: DEFAULT_SCOPES ;
75
48
76
49
unset($ params ['client_secret ' ]);
77
50
@@ -83,7 +56,7 @@ public function getAuthURL(array|null $params = null, array|null $scopes = null)
83
56
]);
84
57
85
58
if (!empty ($ scopes )){
86
- $ params ['scope ' ] = implode ($ this -> scopesDelimiter , $ scopes );
59
+ $ params ['scope ' ] = implode ($ this :: SCOPE_DELIMITER , $ scopes );
87
60
}
88
61
89
62
if ($ this instanceof CSRFToken){
@@ -128,7 +101,7 @@ protected function parseTokenResponse(ResponseInterface $response):AccessToken{
128
101
if (isset ($ data ['scope ' ]) || isset ($ data ['scopes ' ])){
129
102
$ scope = ($ data ['scope ' ] ?? $ data ['scopes ' ] ?? []);
130
103
131
- $ token ->scopes = (is_array ($ scope )) ? $ scope : explode ($ this -> scopesDelimiter , $ scope );
104
+ $ token ->scopes = (is_array ($ scope )) ? $ scope : explode ($ this :: SCOPE_DELIMITER , $ scope );
132
105
}
133
106
134
107
unset($ data ['expires_in ' ], $ data ['refresh_token ' ], $ data ['access_token ' ], $ data ['scope ' ], $ data ['scopes ' ]);
@@ -161,7 +134,7 @@ public function getAccessToken(string $code, string|null $state = null):AccessTo
161
134
->withHeader ('Accept-Encoding ' , 'identity ' )
162
135
->withBody ($ this ->streamFactory ->createStream (QueryUtil::build ($ body , PHP_QUERY_RFC1738 )));
163
136
164
- foreach ($ this -> authHeaders as $ header => $ value ){
137
+ foreach ($ this :: HEADERS_AUTH as $ header => $ value ){
165
138
$ request = $ request ->withHeader ($ header , $ value );
166
139
}
167
140
@@ -177,17 +150,17 @@ public function getAccessToken(string $code, string|null $state = null):AccessTo
177
150
*/
178
151
public function getRequestAuthorization (RequestInterface $ request , AccessToken $ token ):RequestInterface {
179
152
180
- if ($ this -> authMethod === OAuth2Interface::AUTH_METHOD_HEADER ){
181
- return $ request ->withHeader ('Authorization ' , $ this -> authMethodHeader .' ' .$ token ->accessToken );
153
+ if ($ this :: AUTH_METHOD === OAuth2Interface::AUTH_METHOD_HEADER ){
154
+ return $ request ->withHeader ('Authorization ' , $ this :: AUTH_PREFIX_HEADER .' ' .$ token ->accessToken );
182
155
}
183
156
184
- if ($ this -> authMethod === OAuth2Interface::AUTH_METHOD_QUERY ){
185
- $ uri = QueryUtil::merge ((string )$ request ->getUri (), [$ this -> authMethodQuery => $ token ->accessToken ]);
157
+ if ($ this :: AUTH_METHOD === OAuth2Interface::AUTH_METHOD_QUERY ){
158
+ $ uri = QueryUtil::merge ((string )$ request ->getUri (), [$ this :: AUTH_PREFIX_QUERY => $ token ->accessToken ]);
186
159
187
160
return $ request ->withUri ($ this ->uriFactory ->createUri ($ uri ));
188
161
}
189
162
190
- throw new ProviderException ('invalid auth type ' );
163
+ throw new ProviderException ('invalid auth AUTH_METHOD ' );
191
164
}
192
165
193
166
/**
@@ -203,7 +176,7 @@ public function getClientCredentialsToken(array|null $scopes = null):AccessToken
203
176
$ params = ['grant_type ' => 'client_credentials ' ];
204
177
205
178
if (!empty ($ scopes )){
206
- $ params ['scope ' ] = implode ($ this -> scopesDelimiter , $ scopes );
179
+ $ params ['scope ' ] = implode ($ this :: SCOPE_DELIMITER , $ scopes );
207
180
}
208
181
209
182
$ request = $ this ->requestFactory
@@ -214,7 +187,7 @@ public function getClientCredentialsToken(array|null $scopes = null):AccessToken
214
187
->withBody ($ this ->streamFactory ->createStream (QueryUtil::build ($ params , PHP_QUERY_RFC1738 )))
215
188
;
216
189
217
- foreach ($ this -> authHeaders as $ header => $ value ){
190
+ foreach ($ this :: HEADERS_AUTH as $ header => $ value ){
218
191
$ request = $ request ->withAddedHeader ($ header , $ value );
219
192
}
220
193
@@ -267,7 +240,7 @@ public function refreshAccessToken(AccessToken|null $token = null):AccessToken{
267
240
->withBody ($ this ->streamFactory ->createStream (QueryUtil::build ($ body , PHP_QUERY_RFC1738 )))
268
241
;
269
242
270
- foreach ($ this -> authHeaders as $ header => $ value ){
243
+ foreach ($ this :: HEADERS_AUTH as $ header => $ value ){
271
244
$ request = $ request ->withAddedHeader ($ header , $ value );
272
245
}
273
246
0 commit comments