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\Grant; |
34
|
|
|
|
35
|
|
|
use Platine\Http\ResponseInterface; |
36
|
|
|
use Platine\Http\ServerRequestInterface; |
37
|
|
|
use Platine\OAuth2\AuthorizationServerInterface; |
38
|
|
|
use Platine\OAuth2\Entity\Client; |
39
|
|
|
use Platine\OAuth2\Entity\TokenOwnerInterface; |
40
|
|
|
use Platine\OAuth2\Exception\OAuth2Exception; |
41
|
|
|
use Platine\OAuth2\Service\AccessTokenService; |
42
|
|
|
use Platine\OAuth2\Service\AuthorizationCodeService; |
43
|
|
|
use Platine\OAuth2\Service\RefreshTokenService; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @class AuthorizationGrant |
47
|
|
|
* @package Platine\OAuth2\Grant |
48
|
|
|
*/ |
49
|
|
|
class AuthorizationGrant extends BaseGrant implements AuthorizationServerAwareInterface |
50
|
|
|
{ |
51
|
|
|
public const GRANT_TYPE = 'authorization_code'; |
52
|
|
|
public const GRANT_RESPONSE_TYPE = 'code'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The AuthorizationCodeService |
56
|
|
|
* @var AuthorizationCodeService |
57
|
|
|
*/ |
58
|
|
|
protected AuthorizationCodeService $authorizationCodeService; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The AccessTokenService |
62
|
|
|
* @var AccessTokenService |
63
|
|
|
*/ |
64
|
|
|
protected AccessTokenService $accessTokenService; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* The RefreshTokenService |
68
|
|
|
* @var RefreshTokenService |
69
|
|
|
*/ |
70
|
|
|
protected RefreshTokenService $refreshTokenService; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* The authorization server instance |
74
|
|
|
* @var AuthorizationServerInterface|null |
75
|
|
|
*/ |
76
|
|
|
protected ?AuthorizationServerInterface $authorizationServer = null; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Create new instance |
80
|
|
|
* @param AuthorizationCodeService $authorizationCodeService |
81
|
|
|
* @param AccessTokenService $accessTokenService |
82
|
|
|
* @param RefreshTokenService $refreshTokenService |
83
|
|
|
*/ |
84
|
|
|
public function __construct( |
85
|
|
|
AuthorizationCodeService $authorizationCodeService, |
86
|
|
|
AccessTokenService $accessTokenService, |
87
|
|
|
RefreshTokenService $refreshTokenService |
88
|
|
|
) { |
89
|
|
|
$this->authorizationCodeService = $authorizationCodeService; |
90
|
|
|
$this->accessTokenService = $accessTokenService; |
91
|
|
|
$this->refreshTokenService = $refreshTokenService; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function createAuthorizationResponse( |
98
|
|
|
ServerRequestInterface $request, |
99
|
|
|
Client $client, |
100
|
|
|
?TokenOwnerInterface $owner = null |
101
|
|
|
): ResponseInterface { |
102
|
|
|
$queryParams = $request->getQueryParams(); |
103
|
|
|
|
104
|
|
|
// We must validate some parameters first |
105
|
|
|
$responseType = $queryParams['response_type'] ?? null; |
106
|
|
|
if ($responseType !== self::GRANT_RESPONSE_TYPE) { |
107
|
|
|
throw OAuth2Exception::invalidRequest(sprintf( |
108
|
|
|
'The desired grant type must be "code", but "%s" was given', |
109
|
|
|
$responseType |
110
|
|
|
)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// We try to fetch the redirect URI from query param as per spec, |
114
|
|
|
// and if none found, we just use the first redirect URI defined in the client |
115
|
|
|
$clientRedirectUris = $client->getRedirectUris(); |
116
|
|
|
$redirectUri = $queryParams['redirect_uri'] ?? $clientRedirectUris[0]; |
117
|
|
|
|
118
|
|
|
// If the redirect URI cannot be found in the list, we throw an error |
119
|
|
|
// as we don't want the user to be redirected to an unauthorized URL |
120
|
|
|
if ($client->hasRedirectUri($redirectUri) === false) { |
121
|
|
|
throw OAuth2Exception::invalidRequest('Redirect URI does not match the client registered one'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Scope and state allow to perform additional validation |
125
|
|
|
$scope = $queryParams['scope'] ?? null; |
|
|
|
|
126
|
|
|
$state = $queryParams['state'] ?? null; |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
|
|
public function createTokenResponse( |
133
|
|
|
ServerRequestInterface $request, |
134
|
|
|
?Client $client = null, |
135
|
|
|
?TokenOwnerInterface $owner = null |
136
|
|
|
): ResponseInterface { |
137
|
|
|
} |
|
|
|
|
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
|
|
public function setAuthorizationServer( |
143
|
|
|
AuthorizationServerInterface $authorizationServer |
144
|
|
|
): void { |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
|
|
public function allowPublicClients(): bool |
151
|
|
|
{ |
152
|
|
|
return true; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|