5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\Role;
7 use BookStack\Auth\User;
8 use GuzzleHttp\Psr7\Request;
9 use GuzzleHttp\Psr7\Response;
10 use Illuminate\Testing\TestResponse;
11 use Tests\Helpers\OidcJwtHelper;
14 class OidcTest extends TestCase
16 protected string $keyFilePath;
19 protected function setUp(): void
22 // Set default config for OpenID Connect
24 $this->keyFile = tmpfile();
25 $this->keyFilePath = 'file://' . stream_get_meta_data($this->keyFile)['uri'];
26 file_put_contents($this->keyFilePath, OidcJwtHelper::publicPemKey());
29 'auth.method' => 'oidc',
30 'auth.defaults.guard' => 'oidc',
31 'oidc.name' => 'SingleSignOn-Testing',
32 'oidc.display_name_claims' => ['name'],
33 'oidc.client_id' => OidcJwtHelper::defaultClientId(),
34 'oidc.client_secret' => 'testpass',
35 'oidc.jwt_public_key' => $this->keyFilePath,
36 'oidc.issuer' => OidcJwtHelper::defaultIssuer(),
37 'oidc.authorization_endpoint' => 'https://oidc.local/auth',
38 'oidc.token_endpoint' => 'https://oidc.local/token',
39 'oidc.discover' => false,
40 'oidc.dump_user_details' => false,
41 'oidc.additional_scopes' => '',
42 'oidc.user_to_groups' => false,
43 'oidc.groups_claim' => 'group',
44 'oidc.remove_from_groups' => false,
45 'oidc.external_id_claim' => 'sub',
49 protected function tearDown(): void
52 if (file_exists($this->keyFilePath)) {
53 unlink($this->keyFilePath);
57 public function test_login_option_shows_on_login_page()
59 $req = $this->get('/login');
60 $req->assertSeeText('SingleSignOn-Testing');
61 $this->withHtml($req)->assertElementExists('form[action$="/oidc/login"][method=POST] button');
64 public function test_oidc_routes_are_only_active_if_oidc_enabled()
66 config()->set(['auth.method' => 'standard']);
67 $routes = ['/login' => 'post', '/callback' => 'get'];
68 foreach ($routes as $uri => $method) {
69 $req = $this->call($method, '/oidc' . $uri);
70 $this->assertPermissionError($req);
74 public function test_forgot_password_routes_inaccessible()
76 $resp = $this->get('/password/email');
77 $this->assertPermissionError($resp);
79 $resp = $this->post('/password/email');
80 $this->assertPermissionError($resp);
82 $resp = $this->get('/password/reset/abc123');
83 $this->assertPermissionError($resp);
85 $resp = $this->post('/password/reset');
86 $this->assertPermissionError($resp);
89 public function test_standard_login_routes_inaccessible()
91 $resp = $this->post('/login');
92 $this->assertPermissionError($resp);
95 public function test_logout_route_functions()
97 $this->actingAs($this->users->editor());
98 $this->post('/logout');
99 $this->assertFalse(auth()->check());
102 public function test_user_invite_routes_inaccessible()
104 $resp = $this->get('/register/invite/abc123');
105 $this->assertPermissionError($resp);
107 $resp = $this->post('/register/invite/abc123');
108 $this->assertPermissionError($resp);
111 public function test_user_register_routes_inaccessible()
113 $resp = $this->get('/register');
114 $this->assertPermissionError($resp);
116 $resp = $this->post('/register');
117 $this->assertPermissionError($resp);
120 public function test_login()
122 $req = $this->post('/oidc/login');
123 $redirect = $req->headers->get('location');
125 $this->assertStringStartsWith('https://oidc.local/auth', $redirect, 'Login redirects to SSO location');
126 $this->assertFalse($this->isAuthenticated());
127 $this->assertStringContainsString('scope=openid%20profile%20email', $redirect);
128 $this->assertStringContainsString('client_id=' . OidcJwtHelper::defaultClientId(), $redirect);
129 $this->assertStringContainsString('redirect_uri=' . urlencode(url('/oidc/callback')), $redirect);
132 public function test_login_success_flow()
135 $this->post('/oidc/login');
136 $state = session()->get('oidc_state');
138 $transactions = &$this->mockHttpClient([$this->getMockAuthorizationResponse([
140 'sub' => 'benny1010101',
143 // Callback from auth provider
144 // App calls token endpoint to get id token
145 $resp = $this->get('/oidc/callback?code=SplxlOBeZQQYbYS6WxSbIA&state=' . $state);
146 $resp->assertRedirect('/');
147 $this->assertCount(1, $transactions);
148 /** @var Request $tokenRequest */
149 $tokenRequest = $transactions[0]['request'];
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());
290 /** @var Request $discoverRequest */
291 $discoverRequest = $transactions[0]['request'];
292 /** @var Request $discoverRequest */
293 $keysRequest = $transactions[1]['request'];
295 $this->assertEquals('GET', $keysRequest->getMethod());
296 $this->assertEquals('GET', $discoverRequest->getMethod());
297 $this->assertEquals(OidcJwtHelper::defaultIssuer() . '/.well-known/openid-configuration', $discoverRequest->getUri());
298 $this->assertEquals(OidcJwtHelper::defaultIssuer() . '/oidc/keys', $keysRequest->getUri());
301 public function test_auth_fails_if_autodiscovery_fails()
303 $this->withAutodiscovery();
304 $this->mockHttpClient([
305 new Response(404, [], 'Not found'),
308 $resp = $this->followRedirects($this->runLogin());
309 $this->assertFalse(auth()->check());
310 $resp->assertSeeText('Login using SingleSignOn-Testing failed, system did not provide successful authorization');
313 public function test_autodiscovery_calls_are_cached()
315 $this->withAutodiscovery();
317 $transactions = &$this->mockHttpClient([
318 $this->getAutoDiscoveryResponse(),
319 $this->getJwksResponse(),
320 $this->getAutoDiscoveryResponse([
321 'issuer' => 'https://auto.example.com',
323 $this->getJwksResponse(),
327 $this->post('/oidc/login');
328 $this->assertCount(2, $transactions);
329 // Second run, hits cache
330 $this->post('/oidc/login');
331 $this->assertCount(2, $transactions);
333 // Third run, different issuer, new cache key
334 config()->set(['oidc.issuer' => 'https://auto.example.com']);
335 $this->post('/oidc/login');
336 $this->assertCount(4, $transactions);
339 public function test_auth_login_with_autodiscovery_with_keys_that_do_not_have_alg_property()
341 $this->withAutodiscovery();
343 $keyArray = OidcJwtHelper::publicJwkKeyArray();
344 unset($keyArray['alg']);
346 $this->mockHttpClient([
347 $this->getAutoDiscoveryResponse(),
349 'Content-Type' => 'application/json',
350 'Cache-Control' => 'no-cache, no-store',
351 'Pragma' => 'no-cache',
359 $this->assertFalse(auth()->check());
361 $this->assertTrue(auth()->check());
364 public function test_auth_login_with_autodiscovery_with_keys_that_do_not_have_use_property()
366 // Based on reading the OIDC discovery spec:
367 // > This contains the signing key(s) the RP uses to validate signatures from the OP. The JWK Set MAY also
368 // > contain the Server's encryption key(s), which are used by RPs to encrypt requests to the Server. When
369 // > both signing and encryption keys are made available, a use (Key Use) parameter value is REQUIRED for all
370 // > keys in the referenced JWK Set to indicate each key's intended usage.
371 // We can assume that keys without use are intended for signing.
372 $this->withAutodiscovery();
374 $keyArray = OidcJwtHelper::publicJwkKeyArray();
375 unset($keyArray['use']);
377 $this->mockHttpClient([
378 $this->getAutoDiscoveryResponse(),
380 'Content-Type' => 'application/json',
381 'Cache-Control' => 'no-cache, no-store',
382 'Pragma' => 'no-cache',
390 $this->assertFalse(auth()->check());
392 $this->assertTrue(auth()->check());
395 public function test_auth_uses_configured_external_id_claim_option()
398 'oidc.external_id_claim' => 'super_awesome_id',
400 $roleA = Role::factory()->create(['display_name' => 'Wizards']);
402 $resp = $this->runLogin([
404 'sub' => 'benny1010101',
405 'super_awesome_id' => 'xXBennyTheGeezXx',
407 $resp->assertRedirect('/');
409 /** @var User $user */
411 $this->assertEquals('xXBennyTheGeezXx', $user->external_auth_id);
414 public function test_login_group_sync()
417 'oidc.user_to_groups' => true,
418 'oidc.groups_claim' => 'groups',
419 'oidc.remove_from_groups' => false,
421 $roleA = Role::factory()->create(['display_name' => 'Wizards']);
422 $roleB = Role::factory()->create(['display_name' => 'ZooFolks', 'external_auth_id' => 'zookeepers']);
423 $roleC = Role::factory()->create(['display_name' => 'Another Role']);
425 $resp = $this->runLogin([
427 'sub' => 'benny1010101',
428 'groups' => ['Wizards', 'Zookeepers'],
430 $resp->assertRedirect('/');
432 /** @var User $user */
435 $this->assertTrue($user->hasRole($roleA->id));
436 $this->assertTrue($user->hasRole($roleB->id));
437 $this->assertFalse($user->hasRole($roleC->id));
440 public function test_login_group_sync_with_nested_groups_in_token()
443 'oidc.user_to_groups' => true,
444 'oidc.groups_claim' => 'my.custom.groups.attr',
445 'oidc.remove_from_groups' => false,
447 $roleA = Role::factory()->create(['display_name' => 'Wizards']);
449 $resp = $this->runLogin([
451 'sub' => 'benny1010101',
455 'attr' => ['Wizards'],
460 $resp->assertRedirect('/');
462 /** @var User $user */
464 $this->assertTrue($user->hasRole($roleA->id));
467 protected function withAutodiscovery()
470 'oidc.issuer' => OidcJwtHelper::defaultIssuer(),
471 'oidc.discover' => true,
472 'oidc.authorization_endpoint' => null,
473 'oidc.token_endpoint' => null,
474 'oidc.jwt_public_key' => null,
478 protected function runLogin($claimOverrides = []): TestResponse
480 $this->post('/oidc/login');
481 $state = session()->get('oidc_state');
482 $this->mockHttpClient([$this->getMockAuthorizationResponse($claimOverrides)]);
484 return $this->get('/oidc/callback?code=SplxlOBeZQQYbYS6WxSbIA&state=' . $state);
487 protected function getAutoDiscoveryResponse($responseOverrides = []): Response
489 return new Response(200, [
490 'Content-Type' => 'application/json',
491 'Cache-Control' => 'no-cache, no-store',
492 'Pragma' => 'no-cache',
493 ], json_encode(array_merge([
494 'token_endpoint' => OidcJwtHelper::defaultIssuer() . '/oidc/token',
495 'authorization_endpoint' => OidcJwtHelper::defaultIssuer() . '/oidc/authorize',
496 'jwks_uri' => OidcJwtHelper::defaultIssuer() . '/oidc/keys',
497 'issuer' => OidcJwtHelper::defaultIssuer(),
498 ], $responseOverrides)));
501 protected function getJwksResponse(): Response
503 return new Response(200, [
504 'Content-Type' => 'application/json',
505 'Cache-Control' => 'no-cache, no-store',
506 'Pragma' => 'no-cache',
509 OidcJwtHelper::publicJwkKeyArray(),
514 protected function getMockAuthorizationResponse($claimOverrides = []): Response
516 return new Response(200, [
517 'Content-Type' => 'application/json',
518 'Cache-Control' => 'no-cache, no-store',
519 'Pragma' => 'no-cache',
521 'access_token' => 'abc123',
522 'token_type' => 'Bearer',
523 'expires_in' => 3600,
524 'id_token' => OidcJwtHelper::idToken($claimOverrides),