Passed
Pull Request — master (#22)
by Samuel
02:05
created

MultiAuthenticate::handle()   C

Complexity

Conditions 9
Paths 36

Size

Total Lines 61
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 61
ccs 27
cts 27
cp 1
rs 6.7603
cc 9
eloc 28
nc 36
nop 3
crap 9

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SMartins\PassportMultiauth\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Support\Facades\App;
7
use League\OAuth2\Server\ResourceServer;
8
use Illuminate\Auth\AuthenticationException;
9
use Illuminate\Auth\Middleware\Authenticate;
10
use Illuminate\Contracts\Auth\Factory as Auth;
11
use SMartins\PassportMultiauth\PassportMultiauth;
12
use SMartins\PassportMultiauth\ProviderRepository;
13
use SMartins\PassportMultiauth\Guards\GuardChecker;
14
use League\OAuth2\Server\Exception\OAuthServerException;
15
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
16
17
class MultiAuthenticate extends Authenticate
18
{
19
    /**
20
     * @var \League\OAuth2\Server\ResourceServer
21
     */
22
    protected $server;
23
24
    /**
25
     * @var \SMartins\PassportMultiauth\ProviderRepository
26
     */
27
    protected $providers;
28
29
    /**
30
     * The authentication factory instance.
31
     *
32
     * @var \Illuminate\Contracts\Auth\Factory
33
     */
34
    protected $auth;
35
36 12
    public function __construct(ResourceServer $server, ProviderRepository $providers, Auth $auth)
37
    {
38 12
        $this->server = $server;
39 12
        $this->providers = $providers;
40 12
        $this->auth = $auth;
41 12
    }
42
43
    /**
44
     * Handle an incoming request. Authenticates the guard from access token
45
     * used on request.
46
     *
47
     * @param \Illuminate\Http\Request $request
48
     * @param \Closure                 $next
49
     * @param string[]                 ...$guards
50
     *
51
     * @return mixed
52
     */
53 12
    public function handle($request, Closure $next, ...$guards)
54
    {
55
        // If don't has any guard follow the flow
56 12
        if (count($guards) === 0) {
57 1
            return $next($request);
58
        }
59
60 11
        $psrRequest = (new DiactorosFactory())->createRequest($request);
61
62
        try {
63 11
            $psrRequest = $this->server->validateAuthenticatedRequest($psrRequest);
64
65 4
            $tokenId = $psrRequest->getAttribute('oauth_access_token_id');
66
67 4
            if (! $tokenId) {
68 1
                throw new AuthenticationException('Unauthenticated', $guards);
69
            }
70
71 3
            $accessToken = $this->providers->findForToken($tokenId);
72
73 3
            if (! $accessToken) {
74 1
                throw new AuthenticationException('Unauthenticated', $guards);
75
            }
76
77 2
            $providers = collect($guards)->mapWithKeys(function ($guard) {
78 2
                return [GuardChecker::defaultGuardProvider($guard) => $guard];
79 2
            });
80
81
            // use only guard associated to access token provider
82 2
            if ($providers->has($accessToken->provider)) {
83 1
                $this->authenticate([$providers->get($accessToken->provider)]);
84
            } else {
85 1
                $this->authenticate([]);
86
            }
87
88 1
            return $next($request);
89 10
        } catch (OAuthServerException $e) {
90
            // @todo It's the best place to this code???
91
            //
92
            // If running unit test and try authenticate an user with
93
            // `PassportMultiauth::actingAs($user) check the guards on request
94
            // to authenticate or not the user.
95 7
            $user = app('auth')->user();
96
97 7
            if (App::runningUnitTests() && $user) {
98
                // @todo Move to method
99 6
                $guards = GuardChecker::getAuthGuards($request);
0 ignored issues
show
Deprecated Code introduced by
The method SMartins\PassportMultiau...hecker::getAuthGuards() has been deprecated with message: 2.0. This method is not more necessary. The guards are passed directly to MultiAuthenticate middleware on handle() params;

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
100
101 6
                $userGuard = PassportMultiauth::getUserGuard($user);
102
103 6
                if (! in_array($userGuard, $guards)) {
104 2
                    throw new AuthenticationException('Unauthenticated', $guards);
105
                }
106
107 4
                return $next($request);
108
            }
109
110
            // @todo Check if it's the best way to handle with OAuthServerException
111 1
            throw new AuthenticationException('Unauthenticated', $guards);
112
        }
113
    }
114
}
115