From ca64d393a6b1415d3eab070e5955b5bd11a54115 Mon Sep 17 00:00:00 2001 From: zeelrupapara Date: Fri, 31 May 2024 15:42:43 +0530 Subject: [PATCH 1/2] feat: top level domain set in the cookie --- .../utils/__tests__/auth-route-builder.spec.ts | 12 ++++++++++++ .../src/core/passport/utils/auth-routes-builder.ts | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/packages/medusa-plugin-auth/src/core/passport/utils/__tests__/auth-route-builder.spec.ts b/packages/medusa-plugin-auth/src/core/passport/utils/__tests__/auth-route-builder.spec.ts index 3cb3774..3e00986 100644 --- a/packages/medusa-plugin-auth/src/core/passport/utils/__tests__/auth-route-builder.spec.ts +++ b/packages/medusa-plugin-auth/src/core/passport/utils/__tests__/auth-route-builder.spec.ts @@ -13,5 +13,17 @@ describe('auth route builder', () => { url = 'http://google.com'; domain = extractDomain(url); expect(domain).toBe('google.com'); + + url = 'http://auth.google.com/'; + domain = extractDomain(url); + expect(domain).toBe('google.com'); + + url = 'https://auth.google.com/'; + domain = extractDomain(url); + expect(domain).toBe('google.com'); + + url = 'https://www.auth.google.com/'; + domain = extractDomain(url); + expect(domain).toBe('google.com'); }); }); diff --git a/packages/medusa-plugin-auth/src/core/passport/utils/auth-routes-builder.ts b/packages/medusa-plugin-auth/src/core/passport/utils/auth-routes-builder.ts index 808cbc8..c490d38 100644 --- a/packages/medusa-plugin-auth/src/core/passport/utils/auth-routes-builder.ts +++ b/packages/medusa-plugin-auth/src/core/passport/utils/auth-routes-builder.ts @@ -17,6 +17,11 @@ type PassportCallbackAuthenticateMiddlewareOptions = { export const extractDomain = (url) => { const domain = url.match(/^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n]+)/im)[1]; + const mainDomain = domain.split('.'); + if (mainDomain.length > 2) { + // Return the domain and top-level domain (TLD) + return mainDomain.slice(-2).join('.'); + } return domain; }; From a4db64e69d9a6fa4fee6ab5ae17ddc47e763a59c Mon Sep 17 00:00:00 2001 From: zeelrupapara Date: Sun, 2 Jun 2024 17:25:43 +0530 Subject: [PATCH 2/2] feat: use the tldjs for get the top level domain --- packages/medusa-plugin-auth/package.json | 3 +- .../__tests__/auth-route-builder.spec.ts | 29 ------------------- .../passport/utils/auth-routes-builder.ts | 13 ++------- 3 files changed, 4 insertions(+), 41 deletions(-) delete mode 100644 packages/medusa-plugin-auth/src/core/passport/utils/__tests__/auth-route-builder.spec.ts diff --git a/packages/medusa-plugin-auth/package.json b/packages/medusa-plugin-auth/package.json index edad2da..06b3710 100644 --- a/packages/medusa-plugin-auth/package.json +++ b/packages/medusa-plugin-auth/package.json @@ -79,7 +79,8 @@ "passport-firebase-jwt": "^1.2.1", "passport-google-oauth2": "^0.2.0", "passport-linkedin-oauth2": "^2.0.0", - "passport-oauth2": "^1.7.0" + "passport-oauth2": "^1.7.0", + "tldjs": "^2.3.1" }, "jest": { "preset": "ts-jest", diff --git a/packages/medusa-plugin-auth/src/core/passport/utils/__tests__/auth-route-builder.spec.ts b/packages/medusa-plugin-auth/src/core/passport/utils/__tests__/auth-route-builder.spec.ts deleted file mode 100644 index 3e00986..0000000 --- a/packages/medusa-plugin-auth/src/core/passport/utils/__tests__/auth-route-builder.spec.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { extractDomain } from '../auth-routes-builder'; - -describe('auth route builder', () => { - it('should be able to extract a domain from an url', () => { - let url = 'https://www.google.com'; - let domain = extractDomain(url); - expect(domain).toBe('google.com'); - - url = 'http://www.google.com'; - domain = extractDomain(url); - expect(domain).toBe('google.com'); - - url = 'http://google.com'; - domain = extractDomain(url); - expect(domain).toBe('google.com'); - - url = 'http://auth.google.com/'; - domain = extractDomain(url); - expect(domain).toBe('google.com'); - - url = 'https://auth.google.com/'; - domain = extractDomain(url); - expect(domain).toBe('google.com'); - - url = 'https://www.auth.google.com/'; - domain = extractDomain(url); - expect(domain).toBe('google.com'); - }); -}); diff --git a/packages/medusa-plugin-auth/src/core/passport/utils/auth-routes-builder.ts b/packages/medusa-plugin-auth/src/core/passport/utils/auth-routes-builder.ts index c490d38..1673b2d 100644 --- a/packages/medusa-plugin-auth/src/core/passport/utils/auth-routes-builder.ts +++ b/packages/medusa-plugin-auth/src/core/passport/utils/auth-routes-builder.ts @@ -4,6 +4,7 @@ import cors from 'cors'; import { authCallbackMiddleware, authenticateSessionFactory, signToken } from '../../auth-callback-middleware'; import { ConfigModule } from '@medusajs/medusa/dist/types/global'; import { CookieOptions } from 'express-serve-static-core'; +import { getDomain } from 'tldjs' type PassportAuthenticateMiddlewareOptions = { [key: string]: unknown; @@ -15,16 +16,6 @@ type PassportCallbackAuthenticateMiddlewareOptions = { failureRedirect: string; }; -export const extractDomain = (url) => { - const domain = url.match(/^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n]+)/im)[1]; - const mainDomain = domain.split('.'); - if (mainDomain.length > 2) { - // Return the domain and top-level domain (TLD) - return mainDomain.slice(-2).join('.'); - } - return domain; -}; - /** * Build and return a router including the different route and configuration for a passport strategy * @param domain @@ -138,7 +129,7 @@ function successActionHandlerFactory( const returnAccessToken = req.query.returnAccessToken == 'true'; const redirectUrl = (req.query.redirectTo ? req.query.redirectTo : defaultRedirect) as string; const isProdOrStaging = process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'staging'; - const originHost = isProdOrStaging ? req.get('referer') && extractDomain(req.get('referer')) : undefined; + const originHost = isProdOrStaging ? req.get('referer') && getDomain(req.get('referer')) : undefined; if (returnAccessToken) { return (req: Request, res: Response) => {