5 use BookStack\Auth\Access\Oidc\OidcIdToken;
6 use BookStack\Auth\Access\Oidc\OidcInvalidTokenException;
7 use Tests\Helpers\OidcJwtHelper;
10 class OidcIdTokenTest extends TestCase
12 public function test_valid_token_passes_validation()
14 $token = new OidcIdToken(OidcJwtHelper::idToken(), OidcJwtHelper::defaultIssuer(), [
15 OidcJwtHelper::publicJwkKeyArray(),
18 $this->assertTrue($token->validate('xxyyzz.aaa.bbccdd.123'));
21 public function test_get_claim_returns_value_if_existing()
23 $token = new OidcIdToken(OidcJwtHelper::idToken(), OidcJwtHelper::defaultIssuer(), []);
24 $this->assertEquals('bscott@example.com', $token->getClaim('email'));
27 public function test_get_claim_returns_null_if_not_existing()
29 $token = new OidcIdToken(OidcJwtHelper::idToken(), OidcJwtHelper::defaultIssuer(), []);
30 $this->assertEquals(null, $token->getClaim('emails'));
33 public function test_get_all_claims_returns_all_payload_claims()
35 $defaultPayload = OidcJwtHelper::defaultPayload();
36 $token = new OidcIdToken(OidcJwtHelper::idToken($defaultPayload), OidcJwtHelper::defaultIssuer(), []);
37 $this->assertEquals($defaultPayload, $token->getAllClaims());
40 public function test_token_structure_error_cases()
42 $idToken = OidcJwtHelper::idToken();
43 $idTokenExploded = explode('.', $idToken);
45 $messagesAndTokenValues = [
46 ['Could not parse out a valid header within the provided token', ''],
47 ['Could not parse out a valid header within the provided token', 'cat'],
48 ['Could not parse out a valid payload within the provided token', $idTokenExploded[0]],
49 ['Could not parse out a valid payload within the provided token', $idTokenExploded[0] . '.' . 'dog'],
50 ['Could not parse out a valid signature within the provided token', $idTokenExploded[0] . '.' . $idTokenExploded[1]],
51 ['Could not parse out a valid signature within the provided token', $idTokenExploded[0] . '.' . $idTokenExploded[1] . '.' . '@$%'],
54 foreach ($messagesAndTokenValues as [$message, $tokenValue]) {
55 $token = new OidcIdToken($tokenValue, OidcJwtHelper::defaultIssuer(), []);
59 $token->validate('abc');
60 } catch (\Exception $exception) {
64 $this->assertInstanceOf(OidcInvalidTokenException::class, $err, $message);
65 $this->assertEquals($message, $err->getMessage());
69 public function test_error_thrown_if_token_signature_not_validated_from_no_keys()
71 $token = new OidcIdToken(OidcJwtHelper::idToken(), OidcJwtHelper::defaultIssuer(), []);
72 $this->expectException(OidcInvalidTokenException::class);
73 $this->expectExceptionMessage('Token signature could not be validated using the provided keys');
74 $token->validate('abc');
77 public function test_error_thrown_if_token_signature_not_validated_from_non_matching_key()
79 $token = new OidcIdToken(OidcJwtHelper::idToken(), OidcJwtHelper::defaultIssuer(), [
80 array_merge(OidcJwtHelper::publicJwkKeyArray(), [
81 'n' => 'iqK-1QkICMf_cusNLpeNnN-bhT0-9WLBvzgwKLALRbrevhdi5ttrLHIQshaSL0DklzfyG2HWRmAnJ9Q7sweEjuRiiqRcSUZbYu8cIv2hLWYu7K_NH67D2WUjl0EnoHEuiVLsZhQe1CmdyLdx087j5nWkd64K49kXRSdxFQUlj8W3NeK3CjMEUdRQ3H4RZzJ4b7uuMiFA29S2ZhMNG20NPbkUVsFL-jiwTd10KSsPT8yBYipI9O7mWsUWt_8KZs1y_vpM_k3SyYihnWpssdzDm1uOZ8U3mzFr1xsLAO718GNUSXk6npSDzLl59HEqa6zs4O9awO2qnSHvcmyELNk31w',
84 $this->expectException(OidcInvalidTokenException::class);
85 $this->expectExceptionMessage('Token signature could not be validated using the provided keys');
86 $token->validate('abc');
89 public function test_error_thrown_if_invalid_key_provided()
91 $token = new OidcIdToken(OidcJwtHelper::idToken(), OidcJwtHelper::defaultIssuer(), ['url://example.com']);
92 $this->expectException(OidcInvalidTokenException::class);
93 $this->expectExceptionMessage('Unexpected type of key value provided');
94 $token->validate('abc');
97 public function test_error_thrown_if_token_algorithm_is_not_rs256()
99 $token = new OidcIdToken(OidcJwtHelper::idToken([], ['alg' => 'HS256']), OidcJwtHelper::defaultIssuer(), []);
100 $this->expectException(OidcInvalidTokenException::class);
101 $this->expectExceptionMessage('Only RS256 signature validation is supported. Token reports using HS256');
102 $token->validate('abc');
105 public function test_token_claim_error_cases()
107 /** @var array<array{0: string: 1: array}> $claimOverridesByErrorMessage */
108 $claimOverridesByErrorMessage = [
109 // 1. iss claim present
110 ['Missing or non-matching token issuer value', ['iss' => null]],
111 // 1. iss claim matches provided issuer
112 ['Missing or non-matching token issuer value', ['iss' => 'https://auth.example.co.uk']],
113 // 2. aud claim present
114 ['Missing token audience value', ['aud' => null]],
115 // 2. aud claim validates all values against those expected (Only expect single)
116 ['Token audience value has 2 values, Expected 1', ['aud' => ['abc', 'def']]],
117 // 2. aud claim matches client id
118 ['Token audience value did not match the expected client_id', ['aud' => 'xxyyzz.aaa.bbccdd.456']],
119 // 4. azp claim matches client id if present
120 ['Token authorized party exists but does not match the expected client_id', ['azp' => 'xxyyzz.aaa.bbccdd.456']],
121 // 5. exp claim present
122 ['Missing token expiration time value', ['exp' => null]],
123 // 5. exp claim not expired
124 ['Token has expired', ['exp' => time() - 360]],
125 // 6. iat claim present
126 ['Missing token issued at time value', ['iat' => null]],
127 // 6. iat claim too far in the future
128 ['Token issue at time is not recent or is invalid', ['iat' => time() + 600]],
129 // 6. iat claim too far in the past
130 ['Token issue at time is not recent or is invalid', ['iat' => time() - 172800]],
132 // Custom: sub is present
133 ['Missing token subject value', ['sub' => null]],
136 foreach ($claimOverridesByErrorMessage as [$message, $overrides]) {
137 $token = new OidcIdToken(OidcJwtHelper::idToken($overrides), OidcJwtHelper::defaultIssuer(), [
138 OidcJwtHelper::publicJwkKeyArray(),
144 $token->validate('xxyyzz.aaa.bbccdd.123');
145 } catch (\Exception $exception) {
149 $this->assertInstanceOf(OidcInvalidTokenException::class, $err, $message);
150 $this->assertEquals($message, $err->getMessage());
154 public function test_keys_can_be_a_local_file_reference_to_pem_key()
157 $testFilePath = 'file://' . stream_get_meta_data($file)['uri'];
158 file_put_contents($testFilePath, OidcJwtHelper::publicPemKey());
159 $token = new OidcIdToken(OidcJwtHelper::idToken(), OidcJwtHelper::defaultIssuer(), [
163 $this->assertTrue($token->validate('xxyyzz.aaa.bbccdd.123'));
164 unlink($testFilePath);