Code Duplication    Length = 26-27 lines in 2 locations

src/Http/Middleware/MultiAuthCheckForAnyScope.php 1 location

@@ 8-33 (lines=26) @@
5
use Illuminate\Auth\AuthenticationException;
6
use Laravel\Passport\Exceptions\MissingScopeException;
7
8
class MultiAuthCheckForAnyScope
9
{
10
    /**
11
     * Handle the incoming request.
12
     *
13
     * @param  \Illuminate\Http\Request  $request
14
     * @param  \Closure  $next
15
     * @param  mixed  ...$scopes
16
     * @return \Illuminate\Http\Response
17
     * @throws \Illuminate\Auth\AuthenticationException|\Laravel\Passport\Exceptions\MissingScopeException
18
     */
19
    public function handle($request, $next, ...$scopes)
20
    {
21
        if (! $request->user('api') || ! $request->user('api')->token()) {
22
            throw new AuthenticationException;
23
        }
24
25
        foreach ($scopes as $scope) {
26
            if ($request->user('api')->tokenCan($scope)) {
27
                return $next($request);
28
            }
29
        }
30
31
        throw new MissingScopeException($scopes);
32
    }
33
}
34

src/Http/Middleware/MultiAuthCheckScopes.php 1 location

@@ 8-34 (lines=27) @@
5
use Illuminate\Auth\AuthenticationException;
6
use Laravel\Passport\Exceptions\MissingScopeException;
7
8
class MultiAuthCheckScopes
9
{
10
    /**
11
     * Handle the incoming request.
12
     *
13
     * @param  \Illuminate\Http\Request  $request
14
     * @param  \Closure  $next
15
     * @param  mixed  ...$scopes
16
     * @return \Illuminate\Http\Response
17
     * @throws \Illuminate\Auth\AuthenticationException
18
     * @throws \Laravel\Passport\Exceptions\MissingScopeException
19
     */
20
    public function handle($request, $next, ...$scopes)
21
    {
22
        if (! $request->user('api') || ! $request->user('api')->token()) {
23
            throw new AuthenticationException;
24
        }
25
26
        foreach ($scopes as $scope) {
27
            if (! $request->user('api')->tokenCan($scope)) {
28
                throw new MissingScopeException($scope);
29
            }
30
        }
31
32
        return $next($request);
33
    }
34
}
35