| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-13 06:04:28 +08:00
										 |  |  | namespace BookStack\Auth\Access\Oidc; | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-13 06:04:28 +08:00
										 |  |  | class OidcIdToken | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-04-28 06:40:14 +08:00
										 |  |  |     protected array $header; | 
					
						
							|  |  |  |     protected array $payload; | 
					
						
							|  |  |  |     protected string $signature; | 
					
						
							|  |  |  |     protected string $issuer; | 
					
						
							|  |  |  |     protected array $tokenParts = []; | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @var array[]|string[] | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-04-28 06:40:14 +08:00
										 |  |  |     protected array $keys; | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function __construct(string $token, string $issuer, array $keys) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->keys = $keys; | 
					
						
							|  |  |  |         $this->issuer = $issuer; | 
					
						
							|  |  |  |         $this->parse($token); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Parse the token content into its components. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function parse(string $token): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->tokenParts = explode('.', $token); | 
					
						
							|  |  |  |         $this->header = $this->parseEncodedTokenPart($this->tokenParts[0]); | 
					
						
							|  |  |  |         $this->payload = $this->parseEncodedTokenPart($this->tokenParts[1] ?? ''); | 
					
						
							|  |  |  |         $this->signature = $this->base64UrlDecode($this->tokenParts[2] ?? '') ?: ''; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Parse a Base64-JSON encoded token part. | 
					
						
							|  |  |  |      * Returns the data as a key-value array or empty array upon error. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function parseEncodedTokenPart(string $part): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $json = $this->base64UrlDecode($part) ?: '{}'; | 
					
						
							|  |  |  |         $decoded = json_decode($json, true); | 
					
						
							| 
									
										
										
										
											2021-10-16 23:01:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  |         return is_array($decoded) ? $decoded : []; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Base64URL decode. Needs some character conversions to be compatible | 
					
						
							|  |  |  |      * with PHP's default base64 handling. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function base64UrlDecode(string $encoded): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return base64_decode(strtr($encoded, '-_', '+/')); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Validate all possible parts of the id token. | 
					
						
							| 
									
										
										
										
											2021-10-16 23:01:59 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-10-13 06:04:28 +08:00
										 |  |  |      * @throws OidcInvalidTokenException | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-10-12 07:01:51 +08:00
										 |  |  |     public function validate(string $clientId): bool | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->validateTokenStructure(); | 
					
						
							|  |  |  |         $this->validateTokenSignature(); | 
					
						
							| 
									
										
										
										
											2021-10-12 06:00:45 +08:00
										 |  |  |         $this->validateTokenClaims($clientId); | 
					
						
							| 
									
										
										
										
											2021-10-16 23:01:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-12 07:01:51 +08:00
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2021-10-12 06:00:45 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Fetch a specific claim from this token. | 
					
						
							|  |  |  |      * Returns null if it is null or does not exist. | 
					
						
							| 
									
										
										
										
											2021-10-16 23:01:59 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-10-12 06:00:45 +08:00
										 |  |  |      * @return mixed|null | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getClaim(string $claim) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->payload[$claim] ?? null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get all returned claims within the token. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-10-12 23:48:54 +08:00
										 |  |  |     public function getAllClaims(): array | 
					
						
							| 
									
										
										
										
											2021-10-12 06:00:45 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         return $this->payload; | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-28 06:40:14 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Replace the existing claim data of this token with that provided. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function replaceClaims(array $claims): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->payload = $claims; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Validate the structure of the given token and ensure we have the required pieces. | 
					
						
							| 
									
										
										
										
											2021-10-16 23:01:59 +08:00
										 |  |  |      * As per https://datatracker.ietf.org/doc/html/rfc7519#section-7.2.
 | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-10-13 06:04:28 +08:00
										 |  |  |      * @throws OidcInvalidTokenException | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function validateTokenStructure(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         foreach (['header', 'payload'] as $prop) { | 
					
						
							|  |  |  |             if (empty($this->$prop) || !is_array($this->$prop)) { | 
					
						
							| 
									
										
										
										
											2021-10-13 06:04:28 +08:00
										 |  |  |                 throw new OidcInvalidTokenException("Could not parse out a valid {$prop} within the provided token"); | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (empty($this->signature) || !is_string($this->signature)) { | 
					
						
							| 
									
										
										
										
											2021-10-16 23:01:59 +08:00
										 |  |  |             throw new OidcInvalidTokenException('Could not parse out a valid signature within the provided token'); | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Validate the signature of the given token and ensure it validates against the provided key. | 
					
						
							| 
									
										
										
										
											2021-10-16 23:01:59 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-10-13 06:04:28 +08:00
										 |  |  |      * @throws OidcInvalidTokenException | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function validateTokenSignature(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($this->header['alg'] !== 'RS256') { | 
					
						
							| 
									
										
										
										
											2021-10-13 06:04:28 +08:00
										 |  |  |             throw new OidcInvalidTokenException("Only RS256 signature validation is supported. Token reports using {$this->header['alg']}"); | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-16 23:01:59 +08:00
										 |  |  |         $parsedKeys = array_map(function ($key) { | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  |             try { | 
					
						
							| 
									
										
										
										
											2021-10-13 06:04:28 +08:00
										 |  |  |                 return new OidcJwtSigningKey($key); | 
					
						
							|  |  |  |             } catch (OidcInvalidKeyException $e) { | 
					
						
							| 
									
										
										
										
											2021-10-13 23:51:27 +08:00
										 |  |  |                 throw new OidcInvalidTokenException('Failed to read signing key with error: ' . $e->getMessage()); | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |         }, $this->keys); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $parsedKeys = array_filter($parsedKeys); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $contentToSign = $this->tokenParts[0] . '.' . $this->tokenParts[1]; | 
					
						
							| 
									
										
										
										
											2021-10-13 06:04:28 +08:00
										 |  |  |         /** @var OidcJwtSigningKey $parsedKey */ | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  |         foreach ($parsedKeys as $parsedKey) { | 
					
						
							|  |  |  |             if ($parsedKey->verify($contentToSign, $this->signature)) { | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-13 06:04:28 +08:00
										 |  |  |         throw new OidcInvalidTokenException('Token signature could not be validated using the provided keys'); | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Validate the claims of the token. | 
					
						
							| 
									
										
										
										
											2021-10-16 23:01:59 +08:00
										 |  |  |      * As per https://openid.net/specs/openid-connect-basic-1_0.html#IDTokenValidation.
 | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-10-13 06:04:28 +08:00
										 |  |  |      * @throws OidcInvalidTokenException | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-10-12 06:00:45 +08:00
										 |  |  |     protected function validateTokenClaims(string $clientId): void | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-10-12 06:00:45 +08:00
										 |  |  |         // 1. The Issuer Identifier for the OpenID Provider (which is typically obtained during Discovery)
 | 
					
						
							|  |  |  |         // MUST exactly match the value of the iss (issuer) Claim.
 | 
					
						
							|  |  |  |         if (empty($this->payload['iss']) || $this->issuer !== $this->payload['iss']) { | 
					
						
							| 
									
										
										
										
											2021-10-13 06:04:28 +08:00
										 |  |  |             throw new OidcInvalidTokenException('Missing or non-matching token issuer value'); | 
					
						
							| 
									
										
										
										
											2021-10-12 06:00:45 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // 2. The Client MUST validate that the aud (audience) Claim contains its client_id value registered
 | 
					
						
							|  |  |  |         // at the Issuer identified by the iss (issuer) Claim as an audience. The ID Token MUST be rejected
 | 
					
						
							|  |  |  |         // if the ID Token does not list the Client as a valid audience, or if it contains additional
 | 
					
						
							|  |  |  |         // audiences not trusted by the Client.
 | 
					
						
							|  |  |  |         if (empty($this->payload['aud'])) { | 
					
						
							| 
									
										
										
										
											2021-10-13 06:04:28 +08:00
										 |  |  |             throw new OidcInvalidTokenException('Missing token audience value'); | 
					
						
							| 
									
										
										
										
											2021-10-12 06:00:45 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $aud = is_string($this->payload['aud']) ? [$this->payload['aud']] : $this->payload['aud']; | 
					
						
							|  |  |  |         if (count($aud) !== 1) { | 
					
						
							| 
									
										
										
										
											2021-10-13 06:04:28 +08:00
										 |  |  |             throw new OidcInvalidTokenException('Token audience value has ' . count($aud) . ' values, Expected 1'); | 
					
						
							| 
									
										
										
										
											2021-10-12 06:00:45 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($aud[0] !== $clientId) { | 
					
						
							| 
									
										
										
										
											2021-10-13 06:04:28 +08:00
										 |  |  |             throw new OidcInvalidTokenException('Token audience value did not match the expected client_id'); | 
					
						
							| 
									
										
										
										
											2021-10-12 06:00:45 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // 3. If the ID Token contains multiple audiences, the Client SHOULD verify that an azp Claim is present.
 | 
					
						
							|  |  |  |         // NOTE: Addressed by enforcing a count of 1 above.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // 4. If an azp (authorized party) Claim is present, the Client SHOULD verify that its client_id
 | 
					
						
							|  |  |  |         // is the Claim Value.
 | 
					
						
							|  |  |  |         if (isset($this->payload['azp']) && $this->payload['azp'] !== $clientId) { | 
					
						
							| 
									
										
										
										
											2021-10-13 06:04:28 +08:00
										 |  |  |             throw new OidcInvalidTokenException('Token authorized party exists but does not match the expected client_id'); | 
					
						
							| 
									
										
										
										
											2021-10-12 06:00:45 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // 5. The current time MUST be before the time represented by the exp Claim
 | 
					
						
							|  |  |  |         // (possibly allowing for some small leeway to account for clock skew).
 | 
					
						
							|  |  |  |         if (empty($this->payload['exp'])) { | 
					
						
							| 
									
										
										
										
											2021-10-13 06:04:28 +08:00
										 |  |  |             throw new OidcInvalidTokenException('Missing token expiration time value'); | 
					
						
							| 
									
										
										
										
											2021-10-12 06:00:45 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $skewSeconds = 120; | 
					
						
							|  |  |  |         $now = time(); | 
					
						
							|  |  |  |         if ($now >= (intval($this->payload['exp']) + $skewSeconds)) { | 
					
						
							| 
									
										
										
										
											2021-10-13 06:04:28 +08:00
										 |  |  |             throw new OidcInvalidTokenException('Token has expired'); | 
					
						
							| 
									
										
										
										
											2021-10-12 06:00:45 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // 6. The iat Claim can be used to reject tokens that were issued too far away from the current time,
 | 
					
						
							|  |  |  |         // limiting the amount of time that nonces need to be stored to prevent attacks.
 | 
					
						
							|  |  |  |         // The acceptable range is Client specific.
 | 
					
						
							|  |  |  |         if (empty($this->payload['iat'])) { | 
					
						
							| 
									
										
										
										
											2021-10-13 06:04:28 +08:00
										 |  |  |             throw new OidcInvalidTokenException('Missing token issued at time value'); | 
					
						
							| 
									
										
										
										
											2021-10-12 06:00:45 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $dayAgo = time() - 86400; | 
					
						
							|  |  |  |         $iat = intval($this->payload['iat']); | 
					
						
							|  |  |  |         if ($iat > ($now + $skewSeconds) || $iat < $dayAgo) { | 
					
						
							| 
									
										
										
										
											2021-10-13 06:04:28 +08:00
										 |  |  |             throw new OidcInvalidTokenException('Token issue at time is not recent or is invalid'); | 
					
						
							| 
									
										
										
										
											2021-10-12 06:00:45 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // 7. If the acr Claim was requested, the Client SHOULD check that the asserted Claim Value is appropriate.
 | 
					
						
							|  |  |  |         // The meaning and processing of acr Claim Values is out of scope for this document.
 | 
					
						
							|  |  |  |         // NOTE: Not used for our case here. acr is not requested.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // 8. When a max_age request is made, the Client SHOULD check the auth_time Claim value and request
 | 
					
						
							|  |  |  |         // re-authentication if it determines too much time has elapsed since the last End-User authentication.
 | 
					
						
							|  |  |  |         // NOTE: Not used for our case here. A max_age request is not made.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Custom: Ensure the "sub" (Subject) Claim exists and has a value.
 | 
					
						
							|  |  |  |         if (empty($this->payload['sub'])) { | 
					
						
							| 
									
										
										
										
											2021-10-13 06:04:28 +08:00
										 |  |  |             throw new OidcInvalidTokenException('Missing token subject value'); | 
					
						
							| 
									
										
										
										
											2021-10-12 06:00:45 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-10-12 02:05:16 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-10-16 23:01:59 +08:00
										 |  |  | } |