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_login_group_sync()
414 'oidc.user_to_groups' => true,
415 'oidc.groups_claim' => 'groups',
416 'oidc.remove_from_groups' => false,
418 $roleA = Role::factory()->create(['display_name' => 'Wizards']);
419 $roleB = Role::factory()->create(['display_name' => 'ZooFolks', 'external_auth_id' => 'zookeepers']);
420 $roleC = Role::factory()->create(['display_name' => 'Another Role']);
422 $resp = $this->runLogin([
424 'sub' => 'benny1010101',
425 'groups' => ['Wizards', 'Zookeepers'],
427 $resp->assertRedirect('/');
429 /** @var User $user */
432 $this->assertTrue($user->hasRole($roleA->id));
433 $this->assertTrue($user->hasRole($roleB->id));
434 $this->assertFalse($user->hasRole($roleC->id));
437 public function test_login_group_sync_with_nested_groups_in_token()
440 'oidc.user_to_groups' => true,
441 'oidc.groups_claim' => 'my.custom.groups.attr',
442 'oidc.remove_from_groups' => false,
444 $roleA = Role::factory()->create(['display_name' => 'Wizards']);
446 $resp = $this->runLogin([
448 'sub' => 'benny1010101',
452 'attr' => ['Wizards'],
457 $resp->assertRedirect('/');
459 /** @var User $user */
461 $this->assertTrue($user->hasRole($roleA->id));
464 public function test_oidc_id_token_pre_validate_theme_event_without_return()
467 $callback = function (...$eventArgs) use (&$args) {
470 Theme::listen(ThemeEvents::OIDC_ID_TOKEN_PRE_VALIDATE, $callback);
472 $resp = $this->runLogin([
474 'sub' => 'benny1010101',
477 $resp->assertRedirect('/');
479 $this->assertDatabaseHas('users', [
480 'external_auth_id' => 'benny1010101',
483 $this->assertArrayHasKey('iss', $args[0]);
484 $this->assertArrayHasKey('sub', $args[0]);
485 $this->assertEquals('Benny', $args[0]['name']);
486 $this->assertEquals('benny1010101', $args[0]['sub']);
488 $this->assertArrayHasKey('access_token', $args[1]);
489 $this->assertArrayHasKey('expires_in', $args[1]);
490 $this->assertArrayHasKey('refresh_token', $args[1]);
493 public function test_oidc_id_token_pre_validate_theme_event_with_return()
495 $callback = function (...$eventArgs) {
496 return array_merge($eventArgs[0], [
498 'sub' => 'lenny1010101',
502 Theme::listen(ThemeEvents::OIDC_ID_TOKEN_PRE_VALIDATE, $callback);
504 $resp = $this->runLogin([
506 'sub' => 'benny1010101',
509 $resp->assertRedirect('/');
511 $this->assertDatabaseHas('users', [
513 'external_auth_id' => 'lenny1010101',
518 protected function withAutodiscovery()
521 'oidc.issuer' => OidcJwtHelper::defaultIssuer(),
522 'oidc.discover' => true,
523 'oidc.authorization_endpoint' => null,
524 'oidc.token_endpoint' => null,
525 'oidc.jwt_public_key' => null,
529 protected function runLogin($claimOverrides = []): TestResponse
531 $this->post('/oidc/login');
532 $state = session()->get('oidc_state');
533 $this->mockHttpClient([$this->getMockAuthorizationResponse($claimOverrides)]);
535 return $this->get('/oidc/callback?code=SplxlOBeZQQYbYS6WxSbIA&state=' . $state);
538 protected function getAutoDiscoveryResponse($responseOverrides = []): Response
540 return new Response(200, [
541 'Content-Type' => 'application/json',
542 'Cache-Control' => 'no-cache, no-store',
543 'Pragma' => 'no-cache',
544 ], json_encode(array_merge([
545 'token_endpoint' => OidcJwtHelper::defaultIssuer() . '/oidc/token',
546 'authorization_endpoint' => OidcJwtHelper::defaultIssuer() . '/oidc/authorize',
547 'jwks_uri' => OidcJwtHelper::defaultIssuer() . '/oidc/keys',
548 'issuer' => OidcJwtHelper::defaultIssuer(),
549 ], $responseOverrides)));
552 protected function getJwksResponse(): Response
554 return new Response(200, [
555 'Content-Type' => 'application/json',
556 'Cache-Control' => 'no-cache, no-store',
557 'Pragma' => 'no-cache',
560 OidcJwtHelper::publicJwkKeyArray(),
565 protected function getMockAuthorizationResponse($claimOverrides = []): Response
567 return new Response(200, [
568 'Content-Type' => 'application/json',
569 'Cache-Control' => 'no-cache, no-store',
570 'Pragma' => 'no-cache',
572 'access_token' => 'abc123',
573 'token_type' => 'Bearer',
574 'expires_in' => 3600,
575 'id_token' => OidcJwtHelper::idToken($claimOverrides),