]> BookStack Code Mirror - bookstack/commitdiff
Added option to change the OIDC claim regarded as the ID
authorDan Brown <redacted>
Thu, 26 Jan 2023 16:43:15 +0000 (16:43 +0000)
committerDan Brown <redacted>
Thu, 26 Jan 2023 16:43:15 +0000 (16:43 +0000)
Defined via a OIDC_EXTERNAL_ID_CLAIM env option.
For #3914

.env.example.complete
app/Auth/Access/Oidc/OidcService.php
app/Config/oidc.php
tests/Auth/OidcTest.php

index 03e52d6bb673d05e99cd20f9b7eeac2dd399a2c4..7e6c6ee3c0d29d5ad2c24d92b7ac385f538bf13b 100644 (file)
@@ -268,6 +268,7 @@ OIDC_DUMP_USER_DETAILS=false
 OIDC_USER_TO_GROUPS=false
 OIDC_GROUPS_CLAIM=groups
 OIDC_REMOVE_FROM_GROUPS=false
+OIDC_EXTERNAL_ID_CLAIM=sub
 
 # Disable default third-party services such as Gravatar and Draw.IO
 # Service-specific options will override this option
index a9323d4233109e544bd94ec044fa4e57530a2cd9..1ca5e19a236b9aff5e54b9a77ffba18f71a87936 100644 (file)
@@ -198,7 +198,8 @@ class OidcService
      */
     protected function getUserDetails(OidcIdToken $token): array
     {
-        $id = $token->getClaim('sub');
+        $idClaim = $this->config()['external_id_claim'];
+        $id = $token->getClaim($idClaim);
 
         return [
             'external_id' => $id,
index d223a63eff6d4816dd202635ab4891c865a01d21..1f73fb688662e8b72ea69700af2f1d4fa7138e31 100644 (file)
@@ -8,9 +8,12 @@ return [
     // Dump user details after a login request for debugging purposes
     'dump_user_details' => env('OIDC_DUMP_USER_DETAILS', false),
 
-    // Attribute, within a OpenId token, to find the user's display name
+    // Claim, within an OpenId token, to find the user's display name
     'display_name_claims' => explode('|', env('OIDC_DISPLAY_NAME_CLAIMS', 'name')),
 
+    // Claim, within an OpenID token, to use to connect a BookStack user to the OIDC user.
+    'external_id_claim' => env('OIDC_EXTERNAL_ID_CLAIM', 'sub'),
+
     // OAuth2/OpenId client id, as configured in your Authorization server.
     'client_id' => env('OIDC_CLIENT_ID', null),
 
index 32c2d4ae2e5f3225280c6e0cf7a6f03325724920..35acb7752165d1592791b3ba0b7972dbd16d2471 100644 (file)
@@ -42,6 +42,7 @@ class OidcTest extends TestCase
             'oidc.user_to_groups'         => false,
             'oidc.groups_claim'           => 'group',
             'oidc.remove_from_groups'     => false,
+            'oidc.external_id_claim'      => 'sub',
         ]);
     }
 
@@ -391,6 +392,25 @@ class OidcTest extends TestCase
         $this->assertTrue(auth()->check());
     }
 
+    public function test_auth_uses_configured_external_id_claim_option()
+    {
+        config()->set([
+            'oidc.external_id_claim' => 'super_awesome_id',
+        ]);
+        $roleA = Role::factory()->create(['display_name' => 'Wizards']);
+
+        $resp = $this->runLogin([
+            'email'            => '[email protected]',
+            'sub'              => 'benny1010101',
+            'super_awesome_id' => 'xXBennyTheGeezXx',
+        ]);
+        $resp->assertRedirect('/');
+
+        /** @var User $user */
+        $user = User::query()->where('email', '=', '[email protected]')->first();
+        $this->assertEquals('xXBennyTheGeezXx', $user->external_auth_id);
+    }
+
     public function test_login_group_sync()
     {
         config()->set([