From 71a2fbaa58c0b6a7d496605b0a7b767dc416fcc0 Mon Sep 17 00:00:00 2001 From: Marcus Eide Date: Mon, 24 Jan 2022 14:22:54 +0100 Subject: [PATCH] Simplify the cookiePath logic Signed-off-by: Marcus Eide --- .../auth-backend/src/lib/oauth/helpers.test.ts | 2 ++ plugins/auth-backend/src/lib/oauth/helpers.ts | 16 +++++----------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/plugins/auth-backend/src/lib/oauth/helpers.test.ts b/plugins/auth-backend/src/lib/oauth/helpers.test.ts index 9145f73370..a98b684141 100644 --- a/plugins/auth-backend/src/lib/oauth/helpers.test.ts +++ b/plugins/auth-backend/src/lib/oauth/helpers.test.ts @@ -116,6 +116,7 @@ describe('OAuthProvider Utils', () => { expect(getCookieConfig(mockAuthUrl, 'test-provider')).toMatchObject({ cookieDomain: 'domain.org', cookiePath: '/auth/test-provider', + secure: false, }); }); @@ -126,6 +127,7 @@ describe('OAuthProvider Utils', () => { expect(getCookieConfig(mockAuthUrl, 'test-provider')).toMatchObject({ cookieDomain: 'domain.org', cookiePath: '/auth/test-provider', + secure: false, }); }); diff --git a/plugins/auth-backend/src/lib/oauth/helpers.ts b/plugins/auth-backend/src/lib/oauth/helpers.ts index 635c1eb39b..878083e679 100644 --- a/plugins/auth-backend/src/lib/oauth/helpers.ts +++ b/plugins/auth-backend/src/lib/oauth/helpers.ts @@ -63,17 +63,11 @@ export const getCookieConfig = (authUrl: URL, providerId: string) => { const secure = protocol === 'https:'; // If the provider supports callbackUrls, the pathname will - // contain the complete path to the frame handler. - // The regex captures the leading path including the provider. - // - // Example match: /api/auth/provider/handler/frame - // Example result: /api/auth/provider - const matcher = pathname.match( - new RegExp(`(?^\/.*?\/${providerId})\/handler\/frame$`), - )?.groups; - - // If there's no match we can manually construct the path - const cookiePath = matcher?.path ?? `${pathname}/${providerId}`; + // contain the complete path to the frame handler so we need + // to slice off the trailing part of the path. + const cookiePath = pathname.endsWith(`${providerId}/handler/frame`) + ? pathname.slice(0, -'/handler/frame'.length) + : `${pathname}/${providerId}`; return { cookieDomain,