Passed
Push — master ( a721e7...5904cd )
by Rutger
13:40
created

Oauth2DebugConfigAction::getEndpoints()   C

Complexity

Conditions 9
Paths 39

Size

Total Lines 135
Code Lines 100

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 99
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 100
c 1
b 0
f 0
dl 0
loc 135
ccs 99
cts 100
cp 0.99
rs 6.4444
cc 9
nc 39
nop 1
crap 9

How to fix   Long Method   

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
namespace rhertogh\Yii2Oauth2Server\controllers\console\debug;
4
5
use League\OAuth2\Server\Grant\GrantTypeInterface;
6
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2DebugController;
7
use rhertogh\Yii2Oauth2Server\helpers\DateIntervalHelper;
8
use rhertogh\Yii2Oauth2Server\interfaces\controllers\console\debug\Oauth2DebugConfigActionInterface;
9
use rhertogh\Yii2Oauth2Server\Oauth2Module;
10
use yii\base\Action;
11
use yii\console\ExitCode;
12
use yii\console\widgets\Table;
13
use yii\helpers\VarDumper;
14
use yii\log\Logger;
15
16
/**
17
 * @property Oauth2DebugController $controller
18
 */
19
class Oauth2DebugConfigAction extends Action implements Oauth2DebugConfigActionInterface
20
{
21
    /**
22
     * Show Oauth2 Server configuration.
23
     *
24
     * @throws \Throwable
25
     */
26 1
    public function run()
27
    {
28 1
        $module = $this->controller->module;
29
30 1
        $configuration = $this->getConfiguration($module);
31
32 1
        $this->controller->stdout('Configuration:' . PHP_EOL);
33 1
        $this->controller->stdout(Table::widget([
34 1
            'headers' => ['Setting', 'Value'],
35 1
            'rows' => array_map(fn($setting) => [$setting, $configuration[$setting]], array_keys($configuration)),
36 1
        ]));
37
38 1
        $endpoints = $this->getEndpoints($module);
39
40 1
        $this->controller->stdout(PHP_EOL);
41 1
        $this->controller->stdout('Endpoints:' . PHP_EOL);
42 1
        $this->controller->stdout(Table::widget([
43 1
            'headers' => ['Endpoint', 'URL', 'Setting(s)'],
44 1
            'rows' => $endpoints,
45 1
        ]));
46
47 1
        return ExitCode::OK;
48
    }
49
50
    /**
51
     * @param Oauth2Module $module
52
     * @return array
53
     */
54 3
    protected function getConfiguration($module)
55
    {
56 3
        $serverRoles = [];
57 3
        if ($module->serverRole & Oauth2Module::SERVER_ROLE_AUTHORIZATION_SERVER) {
58 2
            $serverRoles[] = 'Authorization Server';
59 2
            $grantTypes = array_values(array_map(
60 2
                fn(GrantTypeInterface $grant) => $grant->getIdentifier(),
61 2
                $module->getAuthorizationServer()->getEnabledGrantTypes()
62 2
            ));
63 2
            $defaultAccessTokenTTL = DateIntervalHelper::toString($module->getDefaultAccessTokenTTL()) ?? '[NOT SET]';
64
        } else {
65 1
            $grantTypes = '-';
66 1
            $defaultAccessTokenTTL = '-';
67
        }
68
69 3
        if ($module->serverRole & Oauth2Module::SERVER_ROLE_RESOURCE_SERVER) {
70 3
            $serverRoles[] = 'Resource Server';
71
        }
72
73 3
        $privateKey = $module->privateKey ? '[SET]' : '[NOT SET]';
74 3
        $privateKeyPassphrase = $module->privateKeyPassphrase ? '[SET]' : '[NOT SET]';
75 3
        $publicKey = $module->publicKey ? '[SET]' : '[NOT SET]';
76 3
        $codesEncryptionKey = $module->codesEncryptionKey ? '[SET]' : '[NOT SET]';
77 3
        $storageEncryptionKeys = $module->storageEncryptionKeys ? '[SET]' : '[NOT SET]';
78
79 3
        $clientRedirectUrisEnvVarConfig = $module->clientRedirectUrisEnvVarConfig
80
            ? VarDumper::export($module->clientRedirectUrisEnvVarConfig)
81 3
            : '';
82
83 3
        $httpClientErrorsLogLevel = $module->getElaboratedHttpClientErrorsLogLevel();
84
85 3
        return [
86 3
            'serverRole' => $module->serverRole . ' (' . implode(', ', $serverRoles) . ')',
87
88 3
            'privateKey' => $privateKey,
89 3
            'privateKeyPassphrase' => $privateKeyPassphrase,
90 3
            'publicKey' => $publicKey,
91 3
            'codesEncryptionKey' => $codesEncryptionKey,
92 3
            'storageEncryptionKeys' => $storageEncryptionKeys,
93 3
            'defaultStorageEncryptionKey' => $module->defaultStorageEncryptionKey,
94
95 3
            'nonTlsAllowedRanges' => $module->nonTlsAllowedRanges,
96
97 3
            'clientRedirectUrisEnvVarConfig' => $clientRedirectUrisEnvVarConfig,
98
99 3
            'identityClass' => $module->identityClass,
100
101 3
            'enableTokenRevocation' => $module->enableTokenRevocation ? 'true' : 'false',
102
103 3
            'urlRulesPrefix' => $module->urlRulesPrefix,
104 3
            'authorizePath' => $module->authorizePath,
105 3
            'accessTokenPath' => $module->accessTokenPath,
106 3
            'tokenRevocationPath' => $module->tokenRevocationPath,
107 3
            'jwksPath' => $module->jwksPath,
108 3
            'clientAuthorizationUrl' => $module->clientAuthorizationUrl,
109 3
            'clientAuthorizationPath' => $module->clientAuthorizationPath,
110 3
            'clientAuthorizationView' => $module->clientAuthorizationView,
111 3
            'openIdConnectUserinfoPath' => $module->openIdConnectUserinfoPath,
112 3
            'openIdConnectRpInitiatedLogoutPath' => $module->openIdConnectRpInitiatedLogoutPath,
113 3
            'openIdConnectLogoutConfirmationUrl' => $module->openIdConnectLogoutConfirmationUrl,
114 3
            'openIdConnectLogoutConfirmationPath' => $module->openIdConnectLogoutConfirmationPath,
115 3
            'openIdConnectLogoutConfirmationView' => $module->openIdConnectLogoutConfirmationView,
116
117 3
            'exceptionOnInvalidScope' => $module->exceptionOnInvalidScope ? 'true' : 'false',
118
119 3
            'grantTypes' => $grantTypes,
120
121 3
            'defaultAccessTokenTTL' => $defaultAccessTokenTTL,
122 3
            'resourceServerAccessTokenRevocationValidation' => $module->resourceServerAccessTokenRevocationValidation,
123
124 3
            'enableOpenIdConnect' => $module->enableOpenIdConnect ? 'true' : 'false',
125 3
            'enableOpenIdConnectDiscovery' => $module->enableOpenIdConnectDiscovery ? 'true' : 'false',
126 3
            'openIdConnectProviderConfigurationInformationPath' =>
127 3
                $module->openIdConnectProviderConfigurationInformationPath,
128 3
            'openIdConnectDiscoveryIncludeSupportedGrantTypes' =>
129 3
                $module->openIdConnectDiscoveryIncludeSupportedGrantTypes ? 'true' : 'false',
130 3
            'openIdConnectUserinfoEndpoint' => $module->openIdConnectUserinfoEndpoint ? 'true' : 'false',
131 3
            'openIdConnectRpInitiatedLogoutEndpoint' =>
132 3
                $module->openIdConnectRpInitiatedLogoutEndpoint ? 'true' : 'false',
133 3
            'openIdConnectAllowAnonymousRpInitiatedLogout' =>
134 3
                $module->openIdConnectAllowAnonymousRpInitiatedLogout ? 'true' : 'false',
135 3
            'openIdConnectDiscoveryServiceDocumentationUrl' => $module->openIdConnectDiscoveryServiceDocumentationUrl,
136 3
            'openIdConnectIssueRefreshTokenWithoutOfflineAccessScope' =>
137 3
                $module->openIdConnectIssueRefreshTokenWithoutOfflineAccessScope ? 'true' : 'false',
138
139 3
            'defaultUserAccountSelection' =>
140 3
                Oauth2Module::USER_ACCOUNT_SELECTION_NAMES[$module->defaultUserAccountSelection],
141
142 3
            'displayConfidentialExceptionMessages' => $module->displayConfidentialExceptionMessages === null
143 3
                ? 'null'
144 3
                : ($module->displayConfidentialExceptionMessages ? 'true' : 'false'),
145
146 3
            'httpClientErrorsLogLevel' => $httpClientErrorsLogLevel === 0
147
                ? 'disabled'
148 3
                : Logger::getLevelName($httpClientErrorsLogLevel),
149 3
        ];
150
    }
151
152
    /**
153
     * @param Oauth2Module $module
154
     * @return array
155
     */
156 10
    protected function getEndpoints($module)
157
    {
158 10
        if ($module->serverRole & Oauth2Module::SERVER_ROLE_AUTHORIZATION_SERVER) {
159 9
            $authorizeClientValue = $module->urlRulesPrefix . '/' . $module->authorizePath;
160 9
            $authorizeClientSettings = 'urlRulesPrefix, authorizePath';
161
162 9
            $accessTokenValue = $module->urlRulesPrefix . '/' . $module->accessTokenPath;
163 9
            $accessTokenSettings = 'urlRulesPrefix, accessTokenPath';
164
165 9
            if ($module->enableTokenRevocation) {
166 8
                $tokenRevocationValue = $module->urlRulesPrefix . '/' . $module->tokenRevocationPath;
167 8
                $tokenRevocationSettings  = 'urlRulesPrefix, tokenRevocationPath';
168
            } else {
169 1
                $tokenRevocationValue = '[Token Revocation is disabled]';
170 1
                $tokenRevocationSettings  = 'enableTokenRevocation';
171
            }
172
173 9
            $jwksValue = $module->urlRulesPrefix . '/' . $module->jwksPath;
174 9
            $jwksSettings = 'urlRulesPrefix, jwksPath';
175
176 9
            $clientAuthorizationValue = $module->urlRulesPrefix . '/' . $module->clientAuthorizationPath;
177 9
            $clientAuthorizationSettings = 'urlRulesPrefix, clientAuthorizationPath';
178
179 9
            if ($module->enableOpenIdConnect) {
180 8
                if ($module->enableOpenIdConnectDiscovery) {
181 7
                    $oidcProviderConfigInfoValue = $module->openIdConnectProviderConfigurationInformationPath;
182 7
                    $oidcProviderConfigInfoSettings = 'openIdConnectProviderConfigurationInformationPath';
183
                } else {
184 1
                    $oidcProviderConfigInfoValue = '[OpenId Connect Discovery is disabled]';
185 1
                    $oidcProviderConfigInfoSettings = 'enableOpenIdConnectDiscovery';
186
                }
187
188 8
                if (!empty($module->openIdConnectUserinfoEndpoint)) {
189 7
                    if ($module->openIdConnectUserinfoEndpoint === true) {
190 6
                        $oidcUserinfoValue = $module->urlRulesPrefix . '/' . $module->openIdConnectUserinfoPath;
191 6
                        $oidcUserinfoSettings = 'urlRulesPrefix, openIdConnectUserinfoPath';
192
                    } else {
193 1
                        $oidcUserinfoValue = $module->openIdConnectUserinfoEndpoint;
194 7
                        $oidcUserinfoSettings = 'openIdConnectUserinfoEndpoint';
195
                    }
196
                } else {
197 1
                    $oidcUserinfoValue = '[Userinfo Endpoint is disabled]';
198 1
                    $oidcUserinfoSettings = 'openIdConnectUserinfoEndpoint';
199
                }
200
201 8
                if (!empty($module->openIdConnectRpInitiatedLogoutEndpoint)) {
202 8
                    if ($module->openIdConnectRpInitiatedLogoutEndpoint === true) {
203 7
                        $oidcRpInitiatedLogoutValue = $module->urlRulesPrefix
204 7
                            . '/' . $module->openIdConnectRpInitiatedLogoutPath;
205 7
                        $oidcRpInitiatedLogoutSettings = 'urlRulesPrefix, openIdConnectRpInitiatedLogoutPath';
206
                    } else {
207 1
                        $oidcRpInitiatedLogoutValue = $module->openIdConnectRpInitiatedLogoutEndpoint;
208 8
                        $oidcRpInitiatedLogoutSettings = 'openIdConnectRpInitiatedLogoutEndpoint';
209
                    }
210
                } else {
211
                    $oidcRpInitiatedLogoutValue = '[Rp Initiated Logout is disabled]';
212 8
                    $oidcRpInitiatedLogoutSettings = 'openIdConnectRpInitiatedLogoutEndpoint';
213
                }
214
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
215
            } else {
216 1
                $oidcProviderConfigInfoValue = '[OpenID Connect is disabled]';
217 1
                $oidcProviderConfigInfoSettings = 'enableOpenIdConnect';
218
219 1
                $oidcUserinfoValue = '[OpenID Connect is disabled]';
220 1
                $oidcUserinfoSettings = 'enableOpenIdConnect';
221
222 1
                $oidcRpInitiatedLogoutValue = '[OpenID Connect is disabled]';
223 9
                $oidcRpInitiatedLogoutSettings = 'enableOpenIdConnect';
224
            }
225
        } else {
226 1
            $authorizeClientValue = '[Only available for "authorization_server" role]';
227 1
            $authorizeClientSettings = 'serverRole';
228
229 1
            $accessTokenValue = '[Only available for "authorization_server" role]';
230 1
            $accessTokenSettings = 'serverRole';
231
232 1
            $tokenRevocationValue = '[Only available for "authorization_server" role]';
233 1
            $tokenRevocationSettings  = 'serverRole';
234
235 1
            $jwksValue = '[Only available for "authorization_server" role]';
236 1
            $jwksSettings = 'serverRole';
237
238 1
            $clientAuthorizationValue = '[Only available for "authorization_server" role]';
239 1
            $clientAuthorizationSettings = 'serverRole';
240
241 1
            $oidcProviderConfigInfoValue = '[Only available for "authorization_server" role]';
242 1
            $oidcProviderConfigInfoSettings = 'serverRole';
243
244 1
            $oidcUserinfoValue = '[Only available for "authorization_server" role]';
245 1
            $oidcUserinfoSettings = 'serverRole';
246
247 1
            $oidcRpInitiatedLogoutValue = '[Only available for "authorization_server" role]';
248 1
            $oidcRpInitiatedLogoutSettings = 'serverRole';
249
        }
250
251 10
        return [
252 10
            'authorizeClient' => [
253 10
                'Authorize Client',
254 10
                $authorizeClientValue,
255 10
                $authorizeClientSettings,
256 10
            ],
257 10
            'accessToken' => [
258 10
                'Access Token',
259 10
                $accessTokenValue,
260 10
                $accessTokenSettings,
261 10
            ],
262 10
            'tokenRevocation' => [
263 10
                'Token Revocation',
264 10
                $tokenRevocationValue,
265 10
                $tokenRevocationSettings,
266 10
            ],
267 10
            'jwks' => [
268 10
                'JSON Web Key Sets',
269 10
                $jwksValue,
270 10
                $jwksSettings,
271 10
            ],
272 10
            'clientAuthorization' => [
273 10
                'Client Authorization',
274 10
                $clientAuthorizationValue,
275 10
                $clientAuthorizationSettings,
276 10
            ],
277 10
            'oidcProviderConfigInfo' => [
278 10
                'OpenID Connect Provider Configuration Information',
279 10
                $oidcProviderConfigInfoValue,
280 10
                $oidcProviderConfigInfoSettings,
281 10
            ],
282 10
            'oidcUserinfo' => [
283 10
                'OpenId Connect Userinfo',
284 10
                $oidcUserinfoValue,
285 10
                $oidcUserinfoSettings,
286 10
            ],
287 10
            'oidcRpInitiatedLogout' => [
288 10
                'OpenId Connect Rp Initiated Logout',
289 10
                $oidcRpInitiatedLogoutValue,
290 10
                $oidcRpInitiatedLogoutSettings,
291 10
            ],
292 10
        ];
293
    }
294
}
295