Passed
Push — master ( 6e82fd...74b096 )
by Rutger
03:04
created

Oauth2DebugConfigAction   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Test Coverage

Coverage 98.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 26
eloc 132
c 1
b 0
f 0
dl 0
loc 197
ccs 131
cts 133
cp 0.985
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 22 1
F getConfiguration() 0 86 19
B getEndpoints() 0 74 6
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
            'urlRulesPrefix' => $module->urlRulesPrefix,
96 3
            'authorizePath' => $module->authorizePath,
97 3
            'accessTokenPath' => $module->accessTokenPath,
98 3
            'jwksPath' => $module->jwksPath,
99 3
            'clientAuthorizationUrl' => $module->clientAuthorizationUrl,
100 3
            'clientAuthorizationPath' => $module->clientAuthorizationPath,
101 3
            'clientAuthorizationView' => $module->clientAuthorizationView,
102 3
            'openIdConnectUserinfoPath' => $module->openIdConnectUserinfoPath,
103 3
            'openIdConnectRpInitiatedLogoutPath' => $module->openIdConnectRpInitiatedLogoutPath,
104
105 3
            'exceptionOnInvalidScope' => $module->exceptionOnInvalidScope,
106
107 3
            'grantTypes' => $grantTypes,
108
109 3
            'defaultAccessTokenTTL' => $defaultAccessTokenTTL,
110 3
            'resourceServerAccessTokenRevocationValidation' => $module->resourceServerAccessTokenRevocationValidation,
111
112 3
            'enableOpenIdConnect' => $module->enableOpenIdConnect ? 'true' : 'false',
113 3
            'enableOpenIdConnectDiscovery' => $module->enableOpenIdConnectDiscovery ? 'true' : 'false',
114 3
            'openIdConnectProviderConfigurationInformationPath' =>
115 3
                $module->openIdConnectProviderConfigurationInformationPath,
116 3
            'openIdConnectDiscoveryIncludeSupportedGrantTypes' =>
117 3
                $module->openIdConnectDiscoveryIncludeSupportedGrantTypes ? 'true' : 'false',
118 3
            'openIdConnectUserinfoEndpoint' => $module->openIdConnectUserinfoEndpoint ? 'true' : 'false',
119 3
            'openIdConnectRpInitiatedLogoutEndpoint' => $module->openIdConnectRpInitiatedLogoutEndpoint ? 'true' : 'false',
120 3
            'openIdConnectAllowAnonymousRpInitiatedLogout' => $module->openIdConnectAllowAnonymousRpInitiatedLogout ? 'true' : 'false',
121 3
            'openIdConnectDiscoveryServiceDocumentationUrl' => $module->openIdConnectDiscoveryServiceDocumentationUrl,
122 3
            'openIdConnectIssueRefreshTokenWithoutOfflineAccessScope' =>
123 3
                $module->openIdConnectIssueRefreshTokenWithoutOfflineAccessScope ? 'true' : 'false',
124
125 3
            'defaultUserAccountSelection' => $module->defaultUserAccountSelection,
126
127 3
            'displayConfidentialExceptionMessages' => $module->displayConfidentialExceptionMessages === null
128 3
                ? 'null'
129 3
                : ($module->displayConfidentialExceptionMessages ? 'true' : 'false'),
130
131 3
            'httpClientErrorsLogLevel' => $httpClientErrorsLogLevel === 0
132
                ? 'disabled'
133 3
                : Logger::getLevelName($httpClientErrorsLogLevel),
134 3
        ];
135
    }
136
137
    /**
138
     * @param Oauth2Module $module
139
     * @return array
140
     */
141 7
    protected function getEndpoints($module)
142
    {
143 7
        if ($module->serverRole & Oauth2Module::SERVER_ROLE_AUTHORIZATION_SERVER) {
144 6
            $authorizeClientValue = $module->urlRulesPrefix . '/' . $module->authorizePath;
145 6
            $authorizeClientSettings = 'urlRulesPrefix, authorizePath';
146
147 6
            $accessTokenValue = $module->urlRulesPrefix . '/' . $module->accessTokenPath;
148 6
            $accessTokenSettings = 'urlRulesPrefix, accessTokenPath';
149
150 6
            $jwksValue = $module->urlRulesPrefix . '/' . $module->jwksPath;
151 6
            $jwksSettings = 'urlRulesPrefix, jwksPath';
152
153 6
            $clientAuthorizationValue = $module->urlRulesPrefix . '/' . $module->clientAuthorizationPath;
154 6
            $clientAuthorizationSettings = 'urlRulesPrefix, clientAuthorizationPath';
155
156 6
            if ($module->enableOpenIdConnect) {
157 5
                if ($module->enableOpenIdConnectDiscovery) {
158 4
                    $oidcProviderConfigInfoValue = $module->openIdConnectProviderConfigurationInformationPath;
159 4
                    $oidcProviderConfigInfoSettings = 'openIdConnectProviderConfigurationInformationPath';
160
                } else {
161 1
                    $oidcProviderConfigInfoValue = '[OpenId Connect Discovery is disabled]';
162 1
                    $oidcProviderConfigInfoSettings = 'enableOpenIdConnectDiscovery';
163
                }
164
165 5
                if (!empty($module->openIdConnectUserinfoEndpoint)) {
166 4
                    if ($module->openIdConnectUserinfoEndpoint === true) {
167 3
                        $oidcUserinfoValue = $module->urlRulesPrefix . '/' . $module->openIdConnectUserinfoPath;
168 3
                        $oidcUserinfoSettings = 'urlRulesPrefix, openIdConnectUserinfoPath';
169
                    } else {
170 1
                        $oidcUserinfoValue = $module->openIdConnectUserinfoEndpoint;
171 4
                        $oidcUserinfoSettings = 'openIdConnectUserinfoEndpoint';
172
                    }
173
                } else {
174 1
                    $oidcUserinfoValue = '[Userinfo Endpoint is disabled]';
175 5
                    $oidcUserinfoSettings = 'openIdConnectUserinfoEndpoint';
176
                }
177
            } else {
178 1
                $oidcProviderConfigInfoValue = '[OpenID Connect is disabled]';
179 1
                $oidcProviderConfigInfoSettings = 'enableOpenIdConnect';
180
181 1
                $oidcUserinfoValue = '[OpenID Connect is disabled]';
182 6
                $oidcUserinfoSettings = 'enableOpenIdConnect';
183
            }
184
        } else {
185 1
            $authorizeClientValue = '[Only available for "authorization_server" role]';
186 1
            $authorizeClientSettings = 'serverRole';
187
188 1
            $accessTokenValue = '[Only available for "authorization_server" role]';
189 1
            $accessTokenSettings = 'serverRole';
190
191 1
            $jwksValue = '[Only available for "authorization_server" role]';
192 1
            $jwksSettings = 'serverRole';
193
194 1
            $clientAuthorizationValue = '[Only available for "authorization_server" role]';
195 1
            $clientAuthorizationSettings = 'serverRole';
196
197 1
            $oidcProviderConfigInfoValue = '[Only available for "authorization_server" role]';
198 1
            $oidcProviderConfigInfoSettings = 'serverRole';
199
200 1
            $oidcUserinfoValue = '[Only available for "authorization_server" role]';
201 1
            $oidcUserinfoSettings = 'serverRole';
202
        }
203
204 7
        return [
205 7
            'authorizeClient' => ['Authorize Client', $authorizeClientValue, $authorizeClientSettings],
206 7
            'accessToken' => ['Access Token', $accessTokenValue, $accessTokenSettings],
207 7
            'jwks' => ['JSON Web Key Sets', $jwksValue, $jwksSettings],
208 7
            'clientAuthorization' => ['Client Authorization', $clientAuthorizationValue, $clientAuthorizationSettings],
209 7
            'oidcProviderConfigInfo' => [
210 7
                'OpenID Connect Provider Configuration Information',
211 7
                $oidcProviderConfigInfoValue,
212 7
                $oidcProviderConfigInfoSettings,
213 7
            ],
214 7
            'oidcUserinfo' => ['OpenId Connect Userinfo', $oidcUserinfoValue, $oidcUserinfoSettings],
215 7
        ];
216
    }
217
}
218