Skip to content

Commit 811be3a

Browse files
committed
Added option to change the OIDC claim regarded as the ID
Defined via a OIDC_EXTERNAL_ID_CLAIM env option. For #3914
1 parent 3202f96 commit 811be3a

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

.env.example.complete

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ OIDC_DUMP_USER_DETAILS=false
268268
OIDC_USER_TO_GROUPS=false
269269
OIDC_GROUPS_CLAIM=groups
270270
OIDC_REMOVE_FROM_GROUPS=false
271+
OIDC_EXTERNAL_ID_CLAIM=sub
271272

272273
# Disable default third-party services such as Gravatar and Draw.IO
273274
# Service-specific options will override this option

app/Auth/Access/Oidc/OidcService.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ protected function getUserGroups(OidcIdToken $token): array
198198
*/
199199
protected function getUserDetails(OidcIdToken $token): array
200200
{
201-
$id = $token->getClaim('sub');
201+
$idClaim = $this->config()['external_id_claim'];
202+
$id = $token->getClaim($idClaim);
202203

203204
return [
204205
'external_id' => $id,

app/Config/oidc.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
// Dump user details after a login request for debugging purposes
99
'dump_user_details' => env('OIDC_DUMP_USER_DETAILS', false),
1010

11-
// Attribute, within a OpenId token, to find the user's display name
11+
// Claim, within an OpenId token, to find the user's display name
1212
'display_name_claims' => explode('|', env('OIDC_DISPLAY_NAME_CLAIMS', 'name')),
1313

14+
// Claim, within an OpenID token, to use to connect a BookStack user to the OIDC user.
15+
'external_id_claim' => env('OIDC_EXTERNAL_ID_CLAIM', 'sub'),
16+
1417
// OAuth2/OpenId client id, as configured in your Authorization server.
1518
'client_id' => env('OIDC_CLIENT_ID', null),
1619

tests/Auth/OidcTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ protected function setUp(): void
4242
'oidc.user_to_groups' => false,
4343
'oidc.groups_claim' => 'group',
4444
'oidc.remove_from_groups' => false,
45+
'oidc.external_id_claim' => 'sub',
4546
]);
4647
}
4748

@@ -391,6 +392,25 @@ public function test_auth_login_with_autodiscovery_with_keys_that_do_not_have_us
391392
$this->assertTrue(auth()->check());
392393
}
393394

395+
public function test_auth_uses_configured_external_id_claim_option()
396+
{
397+
config()->set([
398+
'oidc.external_id_claim' => 'super_awesome_id',
399+
]);
400+
$roleA = Role::factory()->create(['display_name' => 'Wizards']);
401+
402+
$resp = $this->runLogin([
403+
'email' => 'benny@example.com',
404+
'sub' => 'benny1010101',
405+
'super_awesome_id' => 'xXBennyTheGeezXx',
406+
]);
407+
$resp->assertRedirect('/');
408+
409+
/** @var User $user */
410+
$user = User::query()->where('email', '=', 'benny@example.com')->first();
411+
$this->assertEquals('xXBennyTheGeezXx', $user->external_auth_id);
412+
}
413+
394414
public function test_login_group_sync()
395415
{
396416
config()->set([

0 commit comments

Comments
 (0)