Skip to content

Add support for OIDC picture #5177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/Access/Oidc/OidcService.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ protected function processAccessTokenCallback(OidcAccessToken $accessToken, Oidc
$user = $this->registrationService->findOrRegister(
$userDetails->name,
$userDetails->email,
$userDetails->externalId
$userDetails->externalId,
$userDetails->picture,
);
} catch (UserRegistrationException $exception) {
throw new OidcException($exception->getMessage());
Expand Down
2 changes: 2 additions & 0 deletions app/Access/Oidc/OidcUserDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public function __construct(
public ?string $email = null,
public ?string $name = null,
public ?array $groups = null,
public ?string $picture = null,
) {
}

Expand Down Expand Up @@ -40,6 +41,7 @@ public function populate(
$this->email = $claims->getClaim('email') ?? $this->email;
$this->name = static::getUserDisplayName($displayNameClaims, $claims) ?? $this->name;
$this->groups = static::getUserGroups($groupsClaim, $claims) ?? $this->groups;
$this->picture = $claims->getClaim('picture') ?? $this->picture;
}

protected static function getUserDisplayName(string $displayNameClaims, ProvidesClaims $token): string
Expand Down
8 changes: 4 additions & 4 deletions app/Access/RegistrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected function registrationAllowed(): bool
*
* @throws UserRegistrationException
*/
public function findOrRegister(string $name, string $email, string $externalId): User
public function findOrRegister(string $name, string $email, string $externalId, string $picture = null): User
{
$user = User::query()
->where('external_auth_id', '=', $externalId)
Expand All @@ -64,7 +64,7 @@ public function findOrRegister(string $name, string $email, string $externalId):
'external_auth_id' => $externalId,
];

$user = $this->registerUser($userData, null, false);
$user = $this->registerUser($userData, null, false, $picture);
}

return $user;
Expand All @@ -75,7 +75,7 @@ public function findOrRegister(string $name, string $email, string $externalId):
*
* @throws UserRegistrationException
*/
public function registerUser(array $userData, ?SocialAccount $socialAccount = null, bool $emailConfirmed = false): User
public function registerUser(array $userData, ?SocialAccount $socialAccount = null, bool $emailConfirmed = false, string $picture = null): User
{
$userEmail = $userData['email'];
$authSystem = $socialAccount ? $socialAccount->driver : auth()->getDefaultDriver();
Expand All @@ -96,7 +96,7 @@ public function registerUser(array $userData, ?SocialAccount $socialAccount = nu
}

// Create the user
$newUser = $this->userRepo->createWithoutActivity($userData, $emailConfirmed);
$newUser = $this->userRepo->createWithoutActivity($userData, $emailConfirmed, $picture);
$newUser->attachDefaultRole();

// Assign social account if given
Expand Down
8 changes: 4 additions & 4 deletions app/Uploads/UserAvatars.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public function __construct(
/**
* Fetch and assign an avatar image to the given user.
*/
public function fetchAndAssignToUser(User $user): void
public function fetchAndAssignToUser(User $user, string $picture = null): void
{
if (!$this->avatarFetchEnabled()) {
return;
}

try {
$this->destroyAllForUser($user);
$avatar = $this->saveAvatarImage($user);
$avatar = $this->saveAvatarImage($user, 500, $picture);
$user->avatar()->associate($avatar);
$user->save();
} catch (Exception $e) {
Expand Down Expand Up @@ -72,9 +72,9 @@ public function destroyAllForUser(User $user): void
*
* @throws HttpFetchException
*/
protected function saveAvatarImage(User $user, int $size = 500): Image
protected function saveAvatarImage(User $user, int $size = 500, string $picture = null): Image
{
$avatarUrl = $this->getAvatarUrl();
$avatarUrl = $picture ?: $this->getAvatarUrl();
$email = strtolower(trim($user->email));

$replacements = [
Expand Down
8 changes: 4 additions & 4 deletions app/Users/UserRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function getBySlug(string $slug): User
*
* @param array{name: string, email: string, password: ?string, external_auth_id: ?string, language: ?string, roles: ?array} $data
*/
public function createWithoutActivity(array $data, bool $emailConfirmed = false): User
public function createWithoutActivity(array $data, bool $emailConfirmed = false, string $picture = null): User
{
$user = new User();
$user->name = $data['name'];
Expand All @@ -74,7 +74,7 @@ public function createWithoutActivity(array $data, bool $emailConfirmed = false)
$this->setUserRoles($user, $data['roles']);
}

$this->downloadAndAssignUserAvatar($user);
$this->downloadAndAssignUserAvatar($user, $picture);

return $user;
}
Expand Down Expand Up @@ -199,10 +199,10 @@ protected function migrateOwnership(User $fromUser, User $toUser)
* Get an avatar image for a user and set it as their avatar.
* Returns early if avatars disabled or not set in config.
*/
protected function downloadAndAssignUserAvatar(User $user): void
protected function downloadAndAssignUserAvatar(User $user, string $picture = null): void
{
try {
$this->userAvatar->fetchAndAssignToUser($user);
$this->userAvatar->fetchAndAssignToUser($user, $picture);
} catch (Exception $e) {
Log::error('Failed to save user avatar image');
}
Expand Down