Skip to content

Commit 5ae10b4

Browse files
committed
no message
1 parent 6274f60 commit 5ae10b4

16 files changed

+310
-237
lines changed

Bootstrap.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace filsh\yii2\oauth2server;
4+
5+
use yii\web\GroupUrlRule;
6+
7+
class Bootstrap implements \yii\base\BootstrapInterface
8+
{
9+
/**
10+
* @var array Model's map
11+
*/
12+
private $_modelMap = [
13+
'OauthClients' => 'filsh\yii2\oauth2server\models\OauthClients',
14+
'OauthAccessTokens' => 'filsh\yii2\oauth2server\models\OauthAccessTokens',
15+
'OauthAuthorizationCodes' => 'filsh\yii2\oauth2server\models\OauthAuthorizationCodes',
16+
'OauthRefreshTokens' => 'filsh\yii2\oauth2server\models\OauthRefreshTokens',
17+
'OauthScopes' => 'filsh\yii2\oauth2server\models\OauthScopes',
18+
];
19+
20+
/**
21+
* @var array Storage's map
22+
*/
23+
private $_storageMap = [
24+
'access_token' => 'filsh\yii2\oauth2server\storage\Pdo',
25+
'authorization_code' => 'filsh\yii2\oauth2server\storage\Pdo',
26+
'client_credentials' => 'filsh\yii2\oauth2server\storage\Pdo',
27+
'client' => 'filsh\yii2\oauth2server\storage\Pdo',
28+
'refresh_token' => 'filsh\yii2\oauth2server\storage\Pdo',
29+
'user_credentials' => 'filsh\yii2\oauth2server\storage\Pdo',
30+
'public_key' => 'filsh\yii2\oauth2server\storage\Pdo',
31+
'jwt_bearer' => 'filsh\yii2\oauth2server\storage\Pdo',
32+
'scope' => 'filsh\yii2\oauth2server\storage\Pdo',
33+
];
34+
35+
/**
36+
* @inheritdoc
37+
*/
38+
public function bootstrap($app)
39+
{
40+
/** @var $module Module */
41+
if ($app->hasModule('oauth2') && ($module = $app->getModule('oauth2')) instanceof Module) {
42+
$this->_modelMap = array_merge($this->_modelMap, $module->modelMap);
43+
foreach ($this->_modelMap as $name => $definition) {
44+
\Yii::$container->set("filsh\\yii2\\oauth2server\\models\\" . $name, $definition);
45+
$module->modelMap[$name] = is_array($definition) ? $definition['class'] : $definition;
46+
}
47+
48+
$this->_storageMap = array_merge($this->_storageMap, $module->storageMap);
49+
foreach ($this->_storageMap as $name => $definition) {
50+
\Yii::$container->set($name, $definition);
51+
$module->storageMap[$name] = is_array($definition) ? $definition['class'] : $definition;
52+
}
53+
54+
if ($app instanceof \yii\console\Application) {
55+
$module->controllerNamespace = 'filsh\yii2\oauth2server\commands';
56+
}
57+
}
58+
}
59+
}

Module.php

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44

55
use \Yii;
66
use yii\i18n\PhpMessageSource;
7-
use \array_key_exists;
87
use yii\helpers\ArrayHelper;
98

109
/**
1110
* For example,
12-
*
11+
*
1312
* ```php
1413
* 'oauth2' => [
1514
* 'class' => 'filsh\yii2\oauth2server\Module',
@@ -32,7 +31,7 @@
3231
*/
3332
class Module extends \yii\base\Module
3433
{
35-
const VERSION = '2.0.2';
34+
const VERSION = '2.0.3';
3635

3736
/**
3837
* @var array Model's map
@@ -58,10 +57,6 @@ class Module extends \yii\base\Module
5857
* @var type max access lifetime
5958
*/
6059
public $tokenAccessLifetime;
61-
/**
62-
* @var whether to use JWT tokens
63-
*/
64-
public $useJwtToken = false;//ADDED
6560

6661
/**
6762
* @inheritdoc
@@ -74,29 +69,14 @@ public function init()
7469

7570
/**
7671
* Gets Oauth2 Server
77-
*
72+
*
7873
* @return \filsh\yii2\oauth2server\Server
7974
* @throws \yii\base\InvalidConfigException
8075
*/
8176
public function getServer()
8277
{
8378
if(!$this->has('server')) {
8479
$storages = [];
85-
86-
if($this->useJwtToken)
87-
{
88-
if(!array_key_exists('access_token', $this->storageMap) || !array_key_exists('public_key', $this->storageMap)) {
89-
throw new \yii\base\InvalidConfigException('access_token and public_key must be set or set useJwtToken to false');
90-
}
91-
//define dependencies when JWT is used instead of normal token
92-
\Yii::$container->clear('public_key'); //remove old definition
93-
\Yii::$container->set('public_key', $this->storageMap['public_key']);
94-
\Yii::$container->set('OAuth2\Storage\PublicKeyInterface', $this->storageMap['public_key']);
95-
96-
\Yii::$container->clear('access_token'); //remove old definition
97-
\Yii::$container->set('access_token', $this->storageMap['access_token']);
98-
}
99-
10080
foreach(array_keys($this->storageMap) as $name) {
10181
$storages[$name] = \Yii::$container->get($name);
10282
}
@@ -106,13 +86,13 @@ public function getServer()
10686
if(!isset($storages[$name]) || empty($options['class'])) {
10787
throw new \yii\base\InvalidConfigException('Invalid grant types configuration.');
10888
}
109-
89+
11090
$class = $options['class'];
11191
unset($options['class']);
112-
92+
11393
$reflection = new \ReflectionClass($class);
11494
$config = array_merge([0 => $storages[$name]], [$options]);
115-
95+
11696
$instance = $reflection->newInstanceArgs($config);
11797
$grantTypes[$name] = $instance;
11898
}
@@ -121,14 +101,13 @@ public function getServer()
121101
$this,
122102
$storages,
123103
[
124-
'use_jwt_access_tokens' => $this->useJwtToken,//ADDED
125104
'token_param_name' => $this->tokenParamName,
126105
'access_lifetime' => $this->tokenAccessLifetime,
127106
/** add more ... */
128107
],
129108
$grantTypes
130109
]);
131-
110+
132111
$this->set('server', $server);
133112
}
134113
return $this->get('server');
@@ -149,10 +128,10 @@ public function getResponse()
149128
}
150129
return $this->get('response');
151130
}
152-
131+
153132
/**
154133
* Register translations for this module
155-
*
134+
*
156135
* @return array
157136
*/
158137
public function registerTranslations()
@@ -167,7 +146,7 @@ public function registerTranslations()
167146

168147
/**
169148
* Translate module message
170-
*
149+
*
171150
* @param string $category
172151
* @param string $message
173152
* @param array $params
@@ -178,4 +157,4 @@ public static function t($category, $message, $params = [], $language = null)
178157
{
179158
return Yii::t('modules/oauth2/' . $category, $message, $params, $language);
180159
}
181-
}
160+
}

0 commit comments

Comments
 (0)