ResourceServer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 60
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAccessToken() 0 18 4
A getTokenFromRequest() 0 16 3
A __construct() 0 3 1
1
<?php
2
3
/**
4
 * Platine OAuth2
5
 *
6
 * Platine OAuth2 is a library that implements the OAuth2 specification
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine OAuth2
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
declare(strict_types=1);
32
33
namespace Platine\OAuth2;
34
35
use Platine\Http\ServerRequestInterface;
36
use Platine\OAuth2\Entity\AccessToken;
37
use Platine\OAuth2\Exception\InvalidAccessTokenException;
38
use Platine\OAuth2\Service\AccessTokenService;
39
40
/**
41
 * The resource server main role is to validate the access token and that its scope covers the
42
 * requested resource
43
 *
44
 * Currently, the resource server only implements the Bearer token usage, as described in the
45
 * RFC 6750 (http://tools.ietf.org/html/rfc6750)
46
 *
47
 * @class ResourceServer
48
 * @package Platine\OAuth2
49
 */
50
class ResourceServer implements ResourceServerInterface
51
{
52
    /**
53
     * Create new instance
54
     * @param AccessTokenService $accessTokenService
55
     */
56
    public function __construct(
57
        protected AccessTokenService $accessTokenService
58
    ) {
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     * Note that this method will only match tokens that are not expired and match the given scopes
64
     * (if any). If no token is pass, this method will return null, but if a token is given
65
     * and does not exist (ie. has been deleted) or is not valid, then it will trigger an exception
66
     *
67
     * @link   http://tools.ietf.org/html/rfc6750#page-5
68
     */
69
    public function getAccessToken(
70
        ServerRequestInterface $request,
71
        string|array $scopes = []
72
    ): ?AccessToken {
73
        $accessToken = $this->getTokenFromRequest($request);
74
        if ($accessToken === null) {
75
            return null;
76
        }
77
78
        /** @var AccessToken|null $token */
79
        $token = $this->accessTokenService->getToken($accessToken);
80
        if ($token === null || $token->isValid($scopes) === false) {
81
            throw InvalidAccessTokenException::invalidToken(
82
                'Access token has expired or has been deleted'
83
            );
84
        }
85
86
        return $token;
87
    }
88
89
    /**
90
     * Return the access token value using server request
91
     * @param ServerRequestInterface $request
92
     * @return string|null
93
     */
94
    protected function getTokenFromRequest(ServerRequestInterface $request): ?string
95
    {
96
        // The preferred way is using Authorization header
97
        if ($request->hasHeader('Authorization')) {
98
            // Header value is expected to be "Bearer xxx"
99
            $parts = explode(' ', $request->getHeaderLine('Authorization'));
100
            if (count($parts) < 2) {
101
                return null;
102
            }
103
104
            return end($parts);
105
        }
106
        // Default back to authorization in query param
107
        $queryParams = $request->getQueryParams();
108
109
        return $queryParams['access_token'] ?? null;
110
    }
111
}
112