Completed
Pull Request — master (#22)
by Samuel
05:49 queued 02:27
created

MultiAuthenticate::handle()   C

Complexity

Conditions 7
Paths 23

Size

Total Lines 37
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 37
ccs 17
cts 17
cp 1
rs 6.7272
cc 7
eloc 18
nc 23
nop 3
crap 7
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 (empty($guards)) {
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
            if (! $tokenId = $psrRequest->getAttribute('oauth_access_token_id')) {
69 1
                throw new AuthenticationException('Unauthenticated', $guards);
70
            }
71
72 3
            if (! $accessToken = $this->providers->findForToken($tokenId)) {
73 1
                throw new AuthenticationException('Unauthenticated', $guards);
74
            }
75
76 2
            $this->authenticateTokenGuard($accessToken, $guards);
77
78 1
            return $next($request);
79 10
        } catch (OAuthServerException $e) {
80
            // @todo It's the best place to this code???
81 7
            if ($user = PassportMultiauth::userActing()) {
82 6
                if (! $this->canBeAuthenticated($user, $request)) {
83 2
                    throw new AuthenticationException('Unauthenticated', $guards);
84
                }
85
86 4
                return $next($request);
87
            }
88
89
            // @todo Check if it's the best way to handle with OAuthServerException
90 1
            throw new AuthenticationException('Unauthenticated', $guards);
91
        }
92
    }
93
94
    /**
95
     * Check if user acting has the required guards and scopes on request.
96
     *
97
     * @param  \Illuminate\Foundation\Auth\User $user
98
     * @param  \Illuminate\Http\Request $request
99
     * @return bool
100
     */
101 6
    public function canBeAuthenticated(Authenticatable $user, Request $request)
102
    {
103 6
        $guards = GuardChecker::getAuthGuards($request);
104
105 6
        $userGuard = PassportMultiauth::getUserGuard($user);
106
107 6
        return in_array($userGuard, $guards);
108
    }
109
110
    /**
111
     * Authenticate correct guard based on token.
112
     *
113
     * @param \SMartins\PassportMultiauth\Provider $token
114
     * @param  array $guards
115
     * @return void
116
     */
117 2
    public function authenticateTokenGuard(Token $token, $guards)
118
    {
119
        $providers = collect($guards)->mapWithKeys(function ($guard) {
120 2
            return [GuardChecker::defaultGuardProvider($guard) => $guard];
121 2
        });
122
123
        // use only guard associated to access token provider
124 2
        $authGuards = $providers->has($token->provider) ? [$providers->get($token->provider)] : [];
125
126 2
        $this->authenticate($authGuards);
127 1
    }
128
}
129