Passed
Push — master ( 0c1875...09be9f )
by Rutger
15:24 queued 43s
created

Oauth2OpenidConfigurationAction   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Test Coverage

Coverage 91.09%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 81
c 1
b 0
f 0
dl 0
loc 140
ccs 92
cts 101
cp 0.9109
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
F run() 0 135 15
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\controllers\web\wellknown;
4
5
use League\OAuth2\Server\Grant\AuthCodeGrant;
6
use League\OAuth2\Server\Grant\ImplicitGrant;
7
use rhertogh\Yii2Oauth2Server\controllers\web\base\Oauth2BaseWebAction;
8
use rhertogh\Yii2Oauth2Server\controllers\web\Oauth2WellKnownController;
9
use rhertogh\Yii2Oauth2Server\interfaces\components\openidconnect\request\Oauth2OidcAuthenticationRequestInterface;
10
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\Oauth2CertificatesControllerInterface;
11
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\Oauth2OidcControllerInterface;
12
use rhertogh\Yii2Oauth2Server\interfaces\controllers\web\Oauth2ServerControllerInterface;
13
use Yii;
14
use yii\helpers\Url;
15
use yii\web\ForbiddenHttpException;
16
17
/**
18
 * @property Oauth2WellKnownController $controller
19
 */
20
class Oauth2OpenidConfigurationAction extends Oauth2BaseWebAction
21
{
22
    /**
23
     *
24
     */
25 7
    public function run()
26
    {
27 7
        $module = $this->controller->module;
28
29 7
        if (!$module->enableOpenIdConnect) {
30 1
            throw new ForbiddenHttpException('OpenID Connect is disabled.');
31
        }
32
33 6
        $supportedScopeAndClaimIdentifiers = $module->getOidcScopeCollection()
34 6
            ->getSupportedScopeAndClaimIdentifiers();
35
36 6
        $responseTypes = [];
37 6
        foreach ($module->getAuthorizationServer()->getEnabledGrantTypes() as $grantType) {
38 6
            if ($grantType instanceof AuthCodeGrant) {
39 6
                $responseTypes[] = 'code';
40 6
            } elseif ($grantType instanceof ImplicitGrant) {
41 6
                $responseTypes[] = 'token';
42
            }
43
        }
44 6
        $responseTypes = array_unique($responseTypes);
45 6
        $responseTypeCombinations = [];
46 6
        foreach ($responseTypes as $responseType) {
47 6
            $newCombinations = [$responseType];
48 6
            foreach ($responseTypeCombinations as $responseTypeCombination) {
49 6
                $newCombinations[] = $responseTypeCombination . ' ' . $responseType;
50
            }
51 6
            $responseTypeCombinations = array_merge($responseTypeCombinations, $newCombinations);
52
        }
53
54 6
        $authorizationEndpoint = Url::to(
55 6
            [
56 6
                Oauth2ServerControllerInterface::CONTROLLER_NAME
57 6
                    . '/' . Oauth2ServerControllerInterface::ACTION_NAME_AUTHORIZE
58 6
            ],
59 6
            true
60 6
        );
61 6
        $tokenEndpoint = Url::to(
62 6
            [
63 6
                Oauth2ServerControllerInterface::CONTROLLER_NAME
64 6
                    . '/' . Oauth2ServerControllerInterface::ACTION_NAME_ACCESS_TOKEN,
65 6
            ],
66 6
            true
67 6
        );
68 6
        $jwksUri = Url::to(
69 6
            [
70 6
                Oauth2CertificatesControllerInterface::CONTROLLER_NAME
71 6
                    . '/' . Oauth2CertificatesControllerInterface::ACTION_NAME_JWKS,
72 6
            ],
73 6
            true
74 6
        );
75
76
        // See https://openid.net/specs/openid-connect-discovery-1_0.html#rfc.section.3.
77 6
        $openIdConfig = [
78 6
            'issuer' => Yii::$app->request->getHostInfo(),
79 6
            'authorization_endpoint' => $authorizationEndpoint,
80 6
            'token_endpoint' => $tokenEndpoint
81 6
        ];
82
83
        // Add 'userinfo_endpoint' if configured.
84 6
        if (!empty($module->openIdConnectUserinfoEndpoint)) {
85 6
            if ($module->openIdConnectUserinfoEndpoint === true) {
86 5
                $openIdConfig['userinfo_endpoint'] = Url::to(
87 5
                    [
88 5
                        Oauth2OidcControllerInterface::CONTROLLER_NAME
89 5
                            . '/' . Oauth2OidcControllerInterface::ACTION_NAME_USERINFO,
90 5
                    ],
91 5
                    true
92 5
                );
93
            } else {
94 1
                $openIdConfig['userinfo_endpoint'] = $module->openIdConnectUserinfoEndpoint;
95
            }
96
        }
97
98
        // Add 'end_session_endpoint' if configured.
99 6
        if (!empty($module->openIdConnectEndSessionEndpoint)) {
100
            if ($module->openIdConnectEndSessionEndpoint === true) {
101
                $openIdConfig['end_session_endpoint'] = Url::to(
102
                    [
103
                        Oauth2OidcControllerInterface::CONTROLLER_NAME
104
                        . '/' . Oauth2OidcControllerInterface::ACTION_END_SESSION,
105
                    ],
106
                    true
107
                );
108
            } else {
109
                $openIdConfig['end_session_endpoint'] = $module->openIdConnectEndSessionEndpoint;
110
            }
111
        }
112
113 6
        $openIdConfig += [
114 6
            'jwks_uri' => $jwksUri,
115 6
            'scopes_supported' => $supportedScopeAndClaimIdentifiers['scopeIdentifiers'],
116 6
            'claims_supported' => $supportedScopeAndClaimIdentifiers['claimIdentifiers'],
117 6
            'response_types_supported' => $responseTypeCombinations,
118 6
        ];
119
120 6
        if ($module->openIdConnectDiscoveryIncludeSupportedGrantTypes) {
121 6
            $enabledGrantTypes = $module->getAuthorizationServer()->getEnabledGrantTypes();
122 6
            $supportedGrantTypes = [];
123 6
            foreach ($enabledGrantTypes as $grantType) {
124 6
                $grantTypeIdentifier = $grantType->getIdentifier();
125
                if (
126 6
                    in_array(
127 6
                        $grantTypeIdentifier,
128 6
                        Oauth2OidcAuthenticationRequestInterface::SUPPORTED_AUTHENTICATION_FLOWS
129 6
                    )
130
                ) {
131 6
                    $supportedGrantTypes[] = $grantTypeIdentifier;
132
                }
133
            }
134 6
            $openIdConfig['grant_types_supported'] = $supportedGrantTypes;
135
        }
136
137 6
        $openIdConfig += [
138 6
            'subject_types_supported' => [
139 6
                'public',
140 6
            ],
141 6
            'id_token_signing_alg_values_supported' => [
142 6
                'RS256',
143 6
            ],
144 6
            'token_endpoint_auth_methods_supported' => [
145 6
                'client_secret_basic',
146 6
                'client_secret_post',
147 6
            ],
148 6
        ];
149
150 6
        if (!empty($module->openIdConnectDiscoveryServiceDocumentationUrl)) {
151 1
            $openIdConfig['service_documentation'] = $module->openIdConnectDiscoveryServiceDocumentationUrl;
152
        }
153
154 6
        $openIdConfig += [
155 6
            'claims_parameter_supported' => false, //ToDo: set to `true` when the 'claims' parameter is supported.
156 6
            'request_parameter_supported' => false, //ToDo: set to `true` when the 'request' parameter is supported.
157 6
        ];
158
159 6
        return $openIdConfig;
160
    }
161
}
162