Passed
Pull Request — master (#22)
by Samuel
04:56 queued 02:08
created

MultiAuthenticate   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
c 2
b 0
f 0
lcom 1
cbo 10
dl 0
loc 116
ccs 35
cts 35
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C handle() 0 42 7
A canBeAuthenticated() 0 8 1
A authenticateTokenGuard() 0 13 2
1
<?php
2
3
namespace SMartins\PassportMultiauth\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Facades\App;
8
use League\OAuth2\Server\ResourceServer;
9
use Illuminate\Auth\AuthenticationException;
10
use Illuminate\Auth\Middleware\Authenticate;
11
use Illuminate\Contracts\Auth\Factory as Auth;
12
use Illuminate\Contracts\Auth\Authenticatable;
13
use SMartins\PassportMultiauth\Provider as Token;
14
use SMartins\PassportMultiauth\PassportMultiauth;
15
use SMartins\PassportMultiauth\ProviderRepository;
16
use SMartins\PassportMultiauth\Guards\GuardChecker;
17
use League\OAuth2\Server\Exception\OAuthServerException;
18
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
19
20
class MultiAuthenticate extends Authenticate
21
{
22
    /**
23
     * @var \League\OAuth2\Server\ResourceServer
24
     */
25
    protected $server;
26
27
    /**
28
     * @var \SMartins\PassportMultiauth\ProviderRepository
29
     */
30
    protected $providers;
31
32
    /**
33
     * The authentication factory instance.
34
     *
35
     * @var \Illuminate\Contracts\Auth\Factory
36
     */
37
    protected $auth;
38
39 12
    public function __construct(ResourceServer $server, ProviderRepository $providers, Auth $auth)
40
    {
41 12
        $this->server = $server;
42 12
        $this->providers = $providers;
43 12
        $this->auth = $auth;
44 12
    }
45
46
    /**
47
     * Handle an incoming request. Authenticates the guard from access token
48
     * used on request.
49
     *
50
     * @param \Illuminate\Http\Request $request
51
     * @param \Closure                 $next
52
     * @param string[]                 ...$guards
53
     *
54
     * @return mixed
55
     */
56 12
    public function handle($request, Closure $next, ...$guards)
57
    {
58
        // If don't has any guard follow the flow
59 12
        if (count($guards) === 0) {
60 1
            return $next($request);
61
        }
62
63 11
        $psrRequest = (new DiactorosFactory())->createRequest($request);
64
65
        try {
66 11
            $psrRequest = $this->server->validateAuthenticatedRequest($psrRequest);
67
68 4
            $tokenId = $psrRequest->getAttribute('oauth_access_token_id');
69
70 4
            if (! $tokenId) {
71 1
                throw new AuthenticationException('Unauthenticated', $guards);
72
            }
73
74 3
            $accessToken = $this->providers->findForToken($tokenId);
75
76 3
            if (! $accessToken) {
77 1
                throw new AuthenticationException('Unauthenticated', $guards);
78
            }
79
80 2
            $this->authenticateTokenGuard($accessToken, $guards);
81
82 1
            return $next($request);
83 10
        } catch (OAuthServerException $e) {
84
            // @todo It's the best place to this code???
85 7
            if ($user = PassportMultiauth::hasUserActing()) {
86
                // @todo Move to method
87 6
                if (! $this->canBeAuthenticated($user, $request)) {
88 2
                    throw new AuthenticationException('Unauthenticated', $guards);
89
                }
90
91 4
                return $next($request);
92
            }
93
94
            // @todo Check if it's the best way to handle with OAuthServerException
95 1
            throw new AuthenticationException('Unauthenticated', $guards);
96
        }
97
    }
98
99
    /**
100
     * Check if user acting has the required guards and scopes on request.
101
     *
102
     * @param  \Illuminate\Foundation\Auth\User $user
103
     * @param  \Illuminate\Http\Request $request
104
     * @return bool
105
     */
106 6
    public function canBeAuthenticated(Authenticatable $user, Request $request)
107
    {
108 6
        $guards = GuardChecker::getAuthGuards($request);
109
110 6
        $userGuard = PassportMultiauth::getUserGuard($user);
111
112 6
        return in_array($userGuard, $guards);
113
    }
114
115
    /**
116
     * Authenticate correct guard based on token.
117
     *
118
     * @param \SMartins\PassportMultiauth\Provider $token
119
     * @param  array $guards
120
     * @return void
121
     */
122 2
    public function authenticateTokenGuard(Token $token, $guards)
123
    {
124
        $providers = collect($guards)->mapWithKeys(function ($guard) {
125 2
            return [GuardChecker::defaultGuardProvider($guard) => $guard];
126 2
        });
127
128
        // use only guard associated to access token provider
129 2
        if ($providers->has($token->provider)) {
130 1
            $this->authenticate([$providers->get($token->provider)]);
131
        } else {
132 1
            $this->authenticate([]);
133
        }
134 1
    }
135
}
136