|
1
|
|
|
<?php namespace Nord\Lumen\OAuth2; |
|
2
|
|
|
|
|
3
|
|
|
use Exception; |
|
4
|
|
|
use Illuminate\Contracts\Container\Container; |
|
5
|
|
|
use League\OAuth2\Server\Grant\AbstractGrant; |
|
6
|
|
|
use League\OAuth2\Server\Grant\RefreshTokenGrant; |
|
7
|
|
|
use League\OAuth2\Server\Storage\AuthCodeInterface; |
|
8
|
|
|
use League\OAuth2\Server\Storage\RefreshTokenInterface; |
|
9
|
|
|
use Nord\Lumen\OAuth2\Contracts\OAuth2Service as OAuth2ServiceContract; |
|
10
|
|
|
use Nord\Lumen\OAuth2\Exceptions\InvalidArgument; |
|
11
|
|
|
use Illuminate\Config\Repository as ConfigRepository; |
|
12
|
|
|
use Illuminate\Support\ServiceProvider; |
|
13
|
|
|
use League\OAuth2\Server\AuthorizationServer; |
|
14
|
|
|
use League\OAuth2\Server\Grant\PasswordGrant; |
|
15
|
|
|
use League\OAuth2\Server\ResourceServer; |
|
16
|
|
|
use League\OAuth2\Server\Storage\AccessTokenInterface; |
|
17
|
|
|
use League\OAuth2\Server\Storage\ClientInterface; |
|
18
|
|
|
use League\OAuth2\Server\Storage\ScopeInterface; |
|
19
|
|
|
use League\OAuth2\Server\Storage\SessionInterface; |
|
20
|
|
|
|
|
21
|
|
|
class OAuth2ServiceProvider extends ServiceProvider |
|
22
|
|
|
{ |
|
23
|
|
|
const CONFIG_KEY = 'oauth2'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @inheritdoc |
|
27
|
|
|
*/ |
|
28
|
|
|
public function register() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->app->configure(self::CONFIG_KEY); |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
$this->registerBindings($this->app, $this->app['config']); |
|
33
|
|
|
$this->registerFacades(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Registers container bindings. |
|
39
|
|
|
* |
|
40
|
|
|
* @param Container $container |
|
41
|
|
|
* @param ConfigRepository $config |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function registerBindings(Container $container, ConfigRepository $config) |
|
44
|
|
|
{ |
|
45
|
|
|
$container->bind(OAuth2Service::class, function ($container) use ($config) { |
|
46
|
|
|
return $this->createService($container, $config[self::CONFIG_KEY]); |
|
47
|
|
|
}); |
|
48
|
|
|
|
|
49
|
|
|
$container->alias(OAuth2Service::class, OAuth2ServiceContract::class); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Registers facades. |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function registerFacades() |
|
57
|
|
|
{ |
|
58
|
|
|
if (!class_exists('OAuth2')) { |
|
59
|
|
|
class_alias(OAuth2Facade::class, 'OAuth2'); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Creates the service instance. |
|
66
|
|
|
* |
|
67
|
|
|
* @param Container $container |
|
68
|
|
|
* @param array $config |
|
69
|
|
|
* |
|
70
|
|
|
* @return OAuth2Service |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function createService(Container $container, array $config) |
|
73
|
|
|
{ |
|
74
|
|
|
$authorizationServer = $this->createAuthorizationServer($container, $config); |
|
75
|
|
|
$resourceServer = $this->createResourceServer($container); |
|
76
|
|
|
|
|
77
|
|
|
return new OAuth2Service($authorizationServer, $resourceServer); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Creates the authorization instance. |
|
83
|
|
|
* |
|
84
|
|
|
* @param Container $container |
|
85
|
|
|
* @param array $config |
|
86
|
|
|
* |
|
87
|
|
|
* @return AuthorizationServer |
|
88
|
|
|
* @throws Exception |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function createAuthorizationServer(Container $container, array $config) |
|
91
|
|
|
{ |
|
92
|
|
|
// TODO: Support scopes |
|
93
|
|
|
|
|
94
|
|
|
$authorizationServer = $container->make(AuthorizationServer::class); |
|
95
|
|
|
|
|
96
|
|
|
$authorizationServer->setSessionStorage($container->make(SessionInterface::class)); |
|
97
|
|
|
$authorizationServer->setAccessTokenStorage($container->make(AccessTokenInterface::class)); |
|
98
|
|
|
$authorizationServer->setRefreshTokenStorage($container->make(RefreshTokenInterface::class)); |
|
99
|
|
|
$authorizationServer->setClientStorage($container->make(ClientInterface::class)); |
|
100
|
|
|
$authorizationServer->setScopeStorage($container->make(ScopeInterface::class)); |
|
101
|
|
|
$authorizationServer->setAuthCodeStorage($container->make(AuthCodeInterface::class)); |
|
102
|
|
|
|
|
103
|
|
|
$this->configureAuthorizationServer($authorizationServer, $config); |
|
104
|
|
|
|
|
105
|
|
|
return $authorizationServer; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Configures the authorization server instance. |
|
111
|
|
|
* |
|
112
|
|
|
* @param AuthorizationServer $authorizationServer |
|
113
|
|
|
* @param array $config |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function configureAuthorizationServer(AuthorizationServer $authorizationServer, array $config) |
|
116
|
|
|
{ |
|
117
|
|
|
if (isset($config['scope_param'])) { |
|
118
|
|
|
$authorizationServer->requireScopeParam($config['scope_param']); |
|
119
|
|
|
} |
|
120
|
|
|
if (isset($config['default_scope'])) { |
|
121
|
|
|
$authorizationServer->setDefaultScope($config['default_scope']); |
|
122
|
|
|
} |
|
123
|
|
|
if (isset($config['state_param'])) { |
|
124
|
|
|
$authorizationServer->requireStateParam($config['state_param']); |
|
125
|
|
|
} |
|
126
|
|
|
if (isset($config['scope_delimiter'])) { |
|
127
|
|
|
$authorizationServer->setScopeDelimiter($config['scope_delimiter']); |
|
128
|
|
|
} |
|
129
|
|
|
if (isset($config['access_token_ttl'])) { |
|
130
|
|
|
$authorizationServer->setAccessTokenTTL($config['access_token_ttl']); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$this->configureGrantTypes($authorizationServer, $config['grant_types']); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Configures the grant types for the authorization server instance. |
|
139
|
|
|
* |
|
140
|
|
|
* @param AuthorizationServer $authorizationServer |
|
141
|
|
|
* @param array $config |
|
142
|
|
|
* |
|
143
|
|
|
* @throws InvalidArgument |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function configureGrantTypes(AuthorizationServer $authorizationServer, array $config) |
|
146
|
|
|
{ |
|
147
|
|
|
// TODO: Support configuring of the remaining grant types |
|
148
|
|
|
foreach ($config as $name => $params) { |
|
149
|
|
|
if (!isset($params['class']) || !class_exists($params['class'])) { |
|
150
|
|
|
continue; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** @var AbstractGrant $grantType */ |
|
154
|
|
|
$grantType = new $params['class']; |
|
155
|
|
|
|
|
156
|
|
|
if (isset($params['access_token_ttl'])) { |
|
157
|
|
|
$grantType->setAccessTokenTTL($params['access_token_ttl']); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
if ($grantType instanceof PasswordGrant) { |
|
161
|
|
|
/** @var PasswordGrant $grantType */ |
|
162
|
|
|
if (isset($params['callback'])) { |
|
163
|
|
|
$grantType->setVerifyCredentialsCallback($params['callback']); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
if ($grantType instanceof RefreshTokenGrant) { |
|
168
|
|
|
/** @var RefreshTokenGrant $grantType */ |
|
169
|
|
|
if (isset($params['refresh_token_rotate'])) { |
|
170
|
|
|
$grantType->setRefreshTokenRotation($params['refresh_token_rotate']); |
|
171
|
|
|
} |
|
172
|
|
|
if (isset($params['refresh_token_ttl'])) { |
|
173
|
|
|
$grantType->setRefreshTokenTTL($params['refresh_token_ttl']); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
$authorizationServer->addGrantType($grantType); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Creates the resource server. |
|
184
|
|
|
* |
|
185
|
|
|
* @param Container $container |
|
186
|
|
|
* |
|
187
|
|
|
* @return ResourceServer |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function createResourceServer(Container $container) |
|
190
|
|
|
{ |
|
191
|
|
|
return $container->make(ResourceServer::class); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.