|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Module\oidc\Services\Api; |
| 6 | + |
| 7 | +use SimpleSAML\Locale\Translate; |
| 8 | +use SimpleSAML\Module\oidc\Bridges\SspBridge; |
| 9 | +use SimpleSAML\Module\oidc\Codebooks\ApiScopesEnum; |
| 10 | +use SimpleSAML\Module\oidc\Exceptions\AuthorizationException; |
| 11 | +use SimpleSAML\Module\oidc\ModuleConfig; |
| 12 | +use Symfony\Component\HttpFoundation\Request; |
| 13 | +use Throwable; |
| 14 | + |
| 15 | +class Authorization |
| 16 | +{ |
| 17 | + public const KEY_TOKEN = 'token'; |
| 18 | + |
| 19 | + public const KEY_AUTHORIZATION = 'Authorization'; |
| 20 | + |
| 21 | + public function __construct( |
| 22 | + protected readonly ModuleConfig $moduleConfig, |
| 23 | + protected readonly SspBridge $sspBridge, |
| 24 | + ) { |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + /** |
| 29 | + * @throws AuthorizationException |
| 30 | + */ |
| 31 | + public function requireSimpleSAMLphpAdmin(bool $forceAdminAuthentication = false): void |
| 32 | + { |
| 33 | + if ($forceAdminAuthentication) { |
| 34 | + try { |
| 35 | + $this->sspBridge->utils()->auth()->requireAdmin(); |
| 36 | + } catch (\Throwable $exception) { |
| 37 | + throw new AuthorizationException( |
| 38 | + Translate::noop('Unable to initiate admin authentication.'), |
| 39 | + $exception->getCode(), |
| 40 | + $exception, |
| 41 | + ); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + if (! $this->sspBridge->utils()->auth()->isAdmin()) { |
| 46 | + throw new AuthorizationException(Translate::noop('SimpleSAMLphp Admin access required.')); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @param ApiScopesEnum[] $requiredScopes |
| 52 | + * |
| 53 | + * @throws AuthorizationException |
| 54 | + */ |
| 55 | + public function requireTokenForAnyOfScope(Request $request, array $requiredScopes): void |
| 56 | + { |
| 57 | + try { |
| 58 | + $this->requireSimpleSAMLphpAdmin(); |
| 59 | + return; |
| 60 | + } catch (Throwable) { |
| 61 | + // Not admin, check for token. |
| 62 | + } |
| 63 | + |
| 64 | + if (empty($token = $this->findToken($request))) { |
| 65 | + throw new AuthorizationException(Translate::noop('Token not provided.')); |
| 66 | + } |
| 67 | + |
| 68 | + if (empty($tokenScopes = $this->moduleConfig->getApiTokenScopes($token))) { |
| 69 | + throw new AuthorizationException(Translate::noop('Token does not have defined scopes.')); |
| 70 | + } |
| 71 | + |
| 72 | + $hasAny = !empty(array_filter($tokenScopes, fn($tokenScope) => in_array($tokenScope, $requiredScopes, true))); |
| 73 | + |
| 74 | + if (!$hasAny) { |
| 75 | + throw new AuthorizationException(Translate::noop('Token is not authorized.')); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + protected function findToken(Request $request): ?string |
| 80 | + { |
| 81 | + if ($token = trim((string) $request->get(self::KEY_TOKEN))) { |
| 82 | + return $token; |
| 83 | + } |
| 84 | + |
| 85 | + if ($request->headers->has(self::KEY_AUTHORIZATION)) { |
| 86 | + return trim( |
| 87 | + (string) preg_replace( |
| 88 | + '/^\s*Bearer\s/', |
| 89 | + '', |
| 90 | + (string)$request->headers->get(self::KEY_AUTHORIZATION), |
| 91 | + ), |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + return null; |
| 96 | + } |
| 97 | +} |
0 commit comments