|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace MinVWS\OpenIDConnectLaravel\OpenIDConfiguration; |
| 6 | + |
| 7 | +use Illuminate\Contracts\Cache\Repository; |
| 8 | +use Illuminate\Support\Facades\Http; |
| 9 | + |
| 10 | +class OpenIDConfigurationLoader |
| 11 | +{ |
| 12 | + public function __construct( |
| 13 | + protected string $issuer, |
| 14 | + protected ?Repository $cacheStore = null, |
| 15 | + protected int $cacheTtl = 3600, |
| 16 | + ) { |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * @throws OpenIDConfigurationLoaderException |
| 21 | + */ |
| 22 | + public function getConfiguration(): OpenIDConfiguration |
| 23 | + { |
| 24 | + if (!$this->cacheStore) { |
| 25 | + return $this->getConfigurationFromIssuer(); |
| 26 | + } |
| 27 | + |
| 28 | + return $this->cacheStore->remember('openid-configuration', $this->cacheTtl, function () { |
| 29 | + return $this->getConfigurationFromIssuer(); |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @throws OpenIDConfigurationLoaderException |
| 35 | + */ |
| 36 | + protected function getConfigurationFromIssuer(): OpenIDConfiguration |
| 37 | + { |
| 38 | + $url = $this->getOpenIDConfigurationUrl(); |
| 39 | + |
| 40 | + $response = Http::get($url); |
| 41 | + if (!$response->successful()) { |
| 42 | + throw new OpenIDConfigurationLoaderException( |
| 43 | + message: 'Could not load OpenID configuration from issuer', |
| 44 | + context: [ |
| 45 | + 'issuer' => $this->issuer, |
| 46 | + 'url' => $url, |
| 47 | + 'response_status_code' => $response->status(), |
| 48 | + 'response' => $response, |
| 49 | + ], |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + if (!is_array($response->json())) { |
| 54 | + throw new OpenIDConfigurationLoaderException( |
| 55 | + message: 'Response body of OpenID configuration is not JSON', |
| 56 | + context: [ |
| 57 | + 'issuer' => $this->issuer, |
| 58 | + 'url' => $url, |
| 59 | + 'response_status_code' => $response->status(), |
| 60 | + 'response' => $response, |
| 61 | + 'response_body' => $response->body(), |
| 62 | + ], |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + return new OpenIDConfiguration( |
| 67 | + version: $response->json('version', ''), |
| 68 | + tokenEndpointAuthMethodsSupported: $response->json('token_endpoint_auth_methods_supported', []), |
| 69 | + claimsParameterSupported: $response->json('claims_parameter_supported', false), |
| 70 | + requestParameterSupported: $response->json('request_parameter_supported', false), |
| 71 | + requestUriParameterSupported: $response->json('request_uri_parameter_supported', false), |
| 72 | + requireRequestUriRegistration: $response->json('require_request_uri_registration', false), |
| 73 | + grantTypesSupported: $response->json('grant_types_supported', []), |
| 74 | + frontchannelLogoutSupported: $response->json('frontchannel_logout_supported', false), |
| 75 | + frontchannelLogoutSessionSupported: $response->json('frontchannel_logout_session_supported', false), |
| 76 | + backchannelLogoutSupported: $response->json('backchannel_logout_supported', false), |
| 77 | + backchannelLogoutSessionSupported: $response->json('backchannel_logout_session_supported', false), |
| 78 | + issuer: $response->json('issuer', ''), |
| 79 | + authorizationEndpoint: $response->json('authorization_endpoint', ''), |
| 80 | + jwksUri: $response->json('jwks_uri', ''), |
| 81 | + tokenEndpoint: $response->json('token_endpoint', ''), |
| 82 | + scopesSupported: $response->json('scopes_supported', []), |
| 83 | + responseTypesSupported: $response->json('response_types_supported', []), |
| 84 | + responseModesSupported: $response->json('response_modes_supported', []), |
| 85 | + subjectTypesSupported: $response->json('subject_types_supported', []), |
| 86 | + idTokenSigningAlgValuesSupported: $response->json('id_token_signing_alg_values_supported', []), |
| 87 | + userinfoEndpoint: $response->json('userinfo_endpoint', ''), |
| 88 | + codeChallengeMethodsSupported: $response->json('code_challenge_methods_supported', []) |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + protected function getOpenIDConfigurationUrl(): string |
| 93 | + { |
| 94 | + return $this->issuer . '/.well-known/openid-configuration'; |
| 95 | + } |
| 96 | +} |
0 commit comments