|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Module\oidc\Controllers\OAuth2; |
| 6 | + |
| 7 | +use SimpleSAML\Module\oidc\Codebooks\ApiScopesEnum; |
| 8 | +use SimpleSAML\Module\oidc\Exceptions\AuthorizationException; |
| 9 | +use SimpleSAML\Module\oidc\ModuleConfig; |
| 10 | +use SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException; |
| 11 | +use SimpleSAML\Module\oidc\Server\Validators\BearerTokenValidator; |
| 12 | +use SimpleSAML\Module\oidc\Services\Api\Authorization; |
| 13 | +use SimpleSAML\Module\oidc\Services\LoggerService; |
| 14 | +use SimpleSAML\Module\oidc\Utils\AuthenticatedOAuth2ClientResolver; |
| 15 | +use SimpleSAML\Module\oidc\Utils\RequestParamsResolver; |
| 16 | +use SimpleSAML\Module\oidc\Utils\Routes; |
| 17 | +use SimpleSAML\Module\oidc\ValueAbstracts\ResolvedClientAuthenticationMethod; |
| 18 | +use SimpleSAML\OpenID\Codebooks\ClaimsEnum; |
| 19 | +use SimpleSAML\OpenID\Codebooks\HttpMethodsEnum; |
| 20 | +use SimpleSAML\OpenID\Codebooks\ParamsEnum; |
| 21 | +use Symfony\Component\HttpFoundation\Request; |
| 22 | +use Symfony\Component\HttpFoundation\Response; |
| 23 | + |
| 24 | +class TokenIntrospectionController |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @throws OidcServerException |
| 28 | + */ |
| 29 | + public function __construct( |
| 30 | + protected readonly ModuleConfig $moduleConfig, |
| 31 | + protected readonly AuthenticatedOAuth2ClientResolver $authenticatedOAuth2ClientResolver, |
| 32 | + protected readonly Routes $routes, |
| 33 | + protected readonly LoggerService $loggerService, |
| 34 | + protected readonly Authorization $apiAuthorization, |
| 35 | + protected readonly RequestParamsResolver $requestParamsResolver, |
| 36 | + protected readonly BearerTokenValidator $bearerTokenValidator, |
| 37 | + ) { |
| 38 | + if (!$this->moduleConfig->getApiEnabled()) { |
| 39 | + $this->loggerService->warning('API capabilities not enabled.'); |
| 40 | + throw OidcServerException::forbidden('API capabilities not enabled.'); |
| 41 | + } |
| 42 | + |
| 43 | + if (!$this->moduleConfig->getApiOAuth2TokenIntrospectionEndpointEnabled()) { |
| 44 | + $this->loggerService->warning('OAuth2 Token Introspection API endpoint not enabled.'); |
| 45 | + throw OidcServerException::forbidden('OAuth2 Token Introspection API endpoint not enabled.'); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public function __invoke(Request $request): Response |
| 50 | + { |
| 51 | + // TODO mivanci Add support for Refresh Tokens. |
| 52 | + // TODO mivanci Add endpoint to OAuth2 discovery document. |
| 53 | + |
| 54 | + try { |
| 55 | + $this->ensureAuthenticatedClient($request); |
| 56 | + } catch (AuthorizationException $e) { |
| 57 | + $this->loggerService->error( |
| 58 | + 'TokenIntrospectionController::invoke: AuthorizationException: ' . $e->getMessage(), |
| 59 | + ); |
| 60 | + return $this->routes->newJsonErrorResponse( |
| 61 | + error: 'unauthorized', |
| 62 | + description: $e->getMessage(), |
| 63 | + httpCode: Response::HTTP_UNAUTHORIZED, |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + $allowedMethods = [HttpMethodsEnum::POST]; |
| 68 | + |
| 69 | + $tokenParam = $this->requestParamsResolver->getFromRequestBasedOnAllowedMethods( |
| 70 | + ParamsEnum::Token->value, |
| 71 | + $request, |
| 72 | + $allowedMethods, |
| 73 | + ); |
| 74 | + |
| 75 | + if (!$tokenParam) { |
| 76 | + return $this->routes->newJsonErrorResponse( |
| 77 | + error: 'invalid_request', |
| 78 | + description: 'Missing token parameter.', |
| 79 | + httpCode: Response::HTTP_BAD_REQUEST, |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + // For now, we will only support Access Tokens. |
| 84 | +// $tokenTypeHintParam = $this->requestParamsResolver->getFromRequestBasedOnAllowedMethods( |
| 85 | +// ParamsEnum::TokenTypeHint->value, |
| 86 | +// $request, |
| 87 | +// $allowedMethods, |
| 88 | +// ); |
| 89 | + |
| 90 | + try { |
| 91 | + $accessToken = $this->bearerTokenValidator->ensureValidAccessToken($tokenParam); |
| 92 | + } catch (\Throwable $e) { |
| 93 | + $this->loggerService->error('Token validation failed: ' . $e->getMessage()); |
| 94 | + return $this->routes->newJsonResponse(['active' => false]); |
| 95 | + } |
| 96 | + $scopeClaim = null; |
| 97 | + /** @psalm-suppress MixedAssignment */ |
| 98 | + $accessTokenScopes = $accessToken->getPayloadClaim('scopes'); |
| 99 | + if (is_array($accessTokenScopes)) { |
| 100 | + $accessTokenScopes = array_filter( |
| 101 | + $accessTokenScopes, |
| 102 | + static fn($scope) => is_string($scope) && !empty($scope), |
| 103 | + ); |
| 104 | + $scopeClaim = implode(' ', $accessTokenScopes); |
| 105 | + } |
| 106 | + |
| 107 | + $audience = is_array($audience = $accessToken->getAudience()) ? $audience[0] ?? null : null; |
| 108 | + |
| 109 | + $payload = array_filter([ |
| 110 | + 'active' => true, |
| 111 | + 'scope' => $scopeClaim, |
| 112 | + 'token_type' => 'Bearer', |
| 113 | + ClaimsEnum::Exp->value => $accessToken->getExpirationTime(), |
| 114 | + ClaimsEnum::Iat->value => $accessToken->getIssuedAt(), |
| 115 | + ClaimsEnum::Nbf->value => $accessToken->getNotBefore(), |
| 116 | + ClaimsEnum::Sub->value => $accessToken->getSubject(), |
| 117 | + ClaimsEnum::Aud->value => $audience, |
| 118 | + ClaimsEnum::Iss->value => $accessToken->getIssuer(), |
| 119 | + ClaimsEnum::Jti->value => $accessToken->getJwtId(), |
| 120 | + ]); |
| 121 | + |
| 122 | + return $this->routes->newJsonResponse($payload); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @throws AuthorizationException |
| 127 | + */ |
| 128 | + protected function ensureAuthenticatedClient(Request $request): void |
| 129 | + { |
| 130 | + $this->loggerService->debug('TokenIntrospectionController::ensureAuthenticatedClient - start'); |
| 131 | + $this->loggerService->debug('Trying supported OAuth2 client authentication methods.'); |
| 132 | + |
| 133 | + // First, try regular OAuth2 client authentication methods. |
| 134 | + $resolvedClientAuthenticationMethod = $this->authenticatedOAuth2ClientResolver->forAnySupportedMethod($request); |
| 135 | + |
| 136 | + if ( |
| 137 | + $resolvedClientAuthenticationMethod instanceof ResolvedClientAuthenticationMethod && |
| 138 | + $resolvedClientAuthenticationMethod->getClientAuthenticationMethod()->isNotNone() |
| 139 | + ) { |
| 140 | + $this->loggerService->debug( |
| 141 | + 'Client authenticated using supported OAuth2 client authentication method: ' . |
| 142 | + $resolvedClientAuthenticationMethod->getClientAuthenticationMethod()->value, |
| 143 | + ); |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + $this->loggerService->debug('No regular OAuth2 client authentication method found.'); |
| 148 | + $this->loggerService->debug('Trying API client authentication method.'); |
| 149 | + |
| 150 | + |
| 151 | + $this->apiAuthorization->requireTokenForAnyOfScope( |
| 152 | + $request, |
| 153 | + [ApiScopesEnum::OAuth2TokenIntrospection, ApiScopesEnum::OAuth2All, ApiScopesEnum::All], |
| 154 | + ); |
| 155 | + } |
| 156 | +} |
0 commit comments