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

Oauth2OpenidConfigurationAction   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Test Coverage

Coverage 99.01%

Importance

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