From 1e9601179389843f739560fbcd3a4ee5c383c62c Mon Sep 17 00:00:00 2001 From: Elias Schneider Date: Tue, 15 Oct 2024 20:12:09 +0200 Subject: [PATCH] refactor: run formatter --- backend/src/auth/auth.controller.ts | 6 ++- backend/src/auth/auth.service.ts | 39 ++++++++++++------- backend/src/oauth/oauth.service.ts | 19 +++++---- .../src/oauth/provider/discord.provider.ts | 2 +- frontend/src/services/auth.service.ts | 3 +- 5 files changed, 43 insertions(+), 26 deletions(-) diff --git a/backend/src/auth/auth.controller.ts b/backend/src/auth/auth.controller.ts index bc1cca1b..52dff306 100644 --- a/backend/src/auth/auth.controller.ts +++ b/backend/src/auth/auth.controller.ts @@ -120,7 +120,7 @@ export class AuthController { }) @HttpCode(202) async requestResetPassword(@Param("email") email: string) { - this.authService.requestResetPassword(email); + await this.authService.requestResetPassword(email); } @Post("resetPassword") @@ -172,7 +172,9 @@ export class AuthController { @Req() request: Request, @Res({ passthrough: true }) response: Response, ) { - const redirectURI = await this.authService.signOut(request.cookies.access_token); + const redirectURI = await this.authService.signOut( + request.cookies.access_token, + ); const isSecure = this.config.get("general.appUrl").startsWith("https"); response.cookie("access_token", "", { diff --git a/backend/src/auth/auth.service.ts b/backend/src/auth/auth.service.ts index a14fe248..275d1132 100644 --- a/backend/src/auth/auth.service.ts +++ b/backend/src/auth/auth.service.ts @@ -16,12 +16,12 @@ import * as moment from "moment"; import { ConfigService } from "src/config/config.service"; import { EmailService } from "src/email/email.service"; import { PrismaService } from "src/prisma/prisma.service"; +import { OAuthService } from "../oauth/oauth.service"; +import { GenericOidcProvider } from "../oauth/provider/genericOidc.provider"; +import { UserSevice } from "../user/user.service"; import { AuthRegisterDTO } from "./dto/authRegister.dto"; import { AuthSignInDTO } from "./dto/authSignIn.dto"; import { LdapService } from "./ldap.service"; -import { GenericOidcProvider } from "../oauth/provider/genericOidc.provider"; -import { OAuthService } from "../oauth/oauth.service"; -import { UserSevice } from "../user/user.service"; @Injectable() export class AuthService { @@ -120,10 +120,7 @@ export class AuthService { async generateToken(user: User, oauth?: { idToken?: string }) { // TODO: Make all old loginTokens invalid when a new one is created // Check if the user has TOTP enabled - if ( - user.totpVerified && - !(oauth && this.config.get("oauth.ignoreTotp")) - ) { + if (user.totpVerified && !(oauth && this.config.get("oauth.ignoreTotp"))) { const loginToken = await this.createLoginToken(user.id); return { loginToken }; @@ -163,7 +160,7 @@ export class AuthService { }, }); - await this.emailService.sendResetPasswordEmail(user.email, token); + this.emailService.sendResetPasswordEmail(user.email, token); } async resetPassword(token: string, newPassword: string) { @@ -231,7 +228,10 @@ export class AuthService { if (refreshTokenId) { const oauthIDToken = await this.prisma.refreshToken - .findFirst({ select: { oauthIDToken: true }, where: { id: refreshTokenId } }) + .findFirst({ + select: { oauthIDToken: true }, + where: { id: refreshTokenId }, + }) .then((refreshToken) => refreshToken?.oauthIDToken) .catch((e) => { // Ignore error if refresh token doesn't exist @@ -249,16 +249,27 @@ export class AuthService { const provider = this.oAuthService.availableProviders()[providerName]; let signOutFromProviderSupportedAndActivated = false; try { - signOutFromProviderSupportedAndActivated = this.config.get(`oauth.${providerName}-signOut`); + signOutFromProviderSupportedAndActivated = this.config.get( + `oauth.${providerName}-signOut`, + ); } catch (_) { // Ignore error if the provider is not supported or if the provider sign out is not activated } - if (provider instanceof GenericOidcProvider && signOutFromProviderSupportedAndActivated) { - const configuration = await provider.getConfiguration(); - if (configuration.frontchannel_logout_supported && URL.canParse(configuration.end_session_endpoint)) { + if ( + provider instanceof GenericOidcProvider && + signOutFromProviderSupportedAndActivated + ) { + const configuration = await provider.getConfiguration(); + if ( + configuration.frontchannel_logout_supported && + URL.canParse(configuration.end_session_endpoint) + ) { const redirectURI = new URL(configuration.end_session_endpoint); redirectURI.searchParams.append("id_token_hint", idTokenHint); - redirectURI.searchParams.append("client_id", this.config.get(`oauth.${providerName}-clientId`)); + redirectURI.searchParams.append( + "client_id", + this.config.get(`oauth.${providerName}-clientId`), + ); return redirectURI.toString(); } } diff --git a/backend/src/oauth/oauth.service.ts b/backend/src/oauth/oauth.service.ts index 872ad590..08b7eb9a 100644 --- a/backend/src/oauth/oauth.service.ts +++ b/backend/src/oauth/oauth.service.ts @@ -15,7 +15,8 @@ export class OAuthService { private config: ConfigService, @Inject(forwardRef(() => AuthService)) private auth: AuthService, @Inject("OAUTH_PLATFORMS") private platforms: string[], - @Inject("OAUTH_PROVIDERS") private oAuthProviders: Record>, + @Inject("OAUTH_PROVIDERS") + private oAuthProviders: Record>, ) {} private readonly logger = new Logger(OAuthService.name); @@ -30,13 +31,15 @@ export class OAuthService { } availableProviders(): Record> { - return Object.fromEntries(Object.entries(this.oAuthProviders) - .map(([providerName, provider]) => [ - [providerName, provider], - this.config.get(`oauth.${providerName}-enabled`), - ]) - .filter(([_, enabled]) => enabled) - .map(([provider, _]) => provider)); + return Object.fromEntries( + Object.entries(this.oAuthProviders) + .map(([providerName, provider]) => [ + [providerName, provider], + this.config.get(`oauth.${providerName}-enabled`), + ]) + .filter(([_, enabled]) => enabled) + .map(([provider, _]) => provider), + ); } async status(user: User) { diff --git a/backend/src/oauth/provider/discord.provider.ts b/backend/src/oauth/provider/discord.provider.ts index e9ee2138..3268d0f6 100644 --- a/backend/src/oauth/provider/discord.provider.ts +++ b/backend/src/oauth/provider/discord.provider.ts @@ -85,7 +85,7 @@ export class DiscordProvider implements OAuthProvider { if (limitedUsers) { await this.checkLimitedUsers(user, limitedUsers); } - + return { provider: "discord", providerId: user.id, diff --git a/frontend/src/services/auth.service.ts b/frontend/src/services/auth.service.ts index 3198ec7a..634b0fe1 100644 --- a/frontend/src/services/auth.service.ts +++ b/frontend/src/services/auth.service.ts @@ -31,7 +31,8 @@ const signUp = async (email: string, username: string, password: string) => { const signOut = async () => { const response = await api.post("/auth/signOut"); - if (URL.canParse(response.data?.redirectURI)) window.location.href = response.data.redirectURI; + if (URL.canParse(response.data?.redirectURI)) + window.location.href = response.data.redirectURI; else window.location.reload(); };