5 use BookStack\Activity\ActivityType;
6 use BookStack\Facades\Theme;
7 use BookStack\Theming\ThemeEvents;
8 use BookStack\Users\Models\Role;
9 use BookStack\Users\Models\User;
10 use GuzzleHttp\Psr7\Response;
11 use Illuminate\Testing\TestResponse;
12 use Tests\Helpers\OidcJwtHelper;
15 class OidcTest extends TestCase
17 protected string $keyFilePath;
20 protected function setUp(): void
23 // Set default config for OpenID Connect
25 $this->keyFile = tmpfile();
26 $this->keyFilePath = 'file://' . stream_get_meta_data($this->keyFile)['uri'];
27 file_put_contents($this->keyFilePath, OidcJwtHelper::publicPemKey());
30 'auth.method' => 'oidc',
31 'auth.defaults.guard' => 'oidc',
32 'oidc.name' => 'SingleSignOn-Testing',
33 'oidc.display_name_claims' => 'name',
34 'oidc.client_id' => OidcJwtHelper::defaultClientId(),
35 'oidc.client_secret' => 'testpass',
36 'oidc.jwt_public_key' => $this->keyFilePath,
37 'oidc.issuer' => OidcJwtHelper::defaultIssuer(),
38 'oidc.authorization_endpoint' => 'https://oidc.local/auth',
39 'oidc.token_endpoint' => 'https://oidc.local/token',
40 'oidc.discover' => false,
41 'oidc.dump_user_details' => false,
42 'oidc.additional_scopes' => '',
43 'oidc.user_to_groups' => false,
44 'oidc.groups_claim' => 'group',
45 'oidc.remove_from_groups' => false,
46 'oidc.external_id_claim' => 'sub',
50 protected function tearDown(): void
53 if (file_exists($this->keyFilePath)) {
54 unlink($this->keyFilePath);
58 public function test_login_option_shows_on_login_page()
60 $req = $this->get('/login');
61 $req->assertSeeText('SingleSignOn-Testing');
62 $this->withHtml($req)->assertElementExists('form[action$="/oidc/login"][method=POST] button');
65 public function test_oidc_routes_are_only_active_if_oidc_enabled()
67 config()->set(['auth.method' => 'standard']);
68 $routes = ['/login' => 'post', '/callback' => 'get'];
69 foreach ($routes as $uri => $method) {
70 $req = $this->call($method, '/oidc' . $uri);
71 $this->assertPermissionError($req);
75 public function test_forgot_password_routes_inaccessible()
77 $resp = $this->get('/password/email');
78 $this->assertPermissionError($resp);
80 $resp = $this->post('/password/email');
81 $this->assertPermissionError($resp);
83 $resp = $this->get('/password/reset/abc123');
84 $this->assertPermissionError($resp);
86 $resp = $this->post('/password/reset');
87 $this->assertPermissionError($resp);
90 public function test_standard_login_routes_inaccessible()
92 $resp = $this->post('/login');
93 $this->assertPermissionError($resp);
96 public function test_logout_route_functions()
98 $this->actingAs($this->users->editor());
99 $this->post('/logout');
100 $this->assertFalse(auth()->check());
103 public function test_user_invite_routes_inaccessible()
105 $resp = $this->get('/register/invite/abc123');
106 $this->assertPermissionError($resp);
108 $resp = $this->post('/register/invite/abc123');
109 $this->assertPermissionError($resp);
112 public function test_user_register_routes_inaccessible()
114 $resp = $this->get('/register');
115 $this->assertPermissionError($resp);
117 $resp = $this->post('/register');
118 $this->assertPermissionError($resp);
121 public function test_login()
123 $req = $this->post('/oidc/login');
124 $redirect = $req->headers->get('location');
126 $this->assertStringStartsWith('https://oidc.local/auth', $redirect, 'Login redirects to SSO location');
127 $this->assertFalse($this->isAuthenticated());
128 $this->assertStringContainsString('scope=openid%20profile%20email', $redirect);
129 $this->assertStringContainsString('client_id=' . OidcJwtHelper::defaultClientId(), $redirect);
130 $this->assertStringContainsString('redirect_uri=' . urlencode(url('/oidc/callback')), $redirect);
133 public function test_login_success_flow()
136 $this->post('/oidc/login');
137 $state = session()->get('oidc_state');
139 $transactions = $this->mockHttpClient([$this->getMockAuthorizationResponse([
141 'sub' => 'benny1010101',
144 // Callback from auth provider
145 // App calls token endpoint to get id token
146 $resp = $this->get('/oidc/callback?code=SplxlOBeZQQYbYS6WxSbIA&state=' . $state);
147 $resp->assertRedirect('/');
148 $this->assertEquals(1, $transactions->requestCount());
149 $tokenRequest = $transactions->latestRequest();
150 $this->assertEquals('https://oidc.local/token', (string) $tokenRequest->getUri());
151 $this->assertEquals('POST', $tokenRequest->getMethod());
152 $this->assertEquals('Basic ' . base64_encode(OidcJwtHelper::defaultClientId() . ':testpass'), $tokenRequest->getHeader('Authorization')[0]);
153 $this->assertStringContainsString('grant_type=authorization_code', $tokenRequest->getBody());
154 $this->assertStringContainsString('code=SplxlOBeZQQYbYS6WxSbIA', $tokenRequest->getBody());
155 $this->assertStringContainsString('redirect_uri=' . urlencode(url('/oidc/callback')), $tokenRequest->getBody());
157 $this->assertTrue(auth()->check());
158 $this->assertDatabaseHas('users', [
160 'external_auth_id' => 'benny1010101',
161 'email_confirmed' => false,
165 $this->assertActivityExists(ActivityType::AUTH_LOGIN, null, "oidc; ({$user->id}) Barry Scott");
168 public function test_login_uses_custom_additional_scopes_if_defined()
171 'oidc.additional_scopes' => 'groups, badgers',
174 $redirect = $this->post('/oidc/login')->headers->get('location');
176 $this->assertStringContainsString('scope=openid%20profile%20email%20groups%20badgers', $redirect);
179 public function test_callback_fails_if_no_state_present_or_matching()
181 $this->get('/oidc/callback?code=SplxlOBeZQQYbYS6WxSbIA&state=abc124');
182 $this->assertSessionError('Login using SingleSignOn-Testing failed, system did not provide successful authorization');
184 $this->post('/oidc/login');
185 $this->get('/oidc/callback?code=SplxlOBeZQQYbYS6WxSbIA&state=abc124');
186 $this->assertSessionError('Login using SingleSignOn-Testing failed, system did not provide successful authorization');
189 public function test_dump_user_details_option_outputs_as_expected()
191 config()->set('oidc.dump_user_details', true);
193 $resp = $this->runLogin([
198 $resp->assertStatus(200);
202 'iss' => OidcJwtHelper::defaultIssuer(),
203 'aud' => OidcJwtHelper::defaultClientId(),
205 $this->assertFalse(auth()->check());
208 public function test_auth_fails_if_no_email_exists_in_user_data()
215 $this->assertSessionError('Could not find an email address, for this user, in the data provided by the external authentication system');
218 public function test_auth_fails_if_already_logged_in()
227 $this->assertSessionError('Already logged in');
230 public function test_auth_login_as_existing_user()
232 $editor = $this->users->editor();
233 $editor->external_auth_id = 'benny505';
236 $this->assertFalse(auth()->check());
243 $this->assertTrue(auth()->check());
244 $this->assertEquals($editor->id, auth()->user()->id);
247 public function test_auth_login_as_existing_user_email_with_different_auth_id_fails()
249 $editor = $this->users->editor();
250 $editor->external_auth_id = 'editor101';
253 $this->assertFalse(auth()->check());
255 $resp = $this->runLogin([
256 'email' => $editor->email,
259 $resp = $this->followRedirects($resp);
261 $resp->assertSeeText('A user with the email ' . $editor->email . ' already exists but with different credentials.');
262 $this->assertFalse(auth()->check());
265 public function test_auth_login_with_invalid_token_fails()
267 $resp = $this->runLogin([
270 $resp = $this->followRedirects($resp);
272 $resp->assertSeeText('ID token validate failed with error: Missing token subject value');
273 $this->assertFalse(auth()->check());
276 public function test_auth_login_with_autodiscovery()
278 $this->withAutodiscovery();
280 $transactions = $this->mockHttpClient([
281 $this->getAutoDiscoveryResponse(),
282 $this->getJwksResponse(),
285 $this->assertFalse(auth()->check());
289 $this->assertTrue(auth()->check());
291 $discoverRequest = $transactions->requestAt(0);
292 $keysRequest = $transactions->requestAt(1);
293 $this->assertEquals('GET', $keysRequest->getMethod());
294 $this->assertEquals('GET', $discoverRequest->getMethod());
295 $this->assertEquals(OidcJwtHelper::defaultIssuer() . '/.well-known/openid-configuration', $discoverRequest->getUri());
296 $this->assertEquals(OidcJwtHelper::defaultIssuer() . '/oidc/keys', $keysRequest->getUri());
299 public function test_auth_fails_if_autodiscovery_fails()
301 $this->withAutodiscovery();
302 $this->mockHttpClient([
303 new Response(404, [], 'Not found'),
306 $resp = $this->followRedirects($this->runLogin());
307 $this->assertFalse(auth()->check());
308 $resp->assertSeeText('Login using SingleSignOn-Testing failed, system did not provide successful authorization');
311 public function test_autodiscovery_calls_are_cached()
313 $this->withAutodiscovery();
315 $transactions = $this->mockHttpClient([
316 $this->getAutoDiscoveryResponse(),
317 $this->getJwksResponse(),
318 $this->getAutoDiscoveryResponse([
319 'issuer' => 'https://auto.example.com',
321 $this->getJwksResponse(),
325 $this->post('/oidc/login');
326 $this->assertEquals(2, $transactions->requestCount());
327 // Second run, hits cache
328 $this->post('/oidc/login');
329 $this->assertEquals(2, $transactions->requestCount());
331 // Third run, different issuer, new cache key
332 config()->set(['oidc.issuer' => 'https://auto.example.com']);
333 $this->post('/oidc/login');
334 $this->assertEquals(4, $transactions->requestCount());
337 public function test_auth_login_with_autodiscovery_with_keys_that_do_not_have_alg_property()
339 $this->withAutodiscovery();
341 $keyArray = OidcJwtHelper::publicJwkKeyArray();
342 unset($keyArray['alg']);
344 $this->mockHttpClient([
345 $this->getAutoDiscoveryResponse(),
347 'Content-Type' => 'application/json',
348 'Cache-Control' => 'no-cache, no-store',
349 'Pragma' => 'no-cache',
357 $this->assertFalse(auth()->check());
359 $this->assertTrue(auth()->check());
362 public function test_auth_login_with_autodiscovery_with_keys_that_do_not_have_use_property()
364 // Based on reading the OIDC discovery spec:
365 // > This contains the signing key(s) the RP uses to validate signatures from the OP. The JWK Set MAY also
366 // > contain the Server's encryption key(s), which are used by RPs to encrypt requests to the Server. When
367 // > both signing and encryption keys are made available, a use (Key Use) parameter value is REQUIRED for all
368 // > keys in the referenced JWK Set to indicate each key's intended usage.
369 // We can assume that keys without use are intended for signing.
370 $this->withAutodiscovery();
372 $keyArray = OidcJwtHelper::publicJwkKeyArray();
373 unset($keyArray['use']);
375 $this->mockHttpClient([
376 $this->getAutoDiscoveryResponse(),
378 'Content-Type' => 'application/json',
379 'Cache-Control' => 'no-cache, no-store',
380 'Pragma' => 'no-cache',
388 $this->assertFalse(auth()->check());
390 $this->assertTrue(auth()->check());
393 public function test_auth_uses_configured_external_id_claim_option()
396 'oidc.external_id_claim' => 'super_awesome_id',
399 $resp = $this->runLogin([
401 'sub' => 'benny1010101',
402 'super_awesome_id' => 'xXBennyTheGeezXx',
404 $resp->assertRedirect('/');
406 /** @var User $user */
408 $this->assertEquals('xXBennyTheGeezXx', $user->external_auth_id);
411 public function test_auth_uses_mulitple_display_name_claims_if_configured()
413 config()->set(['oidc.display_name_claims' => 'first_name|last_name']);
417 'sub' => 'benny1010101',
418 'first_name' => 'Benny',
419 'last_name' => 'Jenkins'
422 $this->assertDatabaseHas('users', [
423 'name' => 'Benny Jenkins',
428 public function test_login_group_sync()
431 'oidc.user_to_groups' => true,
432 'oidc.groups_claim' => 'groups',
433 'oidc.remove_from_groups' => false,
435 $roleA = Role::factory()->create(['display_name' => 'Wizards']);
436 $roleB = Role::factory()->create(['display_name' => 'ZooFolks', 'external_auth_id' => 'zookeepers']);
437 $roleC = Role::factory()->create(['display_name' => 'Another Role']);
439 $resp = $this->runLogin([
441 'sub' => 'benny1010101',
442 'groups' => ['Wizards', 'Zookeepers'],
444 $resp->assertRedirect('/');
446 /** @var User $user */
449 $this->assertTrue($user->hasRole($roleA->id));
450 $this->assertTrue($user->hasRole($roleB->id));
451 $this->assertFalse($user->hasRole($roleC->id));
454 public function test_login_group_sync_with_nested_groups_in_token()
457 'oidc.user_to_groups' => true,
458 'oidc.groups_claim' => 'my.custom.groups.attr',
459 'oidc.remove_from_groups' => false,
461 $roleA = Role::factory()->create(['display_name' => 'Wizards']);
463 $resp = $this->runLogin([
465 'sub' => 'benny1010101',
469 'attr' => ['Wizards'],
474 $resp->assertRedirect('/');
476 /** @var User $user */
478 $this->assertTrue($user->hasRole($roleA->id));
481 public function test_oidc_id_token_pre_validate_theme_event_without_return()
484 $callback = function (...$eventArgs) use (&$args) {
487 Theme::listen(ThemeEvents::OIDC_ID_TOKEN_PRE_VALIDATE, $callback);
489 $resp = $this->runLogin([
491 'sub' => 'benny1010101',
494 $resp->assertRedirect('/');
496 $this->assertDatabaseHas('users', [
497 'external_auth_id' => 'benny1010101',
500 $this->assertArrayHasKey('iss', $args[0]);
501 $this->assertArrayHasKey('sub', $args[0]);
502 $this->assertEquals('Benny', $args[0]['name']);
503 $this->assertEquals('benny1010101', $args[0]['sub']);
505 $this->assertArrayHasKey('access_token', $args[1]);
506 $this->assertArrayHasKey('expires_in', $args[1]);
507 $this->assertArrayHasKey('refresh_token', $args[1]);
510 public function test_oidc_id_token_pre_validate_theme_event_with_return()
512 $callback = function (...$eventArgs) {
513 return array_merge($eventArgs[0], [
515 'sub' => 'lenny1010101',
519 Theme::listen(ThemeEvents::OIDC_ID_TOKEN_PRE_VALIDATE, $callback);
521 $resp = $this->runLogin([
523 'sub' => 'benny1010101',
526 $resp->assertRedirect('/');
528 $this->assertDatabaseHas('users', [
530 'external_auth_id' => 'lenny1010101',
535 protected function withAutodiscovery()
538 'oidc.issuer' => OidcJwtHelper::defaultIssuer(),
539 'oidc.discover' => true,
540 'oidc.authorization_endpoint' => null,
541 'oidc.token_endpoint' => null,
542 'oidc.jwt_public_key' => null,
546 protected function runLogin($claimOverrides = []): TestResponse
548 $this->post('/oidc/login');
549 $state = session()->get('oidc_state');
550 $this->mockHttpClient([$this->getMockAuthorizationResponse($claimOverrides)]);
552 return $this->get('/oidc/callback?code=SplxlOBeZQQYbYS6WxSbIA&state=' . $state);
555 protected function getAutoDiscoveryResponse($responseOverrides = []): Response
557 return new Response(200, [
558 'Content-Type' => 'application/json',
559 'Cache-Control' => 'no-cache, no-store',
560 'Pragma' => 'no-cache',
561 ], json_encode(array_merge([
562 'token_endpoint' => OidcJwtHelper::defaultIssuer() . '/oidc/token',
563 'authorization_endpoint' => OidcJwtHelper::defaultIssuer() . '/oidc/authorize',
564 'jwks_uri' => OidcJwtHelper::defaultIssuer() . '/oidc/keys',
565 'issuer' => OidcJwtHelper::defaultIssuer(),
566 ], $responseOverrides)));
569 protected function getJwksResponse(): Response
571 return new Response(200, [
572 'Content-Type' => 'application/json',
573 'Cache-Control' => 'no-cache, no-store',
574 'Pragma' => 'no-cache',
577 OidcJwtHelper::publicJwkKeyArray(),
582 protected function getMockAuthorizationResponse($claimOverrides = []): Response
584 return new Response(200, [
585 'Content-Type' => 'application/json',
586 'Cache-Control' => 'no-cache, no-store',
587 'Pragma' => 'no-cache',
589 'access_token' => 'abc123',
590 'token_type' => 'Bearer',
591 'expires_in' => 3600,
592 'id_token' => OidcJwtHelper::idToken($claimOverrides),