|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Keycloak OIDC provider config factory. |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 7 | + * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 8 | + * obtain one at https://mozilla.org/MPL/2.0/. |
| 9 | + * |
| 10 | + * @package phpMyFAQ |
| 11 | + * @author Thorsten Rinne <thorsten@phpmyfaq.de> |
| 12 | + * @copyright 2026 phpMyFAQ Team |
| 13 | + * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
| 14 | + * @link https://www.phpmyfaq.de |
| 15 | + * @since 2026-04-18 |
| 16 | + */ |
| 17 | + |
| 18 | +declare(strict_types=1); |
| 19 | + |
| 20 | +namespace phpMyFAQ\Auth\Keycloak; |
| 21 | + |
| 22 | +use phpMyFAQ\Auth\Oidc\OidcClientConfig; |
| 23 | +use phpMyFAQ\Auth\Oidc\OidcProviderConfig; |
| 24 | +use phpMyFAQ\Configuration; |
| 25 | + |
| 26 | +final readonly class KeycloakProviderConfigFactory |
| 27 | +{ |
| 28 | + public function __construct( |
| 29 | + private Configuration $configuration, |
| 30 | + ) { |
| 31 | + } |
| 32 | + |
| 33 | + public function create(): OidcProviderConfig |
| 34 | + { |
| 35 | + $baseUrl = rtrim(trim((string) $this->configuration->get('keycloak.baseUrl')), characters: '/'); |
| 36 | + $realm = trim((string) $this->configuration->get('keycloak.realm')); |
| 37 | + $redirectUri = trim((string) $this->configuration->get('keycloak.redirectUri')); |
| 38 | + $scopes = preg_split('/\s+/', trim((string) $this->configuration->get('keycloak.scopes'))); |
| 39 | + if ($scopes === false) { |
| 40 | + $scopes = []; |
| 41 | + } |
| 42 | + |
| 43 | + if ($redirectUri === '') { |
| 44 | + $redirectUri = rtrim($this->configuration->getDefaultUrl(), characters: '/') . '/auth/keycloak/callback'; |
| 45 | + } |
| 46 | + |
| 47 | + return new OidcProviderConfig( |
| 48 | + provider: 'keycloak', |
| 49 | + enabled: $this->toBool($this->configuration->get('keycloak.enable')), |
| 50 | + discoveryUrl: $this->buildDiscoveryUrl($baseUrl, $realm), |
| 51 | + client: new OidcClientConfig( |
| 52 | + clientId: trim((string) $this->configuration->get('keycloak.clientId')), |
| 53 | + clientSecret: trim((string) $this->configuration->get('keycloak.clientSecret')), |
| 54 | + redirectUri: $redirectUri, |
| 55 | + scopes: array_values(array_filter($scopes, static fn(string $scope): bool => $scope !== '')), |
| 56 | + ), |
| 57 | + autoProvision: $this->toBool($this->configuration->get('keycloak.autoProvision')), |
| 58 | + logoutRedirectUrl: trim((string) $this->configuration->get('keycloak.logoutRedirectUrl')), |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + private function buildDiscoveryUrl(string $baseUrl, string $realm): string |
| 63 | + { |
| 64 | + if ($baseUrl === '' || $realm === '') { |
| 65 | + return ''; |
| 66 | + } |
| 67 | + |
| 68 | + return $baseUrl . '/realms/' . rawurlencode($realm) . '/.well-known/openid-configuration'; |
| 69 | + } |
| 70 | + |
| 71 | + private function toBool(mixed $value): bool |
| 72 | + { |
| 73 | + return $value === true || $value === 1 || $value === '1' || $value === 'true'; |
| 74 | + } |
| 75 | +} |
0 commit comments