1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-06-07 04:22:06 +03:00

OIDC: Added testing coverage for picture fetching

This commit is contained in:
Dan Brown 2025-05-24 14:36:36 +01:00
parent f9dbbe5d70
commit b64c9b31d5
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
6 changed files with 64 additions and 1 deletions

View File

@ -222,6 +222,8 @@ class OidcService
throw new OidcException($exception->getMessage()); throw new OidcException($exception->getMessage());
} }
// TODO - Update this (and tests and config option comments) to actually align with LDAP system
// which syncs whenever on login or registration, where there's no existing avatar.
if ($this->config()['fetch_avatar'] && $user->wasRecentlyCreated && $userDetails->picture) { if ($this->config()['fetch_avatar'] && $user->wasRecentlyCreated && $userDetails->picture) {
$this->userAvatars->assignToUserFromUrl($user, $userDetails->picture); $this->userAvatars->assignToUserFromUrl($user, $userDetails->picture);
} }

View File

@ -65,7 +65,7 @@ class UserAvatars
$mime = (new WebSafeMimeSniffer())->sniff($imageData); $mime = (new WebSafeMimeSniffer())->sniff($imageData);
[$format, $type] = explode('/', $mime, 2); [$format, $type] = explode('/', $mime, 2);
if ($format !== 'image' || ImageService::isExtensionSupported($type)) { if ($format !== 'image' || !ImageService::isExtensionSupported($type)) {
return; return;
} }

View File

@ -45,6 +45,7 @@ use Illuminate\Support\Collection;
* @property string $system_name * @property string $system_name
* @property Collection $roles * @property Collection $roles
* @property Collection $mfaValues * @property Collection $mfaValues
* @property ?Image $avatar
*/ */
class User extends Model implements AuthenticatableContract, CanResetPasswordContract, Loggable, Sluggable class User extends Model implements AuthenticatableContract, CanResetPasswordContract, Loggable, Sluggable
{ {

View File

@ -41,6 +41,7 @@ class OidcTest extends TestCase
'oidc.discover' => false, 'oidc.discover' => false,
'oidc.dump_user_details' => false, 'oidc.dump_user_details' => false,
'oidc.additional_scopes' => '', 'oidc.additional_scopes' => '',
'odic.fetch_avatar' => false,
'oidc.user_to_groups' => false, 'oidc.user_to_groups' => false,
'oidc.groups_claim' => 'group', 'oidc.groups_claim' => 'group',
'oidc.remove_from_groups' => false, 'oidc.remove_from_groups' => false,
@ -457,6 +458,57 @@ class OidcTest extends TestCase
]); ]);
} }
public function test_user_avatar_fetched_from_picture_on_first_login_if_enabled()
{
config()->set(['oidc.fetch_avatar' => true]);
$this->runLogin([
'email' => 'avatar@example.com',
'picture' => 'https://example.com/my-avatar.jpg',
], [
new Response(200, ['Content-Type' => 'image/jpeg'], $this->files->jpegImageData())
]);
$user = User::query()->where('email', '=', 'avatar@example.com')->first();
$this->assertNotNull($user);
$this->assertTrue($user->avatar()->exists());
}
public function test_user_avatar_not_fetched_if_image_data_format_unknown()
{
config()->set(['oidc.fetch_avatar' => true]);
$this->runLogin([
'email' => 'avatar-format@example.com',
'picture' => 'https://example.com/my-avatar.jpg',
], [
new Response(200, ['Content-Type' => 'image/jpeg'], str_repeat('abc123', 5))
]);
$user = User::query()->where('email', '=', 'avatar-format@example.com')->first();
$this->assertNotNull($user);
$this->assertFalse($user->avatar()->exists());
}
public function test_user_avatar_not_fetched_when_user_already_exists()
{
config()->set(['oidc.fetch_avatar' => true]);
$editor = $this->users->editor();
$editor->external_auth_id = 'benny509';
$this->runLogin([
'picture' => 'https://example.com/my-avatar.jpg',
'sub' => 'benny509',
], [
new Response(200, ['Content-Type' => 'image/jpeg'], $this->files->jpegImageData())
]);
$editor->refresh();
$this->assertFalse($editor->avatar()->exists());
}
public function test_login_group_sync() public function test_login_group_sync()
{ {
config()->set([ config()->set([

View File

@ -60,6 +60,14 @@ class FileProvider
return file_get_contents($this->testFilePath('test-image.png')); return file_get_contents($this->testFilePath('test-image.png'));
} }
/**
* Get raw data for a Jpeg image test file.
*/
public function jpegImageData(): string
{
return file_get_contents($this->testFilePath('test-image.jpg'));
}
/** /**
* Get the expected relative path for an uploaded image of the given type and filename. * Get the expected relative path for an uploaded image of the given type and filename.
*/ */

Binary file not shown.

After

Width:  |  Height:  |  Size: 268 B