CheckCredentials   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 94
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 19 2
A validate() 0 8 1
validateCredentials() 0 1 ?
validateScopes() 0 1 ?
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Oauth\Http\Middleware;
6
7
use Closure;
8
use Nyholm\Psr7\Factory\Psr17Factory;
9
use League\OAuth2\Server\ResourceServer;
10
use Illuminate\Auth\AuthenticationException;
11
use League\OAuth2\Server\Exception\OAuthServerException;
12
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
13
14
abstract class CheckCredentials
15
{
16
    /**
17
     * The Resource Server instance.
18
     *
19
     * @var \League\OAuth2\Server\ResourceServer
20
     */
21
    protected $server;
22
23
    /**
24
     * Create a new middleware instance.
25
     *
26
     * @param \League\OAuth2\Server\ResourceServer $server
27
     *
28
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
29
     */
30
    public function __construct(ResourceServer $server)
31
    {
32
        $this->server = $server;
33
    }
34
35
    /**
36
     * Handle an incoming request.
37
     *
38
     * @param \Illuminate\Http\Request $request
39
     * @param \Closure                 $next
40
     * @param mixed                    ...$scopes
41
     *
42
     * @throws \Illuminate\Auth\AuthenticationException
43
     *
44
     * @return mixed
45
     */
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $scopes a bit more specific; maybe use array.
Loading history...
46
    public function handle($request, Closure $next, ...$scopes)
47
    {
48
        $psr = (new PsrHttpFactory(
49
            new Psr17Factory(),
50
            new Psr17Factory(),
51
            new Psr17Factory(),
52
            new Psr17Factory()
53
        ))->createRequest($request);
54
55
        try {
56
            $psr = $this->server->validateAuthenticatedRequest($psr);
57
        } catch (OAuthServerException $e) {
58
            throw new AuthenticationException();
59
        }
60
61
        $this->validate($psr, $scopes);
62
63
        return $next($request);
64
    }
65
66
    /**
67
     * Validate the scopes and token on the incoming request.
68
     *
69
     * @param \Psr\Http\Message\ServerRequestInterface $psr
70
     * @param array                                    $scopes
71
     *
72
     * @throws \Rinvex\Oauth\Exceptions\MissingScopeException|\Illuminate\Auth\AuthenticationException
73
     *
74
     * @return void
75
     */
76
    protected function validate($psr, $scopes)
77
    {
78
        $accessToken = app('rinvex.oauth.access_token')->with(['abilities'])->where('identifier', $psr->getAttribute('oauth_access_token_id'))->first();
79
80
        $this->validateCredentials($accessToken);
81
82
        $this->validateScopes($accessToken, $scopes);
83
    }
84
85
    /**
86
     * Validate token credentials.
87
     *
88
     * @param \Rinvex\Oauth\Models\AccessToken $accessToken
89
     *
90
     * @throws \Illuminate\Auth\AuthenticationException
91
     *
92
     * @return void
93
     */
94
    abstract protected function validateCredentials($accessToken);
95
96
    /**
97
     * Validate token scopes.
98
     *
99
     * @param \Rinvex\Oauth\Models\AccessToken $accessToken
100
     * @param array                            $scopes
101
     *
102
     * @throws \Rinvex\Oauth\Exceptions\MissingScopeException
103
     *
104
     * @return void
105
     */
106
    abstract protected function validateScopes($accessToken, $scopes);
107
}
108