49
49
use function base64_decode ;
50
50
use function bin2hex ;
51
51
use function explode ;
52
- use function is_null ;
53
52
use function is_string ;
54
53
use function random_bytes ;
55
- use function strpos ;
56
54
use function substr ;
57
55
use function trim ;
58
56
@@ -129,19 +127,19 @@ public function setRefreshTokenTTL(DateInterval $refreshTokenTTL): void
129
127
/**
130
128
* Set the private key
131
129
*/
132
- public function setPrivateKey (CryptKeyInterface $ key ): void
130
+ public function setPrivateKey (CryptKeyInterface $ privateKey ): void
133
131
{
134
- $ this ->privateKey = $ key ;
132
+ $ this ->privateKey = $ privateKey ;
135
133
}
136
134
137
135
public function setDefaultScope (string $ scope ): void
138
136
{
139
137
$ this ->defaultScope = $ scope ;
140
138
}
141
139
142
- public function revokeRefreshTokens (bool $ revokeRefreshTokens ): void
140
+ public function revokeRefreshTokens (bool $ willRevoke ): void
143
141
{
144
- $ this ->revokeRefreshTokens = $ revokeRefreshTokens ;
142
+ $ this ->revokeRefreshTokens = $ willRevoke ;
145
143
}
146
144
147
145
/**
@@ -161,13 +159,9 @@ protected function validateClient(ServerRequestInterface $request): ClientEntity
161
159
$ client = $ this ->getClientEntityOrFail ($ clientId , $ request );
162
160
163
161
// If a redirect URI is provided ensure it matches what is pre-registered
164
- $ redirectUri = $ this ->getRequestParameter ('redirect_uri ' , $ request, null );
162
+ $ redirectUri = $ this ->getRequestParameter ('redirect_uri ' , $ request );
165
163
166
164
if ($ redirectUri !== null ) {
167
- if (!is_string ($ redirectUri )) {
168
- throw OAuthServerException::invalidRequest ('redirect_uri ' );
169
- }
170
-
171
165
$ this ->validateRedirectUri ($ redirectUri , $ client , $ request );
172
166
}
173
167
@@ -183,6 +177,7 @@ protected function validateClient(ServerRequestInterface $request): ClientEntity
183
177
* doesn't actually enforce non-null returns/exception-on-no-client so
184
178
* getClientEntity might return null. By contrast, this method will
185
179
* always either return a ClientEntityInterface or throw.
180
+ * @throws OAuthServerException
186
181
*/
187
182
protected function getClientEntityOrFail (string $ clientId , ServerRequestInterface $ request ): ClientEntityInterface
188
183
{
@@ -200,25 +195,22 @@ protected function getClientEntityOrFail(string $clientId, ServerRequestInterfac
200
195
* Gets the client credentials from the request from the request body or
201
196
* the Http Basic Authorization header
202
197
*
203
- * @return mixed[]
198
+ * @return array{0:non-empty-string,1:string}
199
+ * @throws OAuthServerException
204
200
*/
205
201
protected function getClientCredentials (ServerRequestInterface $ request ): array
206
202
{
207
203
[$ basicAuthUser , $ basicAuthPassword ] = $ this ->getBasicAuthCredentials ($ request );
208
204
209
205
$ clientId = $ this ->getRequestParameter ('client_id ' , $ request , $ basicAuthUser );
210
206
211
- if (is_null ( $ clientId) ) {
207
+ if ($ clientId === null ) {
212
208
throw OAuthServerException::invalidRequest ('client_id ' );
213
209
}
214
210
215
211
$ clientSecret = $ this ->getRequestParameter ('client_secret ' , $ request , $ basicAuthPassword );
216
212
217
- if ($ clientSecret !== null && !is_string ($ clientSecret )) {
218
- throw OAuthServerException::invalidRequest ('client_secret ' );
219
- }
220
-
221
- return [$ clientId , $ clientSecret ];
213
+ return [$ clientId , $ clientSecret ?? '' ];
222
214
}
223
215
224
216
/**
@@ -279,19 +271,43 @@ public function validateScopes(string|array|null $scopes, string $redirectUri =
279
271
*/
280
272
private function convertScopesQueryStringToArray (string $ scopes ): array
281
273
{
282
- return array_filter (explode (self ::SCOPE_DELIMITER_STRING , trim ($ scopes )), function ($ scope ) {
283
- return $ scope !== '' ;
284
- });
274
+ return array_filter (explode (self ::SCOPE_DELIMITER_STRING , trim ($ scopes )), static fn ($ scope ) => $ scope !== '' );
285
275
}
286
276
287
277
/**
288
- * Retrieve request parameter.
278
+ * Parse request parameter.
279
+ * @param array<array-key, mixed> $request
280
+ * @return non-empty-string|null
281
+ * @throws OAuthServerException
289
282
*/
290
- protected function getRequestParameter (string $ parameter , ServerRequestInterface $ request , mixed $ default = null ): mixed
283
+ private static function parseParam (string $ parameter , array $ request , ? string $ default = null ): ? string
291
284
{
292
- $ requestParameters = (array ) $ request ->getParsedBody ();
285
+ $ value = $ request [$ parameter ] ?? '' ;
286
+ if (is_scalar ($ value )) {
287
+ $ value = trim ((string )$ value );
288
+ } else {
289
+ throw OAuthServerException::invalidRequest ($ parameter );
290
+ }
291
+
292
+ if ($ value === '' ) {
293
+ $ value = $ default === null ? null : trim ($ default );
294
+ if ($ value === '' ) {
295
+ $ value = null ;
296
+ }
297
+ }
298
+
299
+ return $ value ;
300
+ }
293
301
294
- return $ requestParameters [$ parameter ] ?? $ default ;
302
+ /**
303
+ * Retrieve request parameter.
304
+ *
305
+ * @return non-empty-string|null
306
+ * @throws OAuthServerException
307
+ */
308
+ protected function getRequestParameter (string $ parameter , ServerRequestInterface $ request , ?string $ default = null ): ?string
309
+ {
310
+ return self ::parseParam ($ parameter , (array ) $ request ->getParsedBody (), $ default );
295
311
}
296
312
297
313
/**
@@ -301,7 +317,7 @@ protected function getRequestParameter(string $parameter, ServerRequestInterface
301
317
* not exist, or is otherwise an invalid HTTP Basic header, return
302
318
* [null, null].
303
319
*
304
- * @return string[]| null[]
320
+ * @return array{0:non-empty- string,1:string}|array{0: null,1:null}
305
321
*/
306
322
protected function getBasicAuthCredentials (ServerRequestInterface $ request ): array
307
323
{
@@ -310,7 +326,7 @@ protected function getBasicAuthCredentials(ServerRequestInterface $request): arr
310
326
}
311
327
312
328
$ header = $ request ->getHeader ('Authorization ' )[0 ];
313
- if (strpos ($ header , 'Basic ' ) !== 0 ) {
329
+ if (! str_starts_with ($ header , 'Basic ' )) {
314
330
return [null , null ];
315
331
}
316
332
@@ -320,35 +336,47 @@ protected function getBasicAuthCredentials(ServerRequestInterface $request): arr
320
336
return [null , null ];
321
337
}
322
338
323
- if (strpos ($ decoded , ': ' ) === false ) {
339
+ if (! str_contains ($ decoded , ': ' )) {
324
340
return [null , null ]; // HTTP Basic header without colon isn't valid
325
341
}
326
342
327
- return explode (': ' , $ decoded , 2 );
343
+ [$ username , $ password ] = explode (': ' , $ decoded , 2 );
344
+
345
+ if ($ username === '' ) {
346
+ return [null , null ];
347
+ }
348
+
349
+ return [$ username , $ password ];
328
350
}
329
351
330
352
/**
331
353
* Retrieve query string parameter.
354
+ * @return non-empty-string|null
355
+ * @throws OAuthServerException
332
356
*/
333
- protected function getQueryStringParameter (string $ parameter , ServerRequestInterface $ request , mixed $ default = null ): mixed
357
+ protected function getQueryStringParameter (string $ parameter , ServerRequestInterface $ request , ? string $ default = null ): ? string
334
358
{
335
- return isset ( $ request -> getQueryParams ()[ $ parameter]) ? $ request ->getQueryParams ()[ $ parameter ] : $ default ;
359
+ return self :: parseParam ( $ parameter, $ request ->getQueryParams (), $ default) ;
336
360
}
337
361
338
362
/**
339
363
* Retrieve cookie parameter.
364
+ * @return non-empty-string|null
365
+ * @throws OAuthServerException
340
366
*/
341
- protected function getCookieParameter (string $ parameter , ServerRequestInterface $ request , mixed $ default = null ): mixed
367
+ protected function getCookieParameter (string $ parameter , ServerRequestInterface $ request , ? string $ default = null ): ? string
342
368
{
343
- return isset ( $ request -> getCookieParams ()[ $ parameter]) ? $ request ->getCookieParams ()[ $ parameter ] : $ default ;
369
+ return self :: parseParam ( $ parameter, $ request ->getCookieParams (), $ default) ;
344
370
}
345
371
346
372
/**
347
373
* Retrieve server parameter.
374
+ * @return non-empty-string|null
375
+ * @throws OAuthServerException
348
376
*/
349
- protected function getServerParameter (string $ parameter , ServerRequestInterface $ request , mixed $ default = null ): mixed
377
+ protected function getServerParameter (string $ parameter , ServerRequestInterface $ request , ? string $ default = null ): ? string
350
378
{
351
- return isset ( $ request -> getServerParams ()[ $ parameter]) ? $ request ->getServerParams ()[ $ parameter ] : $ default ;
379
+ return self :: parseParam ( $ parameter, $ request ->getServerParams (), $ default) ;
352
380
}
353
381
354
382
/**
0 commit comments