Completed
Push — devel ( 7508db...540322 )
by Philippe
03:02
created

Module::setUpDi()   F

Complexity

Conditions 13
Paths 2048

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 41.985

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 38
ccs 16
cts 36
cp 0.4444
rs 2.7716
cc 13
eloc 24
nc 2048
nop 1
crap 41.985

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 XXX
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 XXX
31
 * @link http://www.sweelix.net
32
 * @package sweelix\oauth2\server
33
 * @since XXX
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
     * This user class will be used to link oauth2 authorization system with the application.
48
     * The class must implement \sweelix\oauth2\server\interfaces\UserInterface
49
     * If not defined, the Yii::$app->user->identityClass value will be used
50
     * @var string|array user class definition.
51
     */
52
    public $identityClass;
53
54
    /**
55
     * @var string change base end point
56
     */
57
    public $baseEndPoint = '';
58
59
    /**
60
     * @var bool configure oauth server (use_jwt_access_tokens)
61
     */
62
    public $allowJwtAccesToken = false;
63
64
    /**
65
     * @var array configure oauth server (allowed_algorithms)
66
     */
67
    public $allowAlgorithm = ['RS256', 'RS384', 'RS512'];
68
69
    /**
70
     * @var string|array jwt audience. Default to token endpoint
71
     */
72
    public $jwtAudience = ['token/index'];
73
74
    /**
75
     * @var bool configure oauth server (store_encrypted_token_string)
76
     */
77
    public $storeEncryptedTokenString = true;
78
79
    /**
80
     * @var bool configure oauth server (use_openid_connect)
81
     */
82
    public $allowOpenIdConnect = false;
83
84
    /**
85
     * @var int configure oauth server (id_lifetime)
86
     */
87
    public $idTTL = 3600;
88
89
    /**
90
     * @var int configure oauth server (access_lifetime)
91
     */
92
    public $accessTokenTTL = 3600;
93
94
    /**
95
     * @var int configure oauth server (refresh_token_lifetime)
96
     */
97
    public $refreshTokenTTL = 1209600;
98
99
    /**
100
     * @var string configure oauth server (www_realm)
101
     */
102
    public $realm = 'Service';
103
104
    /**
105
     * @var string configure oauth server (token_param_name)
106
     */
107
    public $tokenQueryName = 'access_token';
108
109
    /**
110
     * @var string configure oauth server (token_bearer_header_name)
111
     */
112
    public $tokenBearerName = 'Bearer';
113
114
    /**
115
     * @var bool configure oauth server (enforce_state)
116
     */
117
    public $enforceState = true;
118
119
    /**
120
     * @var bool configure oauth server (require_exact_redirect_uri)
121
     */
122
    public $allowOnlyRedirectUri = true;
123
124
    /**
125
     * @var bool configure oauth server (allow_implicit)
126
     */
127
    public $allowImplicit = false;
128
129
    /**
130
     * @var bool allow authorization code grant
131
     */
132
    public $allowAuthorizationCode = true;
133
134
    /**
135
     * @var bool allow client credentials grant
136
     */
137
    public $allowClientCredentials = true;
138
139
    /**
140
     * @var bool allow password grant
141
     */
142
    public $allowPassword = true;
143
144
    /**
145
     * @var bool configure oauth server (allow_credentials_in_request_body)
146
     */
147
    public $allowCredentialsInRequestBody = true;
148
149
    /**
150
     * @var bool configure oauth server (allow_public_clients)
151
     */
152
    public $allowPublicClients = true;
153
154
    /**
155
     * @var bool configure oauth server (always_issue_new_refresh_token)
156
     */
157
    public $alwaysIssueNewRefreshToken = true;
158
159
    /**
160
     * @var bool configure oauth server (unset_refresh_token_after_use)
161
     */
162
    public $unsetRefreshTokenAfterUse = false;
163
164
    /**
165
     * @var int duration of login time for multiple authorize calls
166
     */
167
    public $loginDuration = 60 * 60 * 24 * 30;
168
    /**
169
     * @inheritdoc
170
     */
171 45
    public function init()
172
    {
173 45
        parent::init();
174 45
    }
175
176
    /**
177
     * Load dataservices in container
178
     * @param \yii\base\Application $app
179
     * @since XXX
180
     */
181 45
    protected function setUpDi($app)
182
    {
183 45
        if (Yii::$container->has('scope') === false) {
184
            Yii::$container->set('scope', 'sweelix\oauth2\server\validators\ScopeValidator');
185
        }
186 45
        if (Yii::$container->has('sweelix\oauth2\server\interfaces\AccessTokenModelInterface') === false) {
187
            Yii::$container->set('sweelix\oauth2\server\interfaces\AccessTokenModelInterface', 'sweelix\oauth2\server\models\AccessToken');
188
        }
189 45
        if (Yii::$container->has('sweelix\oauth2\server\interfaces\AuthCodeModelInterface') === false) {
190
            Yii::$container->set('sweelix\oauth2\server\interfaces\AuthCodeModelInterface', 'sweelix\oauth2\server\models\AuthCode');
191
        }
192 45
        if (Yii::$container->has('sweelix\oauth2\server\interfaces\ClientModelInterface') === false) {
193
            Yii::$container->set('sweelix\oauth2\server\interfaces\ClientModelInterface', 'sweelix\oauth2\server\models\Client');
194
        }
195 45
        if (Yii::$container->has('sweelix\oauth2\server\interfaces\CypherKeyModelInterface') === false) {
196
            Yii::$container->set('sweelix\oauth2\server\interfaces\CypherKeyModelInterface', 'sweelix\oauth2\server\models\CypherKey');
197
        }
198 45
        if (Yii::$container->has('sweelix\oauth2\server\interfaces\JtiModelInterface') === false) {
199
            Yii::$container->set('sweelix\oauth2\server\interfaces\JtiModelInterface', 'sweelix\oauth2\server\models\Jti');
200
        }
201 45
        if (Yii::$container->has('sweelix\oauth2\server\interfaces\JwtModelInterface') === false) {
202
            Yii::$container->set('sweelix\oauth2\server\interfaces\JwtModelInterface', 'sweelix\oauth2\server\models\Jwt');
203
        }
204 45
        if (Yii::$container->has('sweelix\oauth2\server\interfaces\RefreshTokenModelInterface') === false) {
205
            Yii::$container->set('sweelix\oauth2\server\interfaces\RefreshTokenModelInterface', 'sweelix\oauth2\server\models\RefreshToken');
206
        }
207 45
        if (Yii::$container->has('sweelix\oauth2\server\interfaces\ScopeModelInterface') === false) {
208
            Yii::$container->set('sweelix\oauth2\server\interfaces\ScopeModelInterface', 'sweelix\oauth2\server\models\Scope');
209
        }
210 45
        if ((Yii::$container->has('sweelix\oauth2\server\interfaces\UserModelInterface') === false) && ($this->identityClass !== null)) {
211
            Yii::$container->set('sweelix\oauth2\server\interfaces\UserModelInterface', $this->identityClass);
212
        }
213 45
        if ($this->backend === 'redis') {
214 45
            Redis::register($app);
215 45
        }
216 45
        Oauth::register($app);
217
218 45
    }
219
220
    /**
221
     * @inheritdoc
222
     */
223 45
    public function bootstrap($app)
224
    {
225
        // use the registered identity class if not overloaded
226 45
        if (($this->identityClass === null) && (isset($app->user) === true)) {
227 11
            $this->identityClass = $app->user->identityClass;
228 11
        }
229 45
        $this->setUpDi($app);
230 45
        if (empty($this->baseEndPoint) === false) {
231
            $this->baseEndPoint = trim($this->baseEndPoint, '/').'/';
232
        }
233
234 45
        if ($app instanceof ConsoleApplication) {
235 34
            $this->mapConsoleControllers($app);
236 34
        } else {
237 11
            $app->getUrlManager()->addRules([
238 11
                ['verb' => 'POST', 'pattern' => $this->baseEndPoint.'token', 'route' => $this->id.'/token/index'],
239 11
                ['verb' => 'GET', 'pattern' => $this->baseEndPoint.'authorize', 'route' => $this->id.'/authorize/index'],
240 11
                ['pattern' => $this->baseEndPoint.'authorize-login', 'route' => $this->id.'/authorize/login'],
241 11
                ['pattern' => $this->baseEndPoint.'authorize-application', 'route' => $this->id.'/authorize/authorize'],
242 11
                ['pattern' => $this->baseEndPoint.'authorize-error', 'route' => $this->id.'/authorize/error'],
243 11
            ]);
244
        }
245 45
    }
246
247
    /**
248
     * Update controllers map to add console commands
249
     * @param ConsoleApplication $app
250
     * @since XXX
251
     */
252 34
    protected function mapConsoleControllers(ConsoleApplication $app)
253
    {
254 34
        $app->controllerMap['oauth2:client'] = [
255 34
            'class' => 'sweelix\oauth2\server\commands\ClientController',
256
        ];
257 34
        $app->controllerMap['oauth2:scope'] = [
258 34
            'class' => 'sweelix\oauth2\server\commands\ScopeController',
259
        ];
260 34
        $app->controllerMap['oauth2:key'] = [
261 34
            'class' => 'sweelix\oauth2\server\commands\KeyController',
262
        ];
263
264 34
    }
265
}
266