| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Facile\OpenIDClient\Service; |
| 6 |
|
| 7 |
use Facile\OpenIDClient\Client\ClientInterface as OpenIDClient; |
| 8 |
use Facile\OpenIDClient\Exception\InvalidArgumentException; |
| 9 |
use Facile\OpenIDClient\Exception\OAuth2Exception; |
| 10 |
use Facile\OpenIDClient\Exception\RuntimeException; |
| 11 |
use Facile\OpenIDClient\Session\AuthSessionInterface; |
| 12 |
use Facile\OpenIDClient\Token\IdTokenVerifierBuilderInterface; |
| 13 |
use Facile\OpenIDClient\Token\TokenSetFactoryInterface; |
| 14 |
use Facile\OpenIDClient\Token\TokenSetInterface; |
| 15 |
use Facile\OpenIDClient\Token\TokenVerifierBuilderInterface; |
| 16 |
use JsonSerializable; |
| 17 |
use Psr\Http\Client\ClientExceptionInterface; |
| 18 |
use Psr\Http\Client\ClientInterface; |
| 19 |
use Psr\Http\Message\RequestFactoryInterface; |
| 20 |
use Psr\Http\Message\ServerRequestInterface; |
| 21 |
|
| 22 |
use function array_filter; |
| 23 |
use function array_key_exists; |
| 24 |
use function array_merge; |
| 25 |
use function Facile\OpenIDClient\get_endpoint_uri; |
| 26 |
use function Facile\OpenIDClient\parse_callback_params; |
| 27 |
use function Facile\OpenIDClient\parse_metadata_response; |
| 28 |
use function http_build_query; |
| 29 |
use function is_array; |
| 30 |
use function is_string; |
| 31 |
use function json_encode; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
final class AuthorizationService |
| 60 |
{ |
| 61 |
private TokenSetFactoryInterface $tokenSetFactory; |
| 62 |
|
| 63 |
private ClientInterface $client; |
| 64 |
|
| 65 |
private RequestFactoryInterface $requestFactory; |
| 66 |
|
| 67 |
private IdTokenVerifierBuilderInterface $idTokenVerifierBuilder; |
| 68 |
|
| 69 |
private TokenVerifierBuilderInterface $responseVerifierBuilder; |
| 70 |
|
| 71 |
public function __construct( |
| 72 |
TokenSetFactoryInterface $tokenSetFactory, |
| 73 |
ClientInterface $client, |
| 74 |
RequestFactoryInterface $requestFactory, |
| 75 |
IdTokenVerifierBuilderInterface $idTokenVerifierBuilder, |
| 76 |
TokenVerifierBuilderInterface $responseVerifierBuilder |
| 77 |
) { |
| 78 |
$this->tokenSetFactory = $tokenSetFactory; |
| 79 |
$this->client = $client; |
| 80 |
$this->requestFactory = $requestFactory; |
| 81 |
$this->idTokenVerifierBuilder = $idTokenVerifierBuilder; |
| 82 |
$this->responseVerifierBuilder = $responseVerifierBuilder; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public function getAuthorizationUri(OpenIDClient $client, array $params = []): string |
| 91 |
{ |
| 92 |
$clientMetadata = $client->getMetadata(); |
| 93 |
$issuerMetadata = $client->getIssuer()->getMetadata(); |
| 94 |
$endpointUri = $issuerMetadata->getAuthorizationEndpoint(); |
| 95 |
|
| 96 |
$params = array_merge([ |
| 97 |
'client_id' => $clientMetadata->getClientId(), |
| 98 |
'scope' => 'openid', |
| 99 |
'response_type' => $clientMetadata->getResponseTypes()[0] ?? 'code', |
| 100 |
'redirect_uri' => $clientMetadata->getRedirectUris()[0] ?? null, |
| 101 |
], $params); |
| 102 |
|
| 103 |
$params = array_filter($params, static fn($value): bool => null !== $value); |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
foreach ($params as $key => $value) { |
| 109 |
if (null === $value) { |
| 110 |
unset($params[$key]); |
| 111 |
} elseif ('claims' === $key && (is_array($value) || $value instanceof JsonSerializable)) { |
| 112 |
$params['claims'] = json_encode($value, JSON_THROW_ON_ERROR); |
| 113 |
} elseif (! is_string($value)) { |
| 114 |
$params[$key] = (string) $value; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
if (! array_key_exists('nonce', $params) && 'code' !== ($params['response_type'] ?? '')) { |
| 119 |
throw new InvalidArgumentException('nonce MUST be provided for implicit and hybrid flows'); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
$splitUrl = explode('?', $endpointUri, 2); |
| 124 |
if (isset($splitUrl[1])) { |
| 125 |
parse_str($splitUrl[1], $existingParameters); |
| 126 |
$params = array_merge($existingParameters, $params); |
| 127 |
} |
| 128 |
|
| 129 |
return $splitUrl[0] . '?' . http_build_query($params); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function getCallbackParams(ServerRequestInterface $serverRequest, OpenIDClient $client): array |
| 140 |
{ |
| 141 |
return $this->processResponseParams($client, parse_callback_params($serverRequest)); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public function callback( |
| 150 |
OpenIDClient $client, |
| 151 |
array $params, |
| 152 |
?string $redirectUri = null, |
| 153 |
?AuthSessionInterface $authSession = null, |
| 154 |
?int $maxAge = null |
| 155 |
): TokenSetInterface { |
| 156 |
$allowedParams = ['code', 'state', 'token_type', 'access_token', 'id_token', 'refresh_token', 'expires_in', 'code_verifier']; |
| 157 |
|
| 158 |
$safeParams = array_intersect_key( |
| 159 |
$params, |
| 160 |
array_fill_keys($allowedParams, true) |
| 161 |
); |
| 162 |
$tokenSet = $this->tokenSetFactory->fromArray($safeParams); |
| 163 |
|
| 164 |
$idToken = $tokenSet->getIdToken(); |
| 165 |
|
| 166 |
if (null !== $idToken) { |
| 167 |
|
| 168 |
$claims = $this->idTokenVerifierBuilder->build($client) |
| 169 |
->withState($authSession?->getState()) |
| 170 |
->withCode($tokenSet->getCode()) |
| 171 |
->withAccessToken($tokenSet->getAccessToken()) |
| 172 |
->withMaxAge($maxAge) |
| 173 |
->withNonce($authSession?->getNonce()) |
| 174 |
->verify($idToken); |
| 175 |
$tokenSet = $tokenSet->withClaims($claims); |
| 176 |
} |
| 177 |
|
| 178 |
if (null === $tokenSet->getCode()) { |
| 179 |
return $tokenSet; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
return $this->fetchToken($client, $tokenSet, $redirectUri, $authSession, $maxAge); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public function fetchToken( |
| 190 |
OpenIDClient $client, |
| 191 |
TokenSetInterface $tokenSet, |
| 192 |
?string $redirectUri = null, |
| 193 |
?AuthSessionInterface $authSession = null, |
| 194 |
?int $maxAge = null |
| 195 |
): TokenSetInterface { |
| 196 |
$code = $tokenSet->getCode(); |
| 197 |
|
| 198 |
if (null === $code) { |
| 199 |
throw new RuntimeException('Unable to fetch token without a code'); |
| 200 |
} |
| 201 |
|
| 202 |
if (null === $redirectUri) { |
| 203 |
$redirectUri = $client->getMetadata()->getRedirectUris()[0] ?? null; |
| 204 |
} |
| 205 |
|
| 206 |
if (null === $redirectUri) { |
| 207 |
throw new InvalidArgumentException('A redirect_uri should be provided'); |
| 208 |
} |
| 209 |
|
| 210 |
$params = [ |
| 211 |
'grant_type' => 'authorization_code', |
| 212 |
'code' => $code, |
| 213 |
'redirect_uri' => $redirectUri, |
| 214 |
]; |
| 215 |
|
| 216 |
if (null !== $authSession && null !== $authSession->getCodeVerifier()) { |
| 217 |
$params['code_verifier'] = $authSession->getCodeVerifier(); |
| 218 |
} |
| 219 |
|
| 220 |
$tokenSet = $this->grant($client, $params); |
| 221 |
|
| 222 |
$idToken = $tokenSet->getIdToken(); |
| 223 |
|
| 224 |
if (null !== $idToken) { |
| 225 |
|
| 226 |
$claims = $this->idTokenVerifierBuilder->build($client) |
| 227 |
->withState($authSession?->getState()) |
| 228 |
->withMaxAge($maxAge) |
| 229 |
->withNonce($authSession?->getNonce()) |
| 230 |
->verify($idToken); |
| 231 |
$tokenSet = $tokenSet->withClaims($claims); |
| 232 |
} |
| 233 |
|
| 234 |
return $tokenSet; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public function refresh(OpenIDClient $client, string $refreshToken, array $params = []): TokenSetInterface |
| 241 |
{ |
| 242 |
$tokenSet = $this->grant($client, array_merge($params, [ |
| 243 |
'grant_type' => 'refresh_token', |
| 244 |
'refresh_token' => $refreshToken, |
| 245 |
])); |
| 246 |
|
| 247 |
$idToken = $tokenSet->getIdToken(); |
| 248 |
|
| 249 |
if (null === $idToken) { |
| 250 |
return $tokenSet; |
| 251 |
} |
| 252 |
|
| 253 |
$idToken = $tokenSet->getIdToken(); |
| 254 |
|
| 255 |
if (null !== $idToken) { |
| 256 |
|
| 257 |
$claims = $this->idTokenVerifierBuilder->build($client) |
| 258 |
->withAccessToken($tokenSet->getAccessToken()) |
| 259 |
->verify($idToken); |
| 260 |
$tokenSet = $tokenSet->withClaims($claims); |
| 261 |
} |
| 262 |
|
| 263 |
return $tokenSet; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public function grant(OpenIDClient $client, array $params = []): TokenSetInterface |
| 272 |
{ |
| 273 |
$authMethod = $client->getAuthMethodFactory() |
| 274 |
->create($client->getMetadata()->getTokenEndpointAuthMethod()); |
| 275 |
|
| 276 |
$endpointUri = get_endpoint_uri($client, 'token_endpoint'); |
| 277 |
|
| 278 |
$tokenRequest = $this->requestFactory->createRequest('POST', $endpointUri) |
| 279 |
->withHeader('content-type', 'application/x-www-form-urlencoded'); |
| 280 |
|
| 281 |
$tokenRequest = $authMethod->createRequest($tokenRequest, $client, $params); |
| 282 |
|
| 283 |
$httpClient = $client->getHttpClient() ?? $this->client; |
| 284 |
|
| 285 |
try { |
| 286 |
$response = $httpClient->sendRequest($tokenRequest); |
| 287 |
} catch (ClientExceptionInterface $e) { |
| 288 |
throw new RuntimeException('Unable to get token response', 0, $e); |
| 289 |
} |
| 290 |
|
| 291 |
$data = parse_metadata_response($response); |
| 292 |
$params = $this->processResponseParams($client, $data); |
| 293 |
|
| 294 |
return $this->tokenSetFactory->fromArray($params); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
private function isAuthorizationResponseObject(array $params): bool |
| 305 |
{ |
| 306 |
return array_key_exists('response', $params); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
private function processResponseParams(OpenIDClient $client, array $params): array |
| 321 |
{ |
| 322 |
if ($this->isAuthorizationResponseObject($params)) { |
| 323 |
$params = $this->responseVerifierBuilder->build($client) |
| 324 |
->verify($params['response']); |
| 325 |
} |
| 326 |
|
| 327 |
if (OAuth2Exception::isOAuth2Error($params)) { |
| 328 |
throw OAuth2Exception::fromParameters($params); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
return $params; |
| 333 |
} |
| 334 |
} |