diff --git a/src/Http/Middleware/CheckCredentials.php b/src/Http/Middleware/CheckCredentials.php index 4ece0e21c..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. * diff --git a/src/Http/Middleware/CheckForAnyScope.php b/src/Http/Middleware/CheckForAnyScope.php index 9223dfe97..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. * diff --git a/src/Http/Middleware/CheckScopes.php b/src/Http/Middleware/CheckScopes.php index af9f8596f..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. * diff --git a/src/Http/Middleware/CreateFreshApiToken.php b/src/Http/Middleware/CreateFreshApiToken.php index 4ad06b2ec..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. * diff --git a/tests/Feature/ActingAsTest.php b/tests/Feature/ActingAsTest.php index 031d5abf4..d67f93798 100644 --- a/tests/Feature/ActingAsTest.php +++ b/tests/Feature/ActingAsTest.php @@ -47,6 +47,21 @@ public function testActingAsWhenTheRouteIsProtectedByCheckScopesMiddleware() $response->assertSee('bar'); } + public function testItCanGenerateDefinitionViaStaticMethod() + { + $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'); + $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); + } + public function testActingAsWhenTheRouteIsProtectedByCheckForAnyScopeMiddleware() { $this->withoutExceptionHandling();