From 31366d961f5827c200038b65ec9de5d4ddc8b898 Mon Sep 17 00:00:00 2001 From: "Marvin A. Ruder" Date: Mon, 1 Jul 2024 10:34:31 +0200 Subject: [PATCH] fix(oauth): provider username is ignored when signing up using OAuth (#511) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🐛 Bug Report: Provider username is ignored when signing up using OAuth Fixes #505 Signed-off-by: Marvin A. Ruder * Implement fallback logic for username conflicts * Reprioritize claims for OIDC provider username Signed-off-by: Marvin A. Ruder --------- Signed-off-by: Marvin A. Ruder --- backend/src/oauth/oauth.service.ts | 8 ++++---- backend/src/oauth/provider/genericOidc.provider.ts | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/backend/src/oauth/oauth.service.ts b/backend/src/oauth/oauth.service.ts index 0a374ec2..c44e53ec 100644 --- a/backend/src/oauth/oauth.service.ts +++ b/backend/src/oauth/oauth.service.ts @@ -102,9 +102,9 @@ export class OAuthService { } } - private async getAvailableUsername(email: string) { - // only remove + and - from email for now (maybe not enough) - let username = email.split("@")[0].replace(/[+-]/g, "").substring(0, 20); + private async getAvailableUsername(preferredUsername: string) { + // only remove + and - from preferred username for now (maybe not enough) + let username = preferredUsername.replace(/[+-]/g, "").substring(0, 20); while (true) { const user = await this.prisma.user.findFirst({ where: { @@ -153,7 +153,7 @@ export class OAuthService { const result = await this.auth.signUp({ email: user.email, - username: await this.getAvailableUsername(user.email), + username: await this.getAvailableUsername(user.providerUsername), password: null, }); diff --git a/backend/src/oauth/provider/genericOidc.provider.ts b/backend/src/oauth/provider/genericOidc.provider.ts index 4cff24ee..c69134dd 100644 --- a/backend/src/oauth/provider/genericOidc.provider.ts +++ b/backend/src/oauth/provider/genericOidc.provider.ts @@ -125,9 +125,9 @@ export abstract class GenericOidcProvider implements OAuthProvider { const username = claim ? idTokenData[claim] - : idTokenData.name || - idTokenData.nickname || - idTokenData.preferred_username; + : idTokenData.preferred_username || + idTokenData.name || + idTokenData.nickname; if (!username) { this.logger.error(