From 2a76ae31fc8f74659d54a504f3bd4e402e4354eb Mon Sep 17 00:00:00 2001 From: Michael Nabil Date: Thu, 2 Nov 2023 03:32:35 +0200 Subject: [PATCH 1/5] Standardise of `using` for middleware --- src/Http/Middleware/CheckCredentials.php | 11 +++++++++++ src/Http/Middleware/CheckForAnyScope.php | 11 +++++++++++ src/Http/Middleware/CheckScopes.php | 11 +++++++++++ src/Http/Middleware/CreateFreshApiToken.php | 13 +++++++++++++ tests/Feature/ActingAsTest.php | 21 +++++++++++++++++++++ 5 files changed, 67 insertions(+) diff --git a/src/Http/Middleware/CheckCredentials.php b/src/Http/Middleware/CheckCredentials.php index 4ece0e21c..e5d89f766 100644 --- a/src/Http/Middleware/CheckCredentials.php +++ b/src/Http/Middleware/CheckCredentials.php @@ -69,6 +69,17 @@ public function handle($request, Closure $next, ...$scopes) return $next($request); } + /** + * Generate a string representation of the middleware with specified scopes. + * + * @param array $scopes + * @return string + */ + public static function using(...$scopes) + { + return static::class . ':' . implode(',', $scopes); + } + /** * Validate the scopes and token on the incoming request. * diff --git a/src/Http/Middleware/CheckForAnyScope.php b/src/Http/Middleware/CheckForAnyScope.php index 9223dfe97..817b0cf7b 100644 --- a/src/Http/Middleware/CheckForAnyScope.php +++ b/src/Http/Middleware/CheckForAnyScope.php @@ -31,4 +31,15 @@ public function handle($request, $next, ...$scopes) throw new MissingScopeException($scopes); } + + /** + * Generate a string representation of the middleware with specified scopes. + * + * @param array $scopes + * @return string + */ + public static function using(...$scopes) + { + return static::class . ':' . implode(',', $scopes); + } } diff --git a/src/Http/Middleware/CheckScopes.php b/src/Http/Middleware/CheckScopes.php index af9f8596f..1b0c75b3f 100644 --- a/src/Http/Middleware/CheckScopes.php +++ b/src/Http/Middleware/CheckScopes.php @@ -31,4 +31,15 @@ public function handle($request, $next, ...$scopes) return $next($request); } + + /** + * Generate a string representation of the middleware with specified scopes. + * + * @param array $scopes + * @return string + */ + public static function using(...$scopes) + { + return static::class . ':' . implode(',', $scopes); + } } diff --git a/src/Http/Middleware/CreateFreshApiToken.php b/src/Http/Middleware/CreateFreshApiToken.php index 4ad06b2ec..f8e8c7a42 100644 --- a/src/Http/Middleware/CreateFreshApiToken.php +++ b/src/Http/Middleware/CreateFreshApiToken.php @@ -58,6 +58,19 @@ public function handle($request, Closure $next, $guard = null) return $response; } + /** + * Specify the guard for the middleware. + * + * @param string|null $guard + * @return string + */ + public static function using($guard = null) + { + $args = is_null($guard) ? '' : ':' . $guard; + + return static::class . $args; + } + /** * Determine if the given request should receive a fresh token. * diff --git a/tests/Feature/ActingAsTest.php b/tests/Feature/ActingAsTest.php index bf0b95319..61eaeb77f 100644 --- a/tests/Feature/ActingAsTest.php +++ b/tests/Feature/ActingAsTest.php @@ -48,6 +48,27 @@ public function testActingAsWhenTheRouteIsProtectedByCheckScopesMiddleware() $response->assertSee('bar'); } + public function testItCanGenerateDefinitionViaStaticMethod() + { + $signature = (string) CheckScopes::using(); + $this->assertSame('Laravel\Passport\Http\Middleware\CheckScopes', $signature); + + $signature = (string) CheckScopes::using('admin'); + $this->assertSame('Laravel\Passport\Http\Middleware\CheckScopes:admin', $signature); + + $signature = (string) CheckScopes::using('admin', 'footest'); + $this->assertSame('Laravel\Passport\Http\Middleware\CheckScopes:admin,footest', $signature); + + $signature = (string) CheckForAnyScope::using('admin', 'footest'); + $this->assertSame('Laravel\Passport\Http\Middleware\CheckForAnyScope:admin,footest', $signature); + + $signature = (string) CheckForAnyScope::using('admin', 'footest'); + $this->assertSame('Laravel\Passport\Http\Middleware\CheckForAnyScope:admin,footest', $signature); + + $signature = (string) CheckForAnyScope::using('admin', 'footest'); + $this->assertSame('Laravel\Passport\Http\Middleware\CheckForAnyScope:admin,footest', $signature); + } + public function testActingAsWhenTheRouteIsProtectedByCheckForAnyScopeMiddleware() { $this->withoutExceptionHandling(); From be2e4fbde0198c2554353bffbb39b8d70b252e83 Mon Sep 17 00:00:00 2001 From: Michael Nabil Date: Thu, 2 Nov 2023 03:37:39 +0200 Subject: [PATCH 2/5] Fix --- src/Http/Middleware/CheckCredentials.php | 4 ++-- src/Http/Middleware/CheckForAnyScope.php | 4 ++-- src/Http/Middleware/CheckScopes.php | 4 ++-- src/Http/Middleware/CreateFreshApiToken.php | 6 +++--- tests/Feature/ActingAsTest.php | 10 ++-------- 5 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/Http/Middleware/CheckCredentials.php b/src/Http/Middleware/CheckCredentials.php index e5d89f766..88393d30f 100644 --- a/src/Http/Middleware/CheckCredentials.php +++ b/src/Http/Middleware/CheckCredentials.php @@ -72,12 +72,12 @@ public function handle($request, Closure $next, ...$scopes) /** * Generate a string representation of the middleware with specified scopes. * - * @param array $scopes + * @param array $scopes * @return string */ public static function using(...$scopes) { - return static::class . ':' . implode(',', $scopes); + return static::class.':'.implode(',', $scopes); } /** diff --git a/src/Http/Middleware/CheckForAnyScope.php b/src/Http/Middleware/CheckForAnyScope.php index 817b0cf7b..f1a43c00c 100644 --- a/src/Http/Middleware/CheckForAnyScope.php +++ b/src/Http/Middleware/CheckForAnyScope.php @@ -35,11 +35,11 @@ public function handle($request, $next, ...$scopes) /** * Generate a string representation of the middleware with specified scopes. * - * @param array $scopes + * @param array $scopes * @return string */ public static function using(...$scopes) { - return static::class . ':' . implode(',', $scopes); + return static::class.':'.implode(',', $scopes); } } diff --git a/src/Http/Middleware/CheckScopes.php b/src/Http/Middleware/CheckScopes.php index 1b0c75b3f..6548d3c4b 100644 --- a/src/Http/Middleware/CheckScopes.php +++ b/src/Http/Middleware/CheckScopes.php @@ -35,11 +35,11 @@ public function handle($request, $next, ...$scopes) /** * Generate a string representation of the middleware with specified scopes. * - * @param array $scopes + * @param array $scopes * @return string */ public static function using(...$scopes) { - return static::class . ':' . implode(',', $scopes); + return static::class.':'.implode(',', $scopes); } } diff --git a/src/Http/Middleware/CreateFreshApiToken.php b/src/Http/Middleware/CreateFreshApiToken.php index f8e8c7a42..b4572de24 100644 --- a/src/Http/Middleware/CreateFreshApiToken.php +++ b/src/Http/Middleware/CreateFreshApiToken.php @@ -61,14 +61,14 @@ public function handle($request, Closure $next, $guard = null) /** * Specify the guard for the middleware. * - * @param string|null $guard + * @param string|null $guard * @return string */ public static function using($guard = null) { - $args = is_null($guard) ? '' : ':' . $guard; + $args = is_null($guard) ? '' : ':'.$guard; - return static::class . $args; + return static::class.$args; } /** diff --git a/tests/Feature/ActingAsTest.php b/tests/Feature/ActingAsTest.php index 61eaeb77f..3975b69d5 100644 --- a/tests/Feature/ActingAsTest.php +++ b/tests/Feature/ActingAsTest.php @@ -50,20 +50,14 @@ public function testActingAsWhenTheRouteIsProtectedByCheckScopesMiddleware() public function testItCanGenerateDefinitionViaStaticMethod() { - $signature = (string) CheckScopes::using(); - $this->assertSame('Laravel\Passport\Http\Middleware\CheckScopes', $signature); - $signature = (string) CheckScopes::using('admin'); $this->assertSame('Laravel\Passport\Http\Middleware\CheckScopes:admin', $signature); $signature = (string) CheckScopes::using('admin', 'footest'); $this->assertSame('Laravel\Passport\Http\Middleware\CheckScopes:admin,footest', $signature); - $signature = (string) CheckForAnyScope::using('admin', 'footest'); - $this->assertSame('Laravel\Passport\Http\Middleware\CheckForAnyScope:admin,footest', $signature); - - $signature = (string) CheckForAnyScope::using('admin', 'footest'); - $this->assertSame('Laravel\Passport\Http\Middleware\CheckForAnyScope:admin,footest', $signature); + $signature = (string) CheckForAnyScope::using('admin'); + $this->assertSame('Laravel\Passport\Http\Middleware\CheckForAnyScope:admin', $signature); $signature = (string) CheckForAnyScope::using('admin', 'footest'); $this->assertSame('Laravel\Passport\Http\Middleware\CheckForAnyScope:admin,footest', $signature); From 9ccbd60715b566b06c9d5ed4a0d7d888f611001c Mon Sep 17 00:00:00 2001 From: Michael Nabil Date: Thu, 2 Nov 2023 03:54:54 +0200 Subject: [PATCH 3/5] Use `func_get_args` --- src/Http/Middleware/CheckCredentials.php | 6 +++--- src/Http/Middleware/CheckForAnyScope.php | 6 +++--- src/Http/Middleware/CheckScopes.php | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Http/Middleware/CheckCredentials.php b/src/Http/Middleware/CheckCredentials.php index 88393d30f..5e7df728f 100644 --- a/src/Http/Middleware/CheckCredentials.php +++ b/src/Http/Middleware/CheckCredentials.php @@ -72,12 +72,12 @@ public function handle($request, Closure $next, ...$scopes) /** * Generate a string representation of the middleware with specified scopes. * - * @param array $scopes + * @param array|string $scopes * @return string */ - public static function using(...$scopes) + public static function using($scopes) { - return static::class.':'.implode(',', $scopes); + return static::class.':'.implode(',', func_get_args()); } /** diff --git a/src/Http/Middleware/CheckForAnyScope.php b/src/Http/Middleware/CheckForAnyScope.php index f1a43c00c..123ad6e9a 100644 --- a/src/Http/Middleware/CheckForAnyScope.php +++ b/src/Http/Middleware/CheckForAnyScope.php @@ -35,11 +35,11 @@ public function handle($request, $next, ...$scopes) /** * Generate a string representation of the middleware with specified scopes. * - * @param array $scopes + * @param array|string $scopes * @return string */ - public static function using(...$scopes) + public static function using($scopes) { - return static::class.':'.implode(',', $scopes); + return static::class.':'.implode(',', func_get_args()); } } diff --git a/src/Http/Middleware/CheckScopes.php b/src/Http/Middleware/CheckScopes.php index 6548d3c4b..724548488 100644 --- a/src/Http/Middleware/CheckScopes.php +++ b/src/Http/Middleware/CheckScopes.php @@ -35,11 +35,11 @@ public function handle($request, $next, ...$scopes) /** * Generate a string representation of the middleware with specified scopes. * - * @param array $scopes + * @param array|string $scopes * @return string */ - public static function using(...$scopes) + public static function using($scopes) { - return static::class.':'.implode(',', $scopes); + return static::class.':'. implode(',', func_get_args()); } } From 8184690a002a6b7129dfe94ef435b4ad8319b423 Mon Sep 17 00:00:00 2001 From: Michael Nabil Date: Thu, 2 Nov 2023 03:55:35 +0200 Subject: [PATCH 4/5] Formatting --- src/Http/Middleware/CheckScopes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/Middleware/CheckScopes.php b/src/Http/Middleware/CheckScopes.php index 724548488..515994655 100644 --- a/src/Http/Middleware/CheckScopes.php +++ b/src/Http/Middleware/CheckScopes.php @@ -40,6 +40,6 @@ public function handle($request, $next, ...$scopes) */ public static function using($scopes) { - return static::class.':'. implode(',', func_get_args()); + return static::class.':'.implode(',', func_get_args()); } } From ad36c47343a585521d7f10cb246c2c91bbf64c0b Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 2 Nov 2023 09:40:25 -0500 Subject: [PATCH 5/5] formatting --- src/Http/Middleware/CheckCredentials.php | 26 ++++++++++++--------- src/Http/Middleware/CheckForAnyScope.php | 26 ++++++++++++--------- src/Http/Middleware/CheckScopes.php | 26 ++++++++++++--------- src/Http/Middleware/CreateFreshApiToken.php | 26 ++++++++++----------- 4 files changed, 58 insertions(+), 46 deletions(-) diff --git a/src/Http/Middleware/CheckCredentials.php b/src/Http/Middleware/CheckCredentials.php index 5e7df728f..7c3992f6c 100644 --- a/src/Http/Middleware/CheckCredentials.php +++ b/src/Http/Middleware/CheckCredentials.php @@ -39,6 +39,21 @@ public function __construct(ResourceServer $server, TokenRepository $repository) $this->repository = $repository; } + /** + * Specify the scopes for the middleware. + * + * @param array|string $scopes + * @return string + */ + public static function using(...$scopes) + { + if (is_array($scopes[0])) { + return static::class.':'.implode(',', $scopes[0]); + } else { + return static::class.':'.implode(',', $scopes); + } + } + /** * Handle an incoming request. * @@ -69,17 +84,6 @@ public function handle($request, Closure $next, ...$scopes) return $next($request); } - /** - * Generate a string representation of the middleware with specified scopes. - * - * @param array|string $scopes - * @return string - */ - public static function using($scopes) - { - return static::class.':'.implode(',', func_get_args()); - } - /** * Validate the scopes and token on the incoming request. * diff --git a/src/Http/Middleware/CheckForAnyScope.php b/src/Http/Middleware/CheckForAnyScope.php index 123ad6e9a..c6409f8fb 100644 --- a/src/Http/Middleware/CheckForAnyScope.php +++ b/src/Http/Middleware/CheckForAnyScope.php @@ -7,6 +7,21 @@ class CheckForAnyScope { + /** + * Specify the scopes for the middleware. + * + * @param array|string $scopes + * @return string + */ + public static function using(...$scopes) + { + if (is_array($scopes[0])) { + return static::class.':'.implode(',', $scopes[0]); + } else { + return static::class.':'.implode(',', $scopes); + } + } + /** * Handle the incoming request. * @@ -31,15 +46,4 @@ public function handle($request, $next, ...$scopes) throw new MissingScopeException($scopes); } - - /** - * Generate a string representation of the middleware with specified scopes. - * - * @param array|string $scopes - * @return string - */ - public static function using($scopes) - { - return static::class.':'.implode(',', func_get_args()); - } } diff --git a/src/Http/Middleware/CheckScopes.php b/src/Http/Middleware/CheckScopes.php index 515994655..72d699a2e 100644 --- a/src/Http/Middleware/CheckScopes.php +++ b/src/Http/Middleware/CheckScopes.php @@ -7,6 +7,21 @@ class CheckScopes { + /** + * Specify the scopes for the middleware. + * + * @param array|string $scopes + * @return string + */ + public static function using(...$scopes) + { + if (is_array($scopes[0])) { + return static::class.':'.implode(',', $scopes[0]); + } else { + return static::class.':'.implode(',', $scopes); + } + } + /** * Handle the incoming request. * @@ -31,15 +46,4 @@ public function handle($request, $next, ...$scopes) return $next($request); } - - /** - * Generate a string representation of the middleware with specified scopes. - * - * @param array|string $scopes - * @return string - */ - public static function using($scopes) - { - return static::class.':'.implode(',', func_get_args()); - } } diff --git a/src/Http/Middleware/CreateFreshApiToken.php b/src/Http/Middleware/CreateFreshApiToken.php index b4572de24..1429362c8 100644 --- a/src/Http/Middleware/CreateFreshApiToken.php +++ b/src/Http/Middleware/CreateFreshApiToken.php @@ -35,6 +35,19 @@ public function __construct(ApiTokenCookieFactory $cookieFactory) $this->cookieFactory = $cookieFactory; } + /** + * Specify the guard for the middleware. + * + * @param string|null $guard + * @return string + */ + public static function using($guard = null) + { + $guard = is_null($guard) ? '' : ':'.$guard; + + return static::class.$guard; + } + /** * Handle an incoming request. * @@ -58,19 +71,6 @@ public function handle($request, Closure $next, $guard = null) return $response; } - /** - * Specify the guard for the middleware. - * - * @param string|null $guard - * @return string - */ - public static function using($guard = null) - { - $args = is_null($guard) ? '' : ':'.$guard; - - return static::class.$args; - } - /** * Determine if the given request should receive a fresh token. *