|
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
|
|
|
* The AccessTokenService |
|
54
|
|
|
* @var AccessTokenService |
|
55
|
|
|
*/ |
|
56
|
|
|
protected AccessTokenService $accessTokenService; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Create new instance |
|
60
|
|
|
* @param AccessTokenService $accessTokenService |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct( |
|
63
|
|
|
AccessTokenService $accessTokenService |
|
64
|
|
|
) { |
|
65
|
|
|
$this->accessTokenService = $accessTokenService; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
* Note that this method will only match tokens that are not expired and match the given scopes |
|
71
|
|
|
* (if any). If no token is pass, this method will return null, but if a token is given |
|
72
|
|
|
* and does not exist (ie. has been deleted) or is not valid, then it will trigger an exception |
|
73
|
|
|
* |
|
74
|
|
|
* @link http://tools.ietf.org/html/rfc6750#page-5 |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getAccessToken(ServerRequestInterface $request, $scopes = []): ?AccessToken |
|
77
|
|
|
{ |
|
78
|
|
|
$accessToken = $this->getTokenFromRequest($request); |
|
79
|
|
|
if ($accessToken === null) { |
|
80
|
|
|
return null; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** @var AccessToken|null $token */ |
|
84
|
|
|
$token = $this->accessTokenService->getToken($accessToken); |
|
85
|
|
|
if ($token === null || $token->isValid($scopes) === false) { |
|
86
|
|
|
throw InvalidAccessTokenException::invalidToken( |
|
87
|
|
|
'Access token has expired or has been deleted' |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $token; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Return the access token value using server request |
|
96
|
|
|
* @param ServerRequestInterface $request |
|
97
|
|
|
* @return string|null |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function getTokenFromRequest(ServerRequestInterface $request): ?string |
|
100
|
|
|
{ |
|
101
|
|
|
// The preferred way is using Authorization header |
|
102
|
|
|
if ($request->hasHeader('Authorization')) { |
|
103
|
|
|
// Header value is expected to be "Bearer xxx" |
|
104
|
|
|
$parts = explode(' ', $request->getHeaderLine('Authorization')); |
|
105
|
|
|
if (count($parts) < 2) { |
|
106
|
|
|
return null; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return end($parts); |
|
110
|
|
|
} |
|
111
|
|
|
// Default back to authorization in query param |
|
112
|
|
|
$queryParams = $request->getQueryParams(); |
|
113
|
|
|
|
|
114
|
|
|
return $queryParams['access_token'] ?? null; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|