Oauth2DebugConfigAction   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 278
Duplicated Lines 0 %

Test Coverage

Coverage 98.45%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 32
eloc 190
c 2
b 0
f 0
dl 0
loc 278
rs 9.84
ccs 191
cts 194
cp 0.9845

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 22 1
F getConfiguration() 0 101 22
C getEndpoints() 0 135 9
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
        $userAccountCreationUrl = $module->userAccountCreationUrl
84 3
            ? VarDumper::export($module->userAccountCreationUrl)
85 3
            : '';
86
87 3
        $httpClientErrorsLogLevel = $module->getElaboratedHttpClientErrorsLogLevel();
88
89 3
        return [
90 3
            'serverRole' => $module->serverRole . ' (' . implode(', ', $serverRoles) . ')',
91
92 3
            'privateKey' => $privateKey,
93 3
            'privateKeyPassphrase' => $privateKeyPassphrase,
94 3
            'publicKey' => $publicKey,
95 3
            'codesEncryptionKey' => $codesEncryptionKey,
96 3
            'storageEncryptionKeys' => $storageEncryptionKeys,
97 3
            'defaultStorageEncryptionKey' => $module->defaultStorageEncryptionKey,
98
99 3
            'nonTlsAllowedRanges' => $module->nonTlsAllowedRanges,
100
101 3
            'clientRedirectUrisEnvVarConfig' => $clientRedirectUrisEnvVarConfig,
102
103 3
            'userAccountCreationUrl' => $userAccountCreationUrl,
104
105 3
            'identityClass' => $module->identityClass,
106
107 3
            'enableTokenRevocation' => $module->enableTokenRevocation ? 'true' : 'false',
108
109 3
            'urlRulesPrefix' => $module->urlRulesPrefix,
110 3
            'authorizePath' => $module->authorizePath,
111 3
            'accessTokenPath' => $module->accessTokenPath,
112 3
            'tokenRevocationPath' => $module->tokenRevocationPath,
113 3
            'jwksPath' => $module->jwksPath,
114 3
            'clientAuthorizationUrl' => $module->clientAuthorizationUrl,
115 3
            'clientAuthorizationPath' => $module->clientAuthorizationPath,
116 3
            'clientAuthorizationView' => $module->clientAuthorizationView,
117 3
            'openIdConnectUserinfoPath' => $module->openIdConnectUserinfoPath,
118 3
            'openIdConnectRpInitiatedLogoutPath' => $module->openIdConnectRpInitiatedLogoutPath,
119 3
            'openIdConnectLogoutConfirmationUrl' => $module->openIdConnectLogoutConfirmationUrl,
120 3
            'openIdConnectLogoutConfirmationPath' => $module->openIdConnectLogoutConfirmationPath,
121 3
            'openIdConnectLogoutConfirmationView' => $module->openIdConnectLogoutConfirmationView,
122
123 3
            'exceptionOnInvalidScope' => $module->exceptionOnInvalidScope ? 'true' : 'false',
124
125 3
            'grantTypes' => $grantTypes,
126
127 3
            'defaultAccessTokenTTL' => $defaultAccessTokenTTL,
128 3
            'resourceServerAccessTokenRevocationValidation' => $module->resourceServerAccessTokenRevocationValidation,
129
130 3
            'enableOpenIdConnect' => $module->enableOpenIdConnect ? 'true' : 'false',
131 3
            'enableOpenIdConnectDiscovery' => $module->enableOpenIdConnectDiscovery ? 'true' : 'false',
132 3
            'openIdConnectProviderConfigurationInformationPath' =>
133 3
                $module->openIdConnectProviderConfigurationInformationPath,
134 3
            'openIdConnectDiscoveryIncludeSupportedGrantTypes' =>
135 3
                $module->openIdConnectDiscoveryIncludeSupportedGrantTypes ? 'true' : 'false',
136 3
            'openIdConnectUserinfoEndpoint' => $module->openIdConnectUserinfoEndpoint ? 'true' : 'false',
137 3
            'openIdConnectRpInitiatedLogoutEndpoint' =>
138 3
                $module->openIdConnectRpInitiatedLogoutEndpoint ? 'true' : 'false',
139 3
            'openIdConnectAllowAnonymousRpInitiatedLogout' =>
140 3
                $module->openIdConnectAllowAnonymousRpInitiatedLogout ? 'true' : 'false',
141 3
            'openIdConnectDiscoveryServiceDocumentationUrl' => $module->openIdConnectDiscoveryServiceDocumentationUrl,
142 3
            'openIdConnectIssueRefreshTokenWithoutOfflineAccessScope' =>
143 3
                $module->openIdConnectIssueRefreshTokenWithoutOfflineAccessScope ? 'true' : 'false',
144
145 3
            'defaultUserAccountSelection' =>
146 3
                Oauth2Module::USER_ACCOUNT_SELECTION_NAMES[$module->defaultUserAccountSelection],
147
148 3
            'displayConfidentialExceptionMessages' => $module->displayConfidentialExceptionMessages === null
149 3
                ? 'null'
150 3
                : ($module->displayConfidentialExceptionMessages ? 'true' : 'false'),
151
152 3
            'httpClientErrorsLogLevel' => $httpClientErrorsLogLevel === 0
153
                ? 'disabled'
154 3
                : Logger::getLevelName($httpClientErrorsLogLevel),
155 3
        ];
156
    }
157
158
    /**
159
     * @param Oauth2Module $module
160
     * @return array
161
     */
162 10
    protected function getEndpoints($module)
163
    {
164 10
        if ($module->serverRole & Oauth2Module::SERVER_ROLE_AUTHORIZATION_SERVER) {
165 9
            $authorizeClientValue = $module->urlRulesPrefix . '/' . $module->authorizePath;
166 9
            $authorizeClientSettings = 'urlRulesPrefix, authorizePath';
167
168 9
            $accessTokenValue = $module->urlRulesPrefix . '/' . $module->accessTokenPath;
169 9
            $accessTokenSettings = 'urlRulesPrefix, accessTokenPath';
170
171 9
            if ($module->enableTokenRevocation) {
172 8
                $tokenRevocationValue = $module->urlRulesPrefix . '/' . $module->tokenRevocationPath;
173 8
                $tokenRevocationSettings  = 'urlRulesPrefix, tokenRevocationPath';
174
            } else {
175 1
                $tokenRevocationValue = '[Token Revocation is disabled]';
176 1
                $tokenRevocationSettings  = 'enableTokenRevocation';
177
            }
178
179 9
            $jwksValue = $module->urlRulesPrefix . '/' . $module->jwksPath;
180 9
            $jwksSettings = 'urlRulesPrefix, jwksPath';
181
182 9
            $clientAuthorizationValue = $module->urlRulesPrefix . '/' . $module->clientAuthorizationPath;
183 9
            $clientAuthorizationSettings = 'urlRulesPrefix, clientAuthorizationPath';
184
185 9
            if ($module->enableOpenIdConnect) {
186 8
                if ($module->enableOpenIdConnectDiscovery) {
187 7
                    $oidcProviderConfigInfoValue = $module->openIdConnectProviderConfigurationInformationPath;
188 7
                    $oidcProviderConfigInfoSettings = 'openIdConnectProviderConfigurationInformationPath';
189
                } else {
190 1
                    $oidcProviderConfigInfoValue = '[OpenId Connect Discovery is disabled]';
191 1
                    $oidcProviderConfigInfoSettings = 'enableOpenIdConnectDiscovery';
192
                }
193
194 8
                if (!empty($module->openIdConnectUserinfoEndpoint)) {
195 7
                    if ($module->openIdConnectUserinfoEndpoint === true) {
196 6
                        $oidcUserinfoValue = $module->urlRulesPrefix . '/' . $module->openIdConnectUserinfoPath;
197 6
                        $oidcUserinfoSettings = 'urlRulesPrefix, openIdConnectUserinfoPath';
198
                    } else {
199 1
                        $oidcUserinfoValue = $module->openIdConnectUserinfoEndpoint;
200 7
                        $oidcUserinfoSettings = 'openIdConnectUserinfoEndpoint';
201
                    }
202
                } else {
203 1
                    $oidcUserinfoValue = '[Userinfo Endpoint is disabled]';
204 1
                    $oidcUserinfoSettings = 'openIdConnectUserinfoEndpoint';
205
                }
206
207 8
                if (!empty($module->openIdConnectRpInitiatedLogoutEndpoint)) {
208 8
                    if ($module->openIdConnectRpInitiatedLogoutEndpoint === true) {
209 7
                        $oidcRpInitiatedLogoutValue = $module->urlRulesPrefix
210 7
                            . '/' . $module->openIdConnectRpInitiatedLogoutPath;
211 7
                        $oidcRpInitiatedLogoutSettings = 'urlRulesPrefix, openIdConnectRpInitiatedLogoutPath';
212
                    } else {
213 1
                        $oidcRpInitiatedLogoutValue = $module->openIdConnectRpInitiatedLogoutEndpoint;
214 8
                        $oidcRpInitiatedLogoutSettings = 'openIdConnectRpInitiatedLogoutEndpoint';
215
                    }
216
                } else {
217
                    $oidcRpInitiatedLogoutValue = '[Rp Initiated Logout is disabled]';
218 8
                    $oidcRpInitiatedLogoutSettings = 'openIdConnectRpInitiatedLogoutEndpoint';
219
                }
220
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
221
            } else {
222 1
                $oidcProviderConfigInfoValue = '[OpenID Connect is disabled]';
223 1
                $oidcProviderConfigInfoSettings = 'enableOpenIdConnect';
224
225 1
                $oidcUserinfoValue = '[OpenID Connect is disabled]';
226 1
                $oidcUserinfoSettings = 'enableOpenIdConnect';
227
228 1
                $oidcRpInitiatedLogoutValue = '[OpenID Connect is disabled]';
229 9
                $oidcRpInitiatedLogoutSettings = 'enableOpenIdConnect';
230
            }
231
        } else {
232 1
            $authorizeClientValue = '[Only available for "authorization_server" role]';
233 1
            $authorizeClientSettings = 'serverRole';
234
235 1
            $accessTokenValue = '[Only available for "authorization_server" role]';
236 1
            $accessTokenSettings = 'serverRole';
237
238 1
            $tokenRevocationValue = '[Only available for "authorization_server" role]';
239 1
            $tokenRevocationSettings  = 'serverRole';
240
241 1
            $jwksValue = '[Only available for "authorization_server" role]';
242 1
            $jwksSettings = 'serverRole';
243
244 1
            $clientAuthorizationValue = '[Only available for "authorization_server" role]';
245 1
            $clientAuthorizationSettings = 'serverRole';
246
247 1
            $oidcProviderConfigInfoValue = '[Only available for "authorization_server" role]';
248 1
            $oidcProviderConfigInfoSettings = 'serverRole';
249
250 1
            $oidcUserinfoValue = '[Only available for "authorization_server" role]';
251 1
            $oidcUserinfoSettings = 'serverRole';
252
253 1
            $oidcRpInitiatedLogoutValue = '[Only available for "authorization_server" role]';
254 1
            $oidcRpInitiatedLogoutSettings = 'serverRole';
255
        }
256
257 10
        return [
258 10
            'authorizeClient' => [
259 10
                'Authorize Client',
260 10
                $authorizeClientValue,
261 10
                $authorizeClientSettings,
262 10
            ],
263 10
            'accessToken' => [
264 10
                'Access Token',
265 10
                $accessTokenValue,
266 10
                $accessTokenSettings,
267 10
            ],
268 10
            'tokenRevocation' => [
269 10
                'Token Revocation',
270 10
                $tokenRevocationValue,
271 10
                $tokenRevocationSettings,
272 10
            ],
273 10
            'jwks' => [
274 10
                'JSON Web Key Sets',
275 10
                $jwksValue,
276 10
                $jwksSettings,
277 10
            ],
278 10
            'clientAuthorization' => [
279 10
                'Client Authorization',
280 10
                $clientAuthorizationValue,
281 10
                $clientAuthorizationSettings,
282 10
            ],
283 10
            'oidcProviderConfigInfo' => [
284 10
                'OpenID Connect Provider Configuration Information',
285 10
                $oidcProviderConfigInfoValue,
286 10
                $oidcProviderConfigInfoSettings,
287 10
            ],
288 10
            'oidcUserinfo' => [
289 10
                'OpenId Connect Userinfo',
290 10
                $oidcUserinfoValue,
291 10
                $oidcUserinfoSettings,
292 10
            ],
293 10
            'oidcRpInitiatedLogout' => [
294 10
                'OpenId Connect Rp Initiated Logout',
295 10
                $oidcRpInitiatedLogoutValue,
296 10
                $oidcRpInitiatedLogoutSettings,
297 10
            ],
298 10
        ];
299
    }
300
}
301