1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Module.php |
4
|
|
|
* |
5
|
|
|
* PHP version 5.6+ |
6
|
|
|
* |
7
|
|
|
* @author Philippe Gaultier <[email protected]> |
8
|
|
|
* @copyright 2010-2017 Philippe Gaultier |
9
|
|
|
* @license http://www.sweelix.net/license license |
10
|
|
|
* @version 1.1.0 |
11
|
|
|
* @link http://www.sweelix.net |
12
|
|
|
* @package sweelix\oauth2\server |
13
|
|
|
*/ |
14
|
|
|
namespace sweelix\oauth2\server; |
15
|
|
|
|
16
|
|
|
use sweelix\oauth2\server\services\Oauth; |
17
|
|
|
use sweelix\oauth2\server\services\Redis; |
18
|
|
|
use yii\base\BootstrapInterface; |
19
|
|
|
use yii\base\Module as BaseModule; |
20
|
|
|
use yii\console\Application as ConsoleApplication; |
21
|
|
|
use Yii; |
22
|
|
|
use yii\di\Instance; |
23
|
|
|
use yii\helpers\ArrayHelper; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Oauth2 server Module definition |
27
|
|
|
* |
28
|
|
|
* @author Philippe Gaultier <[email protected]> |
29
|
|
|
* @copyright 2010-2017 Philippe Gaultier |
30
|
|
|
* @license http://www.sweelix.net/license license |
31
|
|
|
* @version 1.1.0 |
32
|
|
|
* @link http://www.sweelix.net |
33
|
|
|
* @package sweelix\oauth2\server |
34
|
|
|
* @since 1.0.0 |
35
|
|
|
*/ |
36
|
|
|
class Module extends BaseModule implements BootstrapInterface |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var string backend to use, available backends are 'redis' |
40
|
|
|
*/ |
41
|
|
|
public $backend; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Connection|array|string the DB connection object or the application component ID of the DB connection. |
45
|
|
|
*/ |
46
|
|
|
public $db; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string override layout. For example @app/views/layouts/oauth2 to use <app>/views/layouts/oauth2.php layout |
50
|
|
|
*/ |
51
|
|
|
public $overrideLayout; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string override view path. For example @app/views/oauth2 to use <app>/views/oauth2/(authorize|login|error) views |
55
|
|
|
*/ |
56
|
|
|
public $overrideViewPath; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* This user class will be used to link oauth2 authorization system with the application. |
60
|
|
|
* The class must implement \sweelix\oauth2\server\interfaces\UserInterface |
61
|
|
|
* If not defined, the Yii::$app->user->identityClass value will be used |
62
|
|
|
* @var string|array user class definition. |
63
|
|
|
*/ |
64
|
|
|
public $identityClass; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var string used to separate user session between this module and current application |
68
|
|
|
*/ |
69
|
|
|
public $webUserParamId = '__oauth2'; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var string used to separate identity cookies between this module and current application |
73
|
|
|
*/ |
74
|
|
|
public $identityCookieName = 'oauth2'; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var array webUser configuration specific to this module |
78
|
|
|
*/ |
79
|
|
|
public $webUser = []; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @var string change base end point |
83
|
|
|
*/ |
84
|
|
|
public $baseEndPoint = ''; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var bool configure oauth server (use_jwt_access_tokens) |
88
|
|
|
*/ |
89
|
|
|
public $allowJwtAccessToken = false; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var array configure oauth server (allowed_algorithms) |
93
|
|
|
*/ |
94
|
|
|
public $allowAlgorithm = ['RS256', 'RS384', 'RS512']; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @var string|array jwt audience. Default to token endpoint |
98
|
|
|
*/ |
99
|
|
|
public $jwtAudience = ['token/index']; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @var bool configure oauth server (store_encrypted_token_string) |
103
|
|
|
*/ |
104
|
|
|
public $storeEncryptedTokenString = true; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @var bool configure oauth server (use_openid_connect) |
108
|
|
|
*/ |
109
|
|
|
public $allowOpenIdConnect = false; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @var int configure oauth server (id_lifetime) |
113
|
|
|
*/ |
114
|
|
|
public $idTTL = 3600; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @var int configure oauth server (access_lifetime) |
118
|
|
|
*/ |
119
|
|
|
public $accessTokenTTL = 3600; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @var int configure oauth server (refresh_token_lifetime) |
123
|
|
|
*/ |
124
|
|
|
public $refreshTokenTTL = 1209600; |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @var string configure oauth server (www_realm) |
128
|
|
|
*/ |
129
|
|
|
public $realm = 'Service'; |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @var string configure oauth server (token_param_name) |
133
|
|
|
*/ |
134
|
|
|
public $tokenQueryName = 'access_token'; |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @var string configure oauth server (token_bearer_header_name) |
138
|
|
|
*/ |
139
|
|
|
public $tokenBearerName = 'Bearer'; |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @var bool configure oauth server (enforce_state) |
143
|
|
|
*/ |
144
|
|
|
public $enforceState = true; |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @var bool configure oauth server (require_exact_redirect_uri) |
148
|
|
|
*/ |
149
|
|
|
public $allowOnlyRedirectUri = true; |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @var bool configure oauth server (allow_implicit) |
153
|
|
|
*/ |
154
|
|
|
public $allowImplicit = false; |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @var bool allow authorization code grant |
158
|
|
|
*/ |
159
|
|
|
public $allowAuthorizationCode = true; |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @var bool allow client credentials grant |
163
|
|
|
*/ |
164
|
|
|
public $allowClientCredentials = true; |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @var bool allow password grant |
168
|
|
|
*/ |
169
|
|
|
public $allowPassword = true; |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @var bool configure oauth server (allow_credentials_in_request_body) |
173
|
|
|
*/ |
174
|
|
|
public $allowCredentialsInRequestBody = true; |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @var bool configure oauth server (allow_public_clients) |
178
|
|
|
*/ |
179
|
|
|
public $allowPublicClients = true; |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @var bool configure oauth server (always_issue_new_refresh_token) |
183
|
|
|
*/ |
184
|
|
|
public $alwaysIssueNewRefreshToken = true; |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @var bool configure oauth server (unset_refresh_token_after_use) |
188
|
|
|
*/ |
189
|
|
|
public $unsetRefreshTokenAfterUse = false; |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @var int duration of login time for multiple authorize calls |
193
|
|
|
*/ |
194
|
|
|
public $loginDuration = 60 * 60 * 24 * 30; |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @var false|array Cors configuration if allowed @see http://www.yiiframework.com/doc-2.0/yii-filters-cors.html |
198
|
|
|
*/ |
199
|
|
|
public $cors = false; |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @inheritdoc |
203
|
|
|
*/ |
204
|
48 |
|
public function init() |
205
|
|
|
{ |
206
|
48 |
|
parent::init(); |
207
|
48 |
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Load dataservices in container |
211
|
|
|
* @param \yii\base\Application $app |
212
|
|
|
* @since 1.0.0 |
213
|
|
|
*/ |
214
|
48 |
|
protected function setUpDi($app) |
215
|
|
|
{ |
216
|
48 |
|
if (Yii::$container->has('scope') === false) { |
217
|
|
|
Yii::$container->set('scope', 'sweelix\oauth2\server\validators\ScopeValidator'); |
218
|
|
|
} |
219
|
48 |
|
if (Yii::$container->has('sweelix\oauth2\server\interfaces\AccessTokenModelInterface') === false) { |
220
|
|
|
Yii::$container->set('sweelix\oauth2\server\interfaces\AccessTokenModelInterface', 'sweelix\oauth2\server\models\AccessToken'); |
221
|
|
|
} |
222
|
48 |
|
if (Yii::$container->has('sweelix\oauth2\server\interfaces\AuthCodeModelInterface') === false) { |
223
|
|
|
Yii::$container->set('sweelix\oauth2\server\interfaces\AuthCodeModelInterface', 'sweelix\oauth2\server\models\AuthCode'); |
224
|
|
|
} |
225
|
48 |
|
if (Yii::$container->has('sweelix\oauth2\server\interfaces\ClientModelInterface') === false) { |
226
|
|
|
Yii::$container->set('sweelix\oauth2\server\interfaces\ClientModelInterface', 'sweelix\oauth2\server\models\Client'); |
227
|
|
|
} |
228
|
48 |
|
if (Yii::$container->has('sweelix\oauth2\server\interfaces\CypherKeyModelInterface') === false) { |
229
|
|
|
Yii::$container->set('sweelix\oauth2\server\interfaces\CypherKeyModelInterface', 'sweelix\oauth2\server\models\CypherKey'); |
230
|
|
|
} |
231
|
48 |
|
if (Yii::$container->has('sweelix\oauth2\server\interfaces\JtiModelInterface') === false) { |
232
|
|
|
Yii::$container->set('sweelix\oauth2\server\interfaces\JtiModelInterface', 'sweelix\oauth2\server\models\Jti'); |
233
|
|
|
} |
234
|
48 |
|
if (Yii::$container->has('sweelix\oauth2\server\interfaces\JwtModelInterface') === false) { |
235
|
|
|
Yii::$container->set('sweelix\oauth2\server\interfaces\JwtModelInterface', 'sweelix\oauth2\server\models\Jwt'); |
236
|
|
|
} |
237
|
48 |
|
if (Yii::$container->has('sweelix\oauth2\server\interfaces\RefreshTokenModelInterface') === false) { |
238
|
|
|
Yii::$container->set('sweelix\oauth2\server\interfaces\RefreshTokenModelInterface', 'sweelix\oauth2\server\models\RefreshToken'); |
239
|
|
|
} |
240
|
48 |
|
if (Yii::$container->has('sweelix\oauth2\server\interfaces\ScopeModelInterface') === false) { |
241
|
|
|
Yii::$container->set('sweelix\oauth2\server\interfaces\ScopeModelInterface', 'sweelix\oauth2\server\models\Scope'); |
242
|
|
|
} |
243
|
48 |
|
if ((Yii::$container->has('sweelix\oauth2\server\interfaces\UserModelInterface') === false) && ($this->identityClass !== null)) { |
244
|
|
|
Yii::$container->set('sweelix\oauth2\server\interfaces\UserModelInterface', $this->identityClass); |
245
|
|
|
} |
246
|
48 |
|
if ($this->backend === 'redis') { |
247
|
48 |
|
Redis::register($app); |
248
|
48 |
|
} |
249
|
48 |
|
Oauth::register($app); |
250
|
|
|
|
251
|
48 |
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @inheritdoc |
255
|
|
|
*/ |
256
|
48 |
|
public function bootstrap($app) |
257
|
|
|
{ |
258
|
|
|
// use the registered identity class if not overloaded |
259
|
48 |
|
if (($this->identityClass === null) && (isset($app->user) === true)) { |
260
|
12 |
|
$this->identityClass = $app->user->identityClass; |
261
|
12 |
|
} |
262
|
48 |
|
$this->setUpDi($app); |
263
|
48 |
|
if (empty($this->baseEndPoint) === false) { |
264
|
|
|
$this->baseEndPoint = trim($this->baseEndPoint, '/').'/'; |
265
|
|
|
} |
266
|
|
|
|
267
|
48 |
|
if ($app instanceof ConsoleApplication) { |
268
|
36 |
|
$this->mapConsoleControllers($app); |
269
|
36 |
|
} else { |
270
|
12 |
|
$app->getUrlManager()->addRules([ |
271
|
12 |
|
['verb' => 'POST', 'pattern' => $this->baseEndPoint.'token', 'route' => $this->id.'/token/index'], |
272
|
12 |
|
['verb' => 'OPTIONS', 'pattern' => $this->baseEndPoint.'token', 'route' => $this->id.'/token/options'], |
273
|
12 |
|
['verb' => 'GET', 'pattern' => $this->baseEndPoint.'authorize', 'route' => $this->id.'/authorize/index'], |
274
|
12 |
|
['pattern' => $this->baseEndPoint.'authorize-login', 'route' => $this->id.'/authorize/login'], |
275
|
12 |
|
['pattern' => $this->baseEndPoint.'authorize-application', 'route' => $this->id.'/authorize/authorize'], |
276
|
12 |
|
['pattern' => $this->baseEndPoint.'authorize-error', 'route' => $this->id.'/authorize/error'], |
277
|
12 |
|
]); |
278
|
|
|
} |
279
|
48 |
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @inheritdoc |
283
|
|
|
*/ |
284
|
17 |
|
public function beforeAction($action) |
285
|
|
|
{ |
286
|
17 |
|
$status = parent::beforeAction($action); |
287
|
|
|
// override web user to avoid conflicts only when routing into this module |
288
|
17 |
|
if ($status === true) { |
289
|
|
|
$userConfig = [ |
290
|
17 |
|
'class' => 'yii\web\User', |
291
|
17 |
|
'identityClass' => $this->identityClass, |
292
|
17 |
|
'enableAutoLogin' => true, |
293
|
17 |
|
'enableSession' => true, |
294
|
17 |
|
'identityCookie' => ['name' => $this->identityCookieName, 'httpOnly' => true], |
295
|
17 |
|
'idParam' => $this->webUserParamId, |
296
|
17 |
|
]; |
297
|
17 |
|
$userConfig = ArrayHelper::merge($userConfig, $this->webUser); |
298
|
|
|
|
299
|
17 |
|
Yii::$app->set('user', $userConfig); |
300
|
17 |
|
} |
301
|
17 |
|
return $status; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Update controllers map to add console commands |
306
|
|
|
* @param ConsoleApplication $app |
307
|
|
|
* @since 1.0.0 |
308
|
|
|
*/ |
309
|
36 |
|
protected function mapConsoleControllers(ConsoleApplication $app) |
310
|
|
|
{ |
311
|
36 |
|
$app->controllerMap['oauth2:client'] = [ |
312
|
36 |
|
'class' => 'sweelix\oauth2\server\commands\ClientController', |
313
|
|
|
]; |
314
|
36 |
|
$app->controllerMap['oauth2:scope'] = [ |
315
|
36 |
|
'class' => 'sweelix\oauth2\server\commands\ScopeController', |
316
|
|
|
]; |
317
|
36 |
|
$app->controllerMap['oauth2:key'] = [ |
318
|
36 |
|
'class' => 'sweelix\oauth2\server\commands\KeyController', |
319
|
|
|
]; |
320
|
|
|
|
321
|
36 |
|
} |
322
|
|
|
} |
323
|
|
|
|