From 76e7c03e82f1fb33468e45f53a7eaded744cea5b Mon Sep 17 00:00:00 2001 From: Bogdan Kostyuk Date: Sun, 10 Nov 2024 20:55:02 +0200 Subject: [PATCH] chore: move oauth validation function to oauth file --- server/utils/oauth/{index.ts => oauth.ts} | 33 +++++++++++++++++++++-- server/utils/oauth/validation.ts | 31 --------------------- 2 files changed, 31 insertions(+), 33 deletions(-) rename server/utils/oauth/{index.ts => oauth.ts} (81%) delete mode 100644 server/utils/oauth/validation.ts diff --git a/server/utils/oauth/index.ts b/server/utils/oauth/oauth.ts similarity index 81% rename from server/utils/oauth/index.ts rename to server/utils/oauth/oauth.ts index f4417033..ed0833c1 100644 --- a/server/utils/oauth/index.ts +++ b/server/utils/oauth/oauth.ts @@ -11,6 +11,10 @@ const stateSerializeOptions = { sameSite: 'lax', } satisfies CookieSerializeOptions; +export function deleteOAuthStateCookie(event: H3Event) { + deleteCookie(event, 'state', stateSerializeOptions); +} + export function sendOAuthRedirectIfNeeded({ event, query, config }: { event: H3Event, query?: QueryObject, config: OAuthProviderConfig }): boolean { if (!query) { query = getQuery(event)!; @@ -45,8 +49,33 @@ export function sendOAuthRedirectIfNeeded({ event, query, config }: { event: H3E return true; } -export function deleteOAuthStateCookie(event: H3Event) { - deleteCookie(event, 'state', stateSerializeOptions); +export async function assertNoOAuthErrors(event: H3Event, query: QueryObject) { + // Can't delete the cookie here, because user might need to enter a username + // which is done later, with separate check + // deleteCookie(event, 'state'); + + if (query.error) { + deleteOAuthStateCookie(event); + + await logger.error(event, { err: new Error(query.error.toString()), msg: 'oauth failed' }); + + throw createError({ status: 418, message: decodeURIComponent(query.error.toString()) }); + } + + const stateCookie = getCookie(event, 'state'); + if (typeof query.state !== 'string' || query.state !== stateCookie) { + deleteOAuthStateCookie(event); + + const identifier = getRequestIP(event, { xForwardedFor: true }); + + await logger.error(event, { + msg: 'someone is messing with authentication', + identifier, + stateCookie, + }); + + throw createError({ status: 422 }); + } } export async function createUserWithSocialAuth(socialAuth: NormalizedSocialUser) { diff --git a/server/utils/oauth/validation.ts b/server/utils/oauth/validation.ts deleted file mode 100644 index a131e71b..00000000 --- a/server/utils/oauth/validation.ts +++ /dev/null @@ -1,31 +0,0 @@ -import type { H3Event } from 'h3'; -import type { QueryObject } from 'ufo'; - -export async function assertNoOAuthErrors(event: H3Event, query: QueryObject) { - // Can't delete the cookie here, because user might need to enter a username - // which is done later, with separate check - // deleteCookie(event, 'state'); - - if (query.error) { - deleteOAuthStateCookie(event); - - await logger.error(event, { err: new Error(query.error.toString()), msg: 'oauth failed' }); - - throw createError({ status: 418, message: decodeURIComponent(query.error.toString()) }); - } - - const stateCookie = getCookie(event, 'state'); - if (typeof query.state !== 'string' || query.state !== stateCookie) { - deleteOAuthStateCookie(event); - - const identifier = getRequestIP(event, { xForwardedFor: true }); - - await logger.error(event, { - msg: 'someone is messing with authentication', - identifier, - stateCookie, - }); - - throw createError({ status: 422 }); - } -}