Passed
Push — master ( 305862...fff288 )
by Rutger
11:51 queued 08:42
created

Oauth2DebugConfigAction::getEndpoints()   B

Complexity

Conditions 7
Paths 15

Size

Total Lines 86
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 59
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 63
c 1
b 0
f 0
dl 0
loc 86
ccs 59
cts 59
cp 1
rs 7.8739
cc 7
nc 15
nop 1
crap 7

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