Skip to content

Commit 30bf0ce

Browse files
committed
OIDC: Updated avatar fetching to run on each login
But only where the user does not already have an avatar assigned. This aligns with the LDAP avatar fetching logic.
1 parent b64c9b3 commit 30bf0ce

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

app/Access/Oidc/OidcService.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,7 @@ protected function processAccessTokenCallback(OidcAccessToken $accessToken, Oidc
222222
throw new OidcException($exception->getMessage());
223223
}
224224

225-
// TODO - Update this (and tests and config option comments) to actually align with LDAP system
226-
// which syncs whenever on login or registration, where there's no existing avatar.
227-
if ($this->config()['fetch_avatar'] && $user->wasRecentlyCreated && $userDetails->picture) {
225+
if ($this->config()['fetch_avatar'] && !$user->avatar()->exists() && $userDetails->picture) {
228226
$this->userAvatars->assignToUserFromUrl($user, $userDetails->picture);
229227
}
230228

app/Config/oidc.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
// Multiple values can be provided comma seperated.
4848
'additional_scopes' => env('OIDC_ADDITIONAL_SCOPES', null),
4949

50-
// Enable fetching of the user's avatar from the 'picture' claim on initial login.
50+
// Enable fetching of the user's avatar from the 'picture' claim on login.
51+
// Will only be fetched if the user doesn't already have an avatar image assigned.
5152
// This can be a security risk due to performing server-side fetching of data from external URLs.
5253
// Only enable if you trust the OIDC auth provider to provide safe URLs for user images.
5354
'fetch_avatar' => env('OIDC_FETCH_AVATAR', false),

tests/Auth/OidcTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use BookStack\Activity\ActivityType;
66
use BookStack\Facades\Theme;
77
use BookStack\Theming\ThemeEvents;
8+
use BookStack\Uploads\UserAvatars;
89
use BookStack\Users\Models\Role;
910
use BookStack\Users\Models\User;
1011
use GuzzleHttp\Psr7\Response;
@@ -475,6 +476,26 @@ public function test_user_avatar_fetched_from_picture_on_first_login_if_enabled(
475476
$this->assertTrue($user->avatar()->exists());
476477
}
477478

479+
public function test_user_avatar_fetched_for_existing_user_when_no_avatar_already_assigned()
480+
{
481+
config()->set(['oidc.fetch_avatar' => true]);
482+
$editor = $this->users->editor();
483+
$editor->external_auth_id = 'benny509';
484+
$editor->save();
485+
486+
$this->assertFalse($editor->avatar()->exists());
487+
488+
$this->runLogin([
489+
'picture' => 'https://example.com/my-avatar.jpg',
490+
'sub' => 'benny509',
491+
], [
492+
new Response(200, ['Content-Type' => 'image/jpeg'], $this->files->jpegImageData())
493+
]);
494+
495+
$editor->refresh();
496+
$this->assertTrue($editor->avatar()->exists());
497+
}
498+
478499
public function test_user_avatar_not_fetched_if_image_data_format_unknown()
479500
{
480501
config()->set(['oidc.fetch_avatar' => true]);
@@ -492,11 +513,16 @@ public function test_user_avatar_not_fetched_if_image_data_format_unknown()
492513
$this->assertFalse($user->avatar()->exists());
493514
}
494515

495-
public function test_user_avatar_not_fetched_when_user_already_exists()
516+
public function test_user_avatar_not_fetched_when_avatar_already_assigned()
496517
{
497518
config()->set(['oidc.fetch_avatar' => true]);
498519
$editor = $this->users->editor();
499520
$editor->external_auth_id = 'benny509';
521+
$editor->save();
522+
523+
$avatars = $this->app->make(UserAvatars::class);
524+
$originalImageData = $this->files->pngImageData();
525+
$avatars->assignToUserFromExistingData($editor, $originalImageData, 'png');
500526

501527
$this->runLogin([
502528
'picture' => 'https://example.com/my-avatar.jpg',
@@ -506,7 +532,8 @@ public function test_user_avatar_not_fetched_when_user_already_exists()
506532
]);
507533

508534
$editor->refresh();
509-
$this->assertFalse($editor->avatar()->exists());
535+
$newAvatarData = file_get_contents($this->files->relativeToFullPath($editor->avatar->path));
536+
$this->assertEquals($originalImageData, $newAvatarData);
510537
}
511538

512539
public function test_login_group_sync()

0 commit comments

Comments
 (0)