3 namespace BookStack\Auth\Access\OpenIdConnect;
5 class OpenIdConnectIdToken
23 * @var array[]|string[]
35 protected $tokenParts = [];
37 public function __construct(string $token, string $issuer, array $keys)
40 $this->issuer = $issuer;
45 * Parse the token content into its components.
47 protected function parse(string $token): void
49 $this->tokenParts = explode('.', $token);
50 $this->header = $this->parseEncodedTokenPart($this->tokenParts[0]);
51 $this->payload = $this->parseEncodedTokenPart($this->tokenParts[1] ?? '');
52 $this->signature = $this->base64UrlDecode($this->tokenParts[2] ?? '') ?: '';
56 * Parse a Base64-JSON encoded token part.
57 * Returns the data as a key-value array or empty array upon error.
59 protected function parseEncodedTokenPart(string $part): array
61 $json = $this->base64UrlDecode($part) ?: '{}';
62 $decoded = json_decode($json, true);
63 return is_array($decoded) ? $decoded : [];
67 * Base64URL decode. Needs some character conversions to be compatible
68 * with PHP's default base64 handling.
70 protected function base64UrlDecode(string $encoded): string
72 return base64_decode(strtr($encoded, '-_', '+/'));
76 * Validate all possible parts of the id token.
77 * @throws InvalidTokenException
79 public function validate()
81 $this->validateTokenStructure();
82 $this->validateTokenSignature();
83 $this->validateTokenClaims();
87 * Validate the structure of the given token and ensure we have the required pieces.
88 * As per https://datatracker.ietf.org/doc/html/rfc7519#section-7.2
89 * @throws InvalidTokenException
91 protected function validateTokenStructure(): void
93 foreach (['header', 'payload'] as $prop) {
94 if (empty($this->$prop) || !is_array($this->$prop)) {
95 throw new InvalidTokenException("Could not parse out a valid {$prop} within the provided token");
99 if (empty($this->signature) || !is_string($this->signature)) {
100 throw new InvalidTokenException("Could not parse out a valid signature within the provided token");
105 * Validate the signature of the given token and ensure it validates against the provided key.
106 * @throws InvalidTokenException
108 protected function validateTokenSignature(): void
110 if ($this->header['alg'] !== 'RS256') {
111 throw new InvalidTokenException("Only RS256 signature validation is supported. Token reports using {$this->header['alg']}");
114 $parsedKeys = array_map(function($key) {
116 return new JwtSigningKey($key);
117 } catch (InvalidKeyException $e) {
122 $parsedKeys = array_filter($parsedKeys);
124 $contentToSign = $this->tokenParts[0] . '.' . $this->tokenParts[1];
125 foreach ($parsedKeys as $parsedKey) {
126 if ($parsedKey->verify($contentToSign, $this->signature)) {
131 throw new InvalidTokenException('Token signature could not be validated using the provided keys.');
135 * Validate the claims of the token.
136 * As per https://openid.net/specs/openid-connect-basic-1_0.html#IDTokenValidation
138 protected function validateTokenClaims(): void