|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Platine PHP |
|
5
|
|
|
* |
|
6
|
|
|
* Platine PHP is a lightweight, high-performance, simple and elegant |
|
7
|
|
|
* PHP Web framework |
|
8
|
|
|
* |
|
9
|
|
|
* This content is released under the MIT License (MIT) |
|
10
|
|
|
* |
|
11
|
|
|
* Copyright (c) 2020 Platine PHP |
|
12
|
|
|
* |
|
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
15
|
|
|
* in the Software without restriction, including without limitation the rights |
|
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
18
|
|
|
* furnished to do so, subject to the following conditions: |
|
19
|
|
|
* |
|
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
21
|
|
|
* copies or substantial portions of the Software. |
|
22
|
|
|
* |
|
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
29
|
|
|
* SOFTWARE. |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @file OAuth2ServiceProvider.php |
|
34
|
|
|
* |
|
35
|
|
|
* The OAuth2 service provider class |
|
36
|
|
|
* |
|
37
|
|
|
* @package Platine\Framework\Service\Provider |
|
38
|
|
|
* @author Platine Developers team |
|
39
|
|
|
* @copyright Copyright (c) 2020 |
|
40
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
41
|
|
|
* @link http://www.iacademy.cf |
|
42
|
|
|
* @version 1.0.0 |
|
43
|
|
|
* @filesource |
|
44
|
|
|
*/ |
|
45
|
|
|
|
|
46
|
|
|
declare(strict_types=1); |
|
47
|
|
|
|
|
48
|
|
|
namespace Platine\Framework\Service\Provider; |
|
49
|
|
|
|
|
50
|
|
|
use Platine\Config\Config; |
|
51
|
|
|
use Platine\Container\ContainerInterface; |
|
52
|
|
|
use Platine\Framework\OAuth2\Handler\AccessTokenRequestHandler; |
|
53
|
|
|
use Platine\Framework\OAuth2\Handler\AuthorizationRequestHandler; |
|
54
|
|
|
use Platine\Framework\OAuth2\Handler\TokenRevocationRequestHandler; |
|
55
|
|
|
use Platine\Framework\OAuth2\Middleware\OauthResourceMiddleware; |
|
56
|
|
|
use Platine\Framework\OAuth2\Repository\AccessTokenRepository; |
|
57
|
|
|
use Platine\Framework\OAuth2\Repository\AuthorizationCodeRepository; |
|
58
|
|
|
use Platine\Framework\OAuth2\Repository\ClientRepository; |
|
59
|
|
|
use Platine\Framework\OAuth2\Repository\RefreshTokenRepository; |
|
60
|
|
|
use Platine\Framework\OAuth2\Repository\ScopeRepository; |
|
61
|
|
|
use Platine\Framework\OAuth2\User\UserAuthentication; |
|
62
|
|
|
use Platine\Framework\Service\ServiceProvider; |
|
63
|
|
|
use Platine\Logger\LoggerInterface; |
|
64
|
|
|
use Platine\OAuth2\AuthorizationServer; |
|
65
|
|
|
use Platine\OAuth2\AuthorizationServerInterface; |
|
66
|
|
|
use Platine\OAuth2\Configuration; |
|
67
|
|
|
use Platine\OAuth2\Entity\UserAuthenticationInterface; |
|
68
|
|
|
use Platine\OAuth2\Grant\AuthorizationGrant; |
|
69
|
|
|
use Platine\OAuth2\Grant\ClientCredentialsGrant; |
|
70
|
|
|
use Platine\OAuth2\Grant\PasswordGrant; |
|
71
|
|
|
use Platine\OAuth2\Grant\RefreshTokenGrant; |
|
72
|
|
|
use Platine\OAuth2\Middleware\AuthorizationRequestMiddleware; |
|
73
|
|
|
use Platine\OAuth2\Middleware\ResourceServerMiddleware; |
|
74
|
|
|
use Platine\OAuth2\Middleware\RevocationRequestMiddleware; |
|
75
|
|
|
use Platine\OAuth2\Middleware\TokenRequestMiddleware; |
|
76
|
|
|
use Platine\OAuth2\Repository\AccessTokenRepositoryInterface; |
|
77
|
|
|
use Platine\OAuth2\Repository\AuthorizationCodeRepositoryInterface; |
|
78
|
|
|
use Platine\OAuth2\Repository\ClientRepositoryInterface; |
|
79
|
|
|
use Platine\OAuth2\Repository\RefreshTokenRepositoryInterface; |
|
80
|
|
|
use Platine\OAuth2\Repository\ScopeRepositoryInterface; |
|
81
|
|
|
use Platine\OAuth2\ResourceServer; |
|
82
|
|
|
use Platine\OAuth2\ResourceServerInterface; |
|
83
|
|
|
use Platine\OAuth2\Service\AccessTokenService; |
|
84
|
|
|
use Platine\OAuth2\Service\AuthorizationCodeService; |
|
85
|
|
|
use Platine\OAuth2\Service\ClientService; |
|
86
|
|
|
use Platine\OAuth2\Service\RefreshTokenService; |
|
87
|
|
|
use Platine\OAuth2\Service\ScopeService; |
|
88
|
|
|
use Platine\Route\Router; |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @class OAuth2ServiceProvider |
|
95
|
|
|
* @package Platine\App\Provider |
|
96
|
|
|
*/ |
|
97
|
|
|
class OAuth2ServiceProvider extends ServiceProvider |
|
98
|
|
|
{ |
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function register(): void |
|
103
|
|
|
{ |
|
104
|
|
|
|
|
105
|
|
|
// User authentication |
|
106
|
|
|
$this->app->bind(UserAuthenticationInterface::class, UserAuthentication::class); |
|
107
|
|
|
|
|
108
|
|
|
// Configuration |
|
109
|
|
|
$this->app->bind(Configuration::class, function (ContainerInterface $app) { |
|
110
|
|
|
return new Configuration($app->get(Config::class)->get('oauth2', [])); |
|
111
|
|
|
}); |
|
112
|
|
|
|
|
113
|
|
|
// Grants |
|
114
|
|
|
$this->app->bind(AuthorizationGrant::class); |
|
115
|
|
|
$this->app->bind(ClientCredentialsGrant::class); |
|
116
|
|
|
$this->app->bind(RefreshTokenGrant::class); |
|
117
|
|
|
$this->app->bind(PasswordGrant::class); |
|
118
|
|
|
|
|
119
|
|
|
// Repositories |
|
120
|
|
|
$this->app->bind(AccessTokenRepositoryInterface::class, AccessTokenRepository::class); |
|
121
|
|
|
$this->app->bind(ScopeRepositoryInterface::class, ScopeRepository::class); |
|
122
|
|
|
$this->app->bind(ClientRepositoryInterface::class, ClientRepository::class); |
|
123
|
|
|
$this->app->bind(RefreshTokenRepositoryInterface::class, RefreshTokenRepository::class); |
|
124
|
|
|
$this->app->bind(AuthorizationCodeRepositoryInterface::class, AuthorizationCodeRepository::class); |
|
125
|
|
|
|
|
126
|
|
|
// Services |
|
127
|
|
|
$this->app->bind(ScopeService::class); |
|
128
|
|
|
$this->app->bind(AccessTokenService::class); |
|
129
|
|
|
$this->app->bind(ClientService::class); |
|
130
|
|
|
$this->app->bind(RefreshTokenService::class); |
|
131
|
|
|
$this->app->bind(AuthorizationCodeService::class); |
|
132
|
|
|
|
|
133
|
|
|
// Servers |
|
134
|
|
|
$this->app->bind(ResourceServerInterface::class, ResourceServer::class); |
|
135
|
|
|
$this->app->bind(AuthorizationServerInterface::class, function (ContainerInterface $app) { |
|
136
|
|
|
/** @var Configuration $cfg */ |
|
137
|
|
|
$cfg = $app->get(Configuration::class); |
|
138
|
|
|
$grants = $cfg->getGrants(); |
|
139
|
|
|
$serverGrants = []; |
|
140
|
|
|
foreach ($grants as $grant) { |
|
141
|
|
|
$serverGrants[] = $app->get($grant); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return new AuthorizationServer( |
|
145
|
|
|
$app->get(ClientService::class), |
|
146
|
|
|
$app->get(AccessTokenService::class), |
|
147
|
|
|
$app->get(RefreshTokenService::class), |
|
148
|
|
|
$app->get(LoggerInterface::class), |
|
149
|
|
|
$serverGrants |
|
150
|
|
|
); |
|
151
|
|
|
}); |
|
152
|
|
|
|
|
153
|
|
|
// Middlewares |
|
154
|
|
|
$this->app->bind(ResourceServerMiddleware::class); |
|
155
|
|
|
$this->app->bind(AuthorizationRequestMiddleware::class); |
|
156
|
|
|
$this->app->bind(TokenRequestMiddleware::class); |
|
157
|
|
|
$this->app->bind(RevocationRequestMiddleware::class); |
|
158
|
|
|
$this->app->bind(OauthResourceMiddleware::class); |
|
159
|
|
|
|
|
160
|
|
|
// Handlers |
|
161
|
|
|
$this->app->bind(AccessTokenRequestHandler::class); |
|
162
|
|
|
$this->app->bind(AuthorizationRequestHandler::class); |
|
163
|
|
|
$this->app->bind(TokenRevocationRequestHandler::class); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* {@inheritdoc} |
|
168
|
|
|
*/ |
|
169
|
|
|
public function addRoutes(Router $router): void |
|
170
|
|
|
{ |
|
171
|
|
|
$router->group('/oauth2', function (Router $router) { |
|
172
|
|
|
$router->post('/token', AccessTokenRequestHandler::class, 'oauth2_access_token'); |
|
173
|
|
|
$router->add('/authorize', AuthorizationRequestHandler::class, ['GET', 'POST'], 'oauth2_authorization_code'); |
|
174
|
|
|
$router->post('/revocation', TokenRevocationRequestHandler::class, 'oauth2_revoke_access_token'); |
|
175
|
|
|
}); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|