Completed
Push — devel ( 335712...856bfb )
by Philippe
04:48
created

Module::mapConsoleControllers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

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