diff --git a/packages/medusa-plugin-auth/package.json b/packages/medusa-plugin-auth/package.json index 5e9ae1b..9d9f94e 100644 --- a/packages/medusa-plugin-auth/package.json +++ b/packages/medusa-plugin-auth/package.json @@ -60,6 +60,7 @@ }, "dependencies": { "@superfaceai/passport-twitter-oauth2": "^1.1.0", + "@types/node": "^18.11.10", "cors": "^2.8.5", "express": "^4.18.1", "jsonwebtoken": "^8.5.1", diff --git a/packages/medusa-plugin-auth/src/auth-strategies/facebook/admin.ts b/packages/medusa-plugin-auth/src/auth-strategies/facebook/admin.ts index 495e30a..7fda9e2 100644 --- a/packages/medusa-plugin-auth/src/auth-strategies/facebook/admin.ts +++ b/packages/medusa-plugin-auth/src/auth-strategies/facebook/admin.ts @@ -8,7 +8,7 @@ import { Router } from 'express'; import cors from 'cors'; import { FACEBOOK_ADMIN_STRATEGY_NAME, FacebookAuthOptions, Profile } from './types'; import { PassportStrategy } from '../../core/Strategy'; -import { buildCallbackHandler } from '../../utils/build-callback-handler'; +import { buildCallbackHandler } from '../../core/utils/build-callback-handler'; export class FacebookAdminStrategy extends PassportStrategy(FacebookStrategy, FACEBOOK_ADMIN_STRATEGY_NAME) { constructor( @@ -92,6 +92,7 @@ export function getFacebookAdminAuthRouter(facebook: FacebookAuthOptions, config const expiresIn = facebook.admin.expiresIn ?? TWENTY_FOUR_HOURS_IN_MS; const callbackHandler = buildCallbackHandler( + "admin", ADMIN_AUTH_TOKEN_COOKIE_NAME, configModule.projectConfig.jwt_secret, expiresIn, diff --git a/packages/medusa-plugin-auth/src/auth-strategies/facebook/store.ts b/packages/medusa-plugin-auth/src/auth-strategies/facebook/store.ts index be1ca97..daa760a 100644 --- a/packages/medusa-plugin-auth/src/auth-strategies/facebook/store.ts +++ b/packages/medusa-plugin-auth/src/auth-strategies/facebook/store.ts @@ -10,7 +10,7 @@ import { EntityManager } from 'typeorm'; import { CUSTOMER_METADATA_KEY, STORE_AUTH_TOKEN_COOKIE_NAME, TWENTY_FOUR_HOURS_IN_MS } from '../../types'; import { FACEBOOK_STORE_STRATEGY_NAME, FacebookAuthOptions, Profile } from './types'; import { PassportStrategy } from '../../core/Strategy'; -import { buildCallbackHandler } from '../../utils/build-callback-handler'; +import { buildCallbackHandler } from '../../core/utils/build-callback-handler'; export class FacebookStoreStrategy extends PassportStrategy(FacebookStrategy, FACEBOOK_STORE_STRATEGY_NAME) { constructor( @@ -118,6 +118,7 @@ export function getFacebookStoreAuthRouter(facebook: FacebookAuthOptions, config const expiresIn = facebook.store.expiresIn ?? TWENTY_FOUR_HOURS_IN_MS; const callbackHandler = buildCallbackHandler( + "store", STORE_AUTH_TOKEN_COOKIE_NAME, configModule.projectConfig.jwt_secret, expiresIn, diff --git a/packages/medusa-plugin-auth/src/auth-strategies/google/admin.ts b/packages/medusa-plugin-auth/src/auth-strategies/google/admin.ts index 953bd5b..a3788f1 100644 --- a/packages/medusa-plugin-auth/src/auth-strategies/google/admin.ts +++ b/packages/medusa-plugin-auth/src/auth-strategies/google/admin.ts @@ -8,7 +8,7 @@ import { Router } from 'express'; import cors from 'cors'; import { GOOGLE_ADMIN_STRATEGY_NAME, GoogleAuthOptions, Profile } from './types'; import { PassportStrategy } from '../../core/Strategy'; -import { buildCallbackHandler } from '../../utils/build-callback-handler'; +import { buildCallbackHandler } from '../../core/utils/build-callback-handler'; export class GoogleAdminStrategy extends PassportStrategy(GoogleStrategy, GOOGLE_ADMIN_STRATEGY_NAME) { constructor( @@ -94,6 +94,7 @@ export function getGoogleAdminAuthRouter(google: GoogleAuthOptions, configModule const expiresIn = google.admin.expiresIn ?? TWENTY_FOUR_HOURS_IN_MS; const callbackHandler = buildCallbackHandler( + "admin", ADMIN_AUTH_TOKEN_COOKIE_NAME, configModule.projectConfig.jwt_secret, expiresIn, diff --git a/packages/medusa-plugin-auth/src/auth-strategies/google/store.ts b/packages/medusa-plugin-auth/src/auth-strategies/google/store.ts index 8b6e578..e80759c 100644 --- a/packages/medusa-plugin-auth/src/auth-strategies/google/store.ts +++ b/packages/medusa-plugin-auth/src/auth-strategies/google/store.ts @@ -10,7 +10,7 @@ import { EntityManager } from 'typeorm'; import { CUSTOMER_METADATA_KEY, STORE_AUTH_TOKEN_COOKIE_NAME, TWENTY_FOUR_HOURS_IN_MS } from '../../types'; import { PassportStrategy } from '../../core/Strategy'; import { GOOGLE_STORE_STRATEGY_NAME, GoogleAuthOptions, Profile } from './types'; -import { buildCallbackHandler } from '../../utils/build-callback-handler'; +import { buildCallbackHandler } from '../../core/utils/build-callback-handler'; export class GoogleStoreStrategy extends PassportStrategy(GoogleStrategy, GOOGLE_STORE_STRATEGY_NAME) { constructor( @@ -120,6 +120,7 @@ export function getGoogleStoreAuthRouter(google: GoogleAuthOptions, configModule const expiresIn = google.store.expiresIn ?? TWENTY_FOUR_HOURS_IN_MS; const callbackHandler = buildCallbackHandler( + "store", STORE_AUTH_TOKEN_COOKIE_NAME, configModule.projectConfig.jwt_secret, expiresIn, diff --git a/packages/medusa-plugin-auth/src/auth-strategies/linkedin/admin.ts b/packages/medusa-plugin-auth/src/auth-strategies/linkedin/admin.ts index e9c1e80..6701447 100644 --- a/packages/medusa-plugin-auth/src/auth-strategies/linkedin/admin.ts +++ b/packages/medusa-plugin-auth/src/auth-strategies/linkedin/admin.ts @@ -8,7 +8,7 @@ import { Router } from 'express'; import cors from 'cors'; import { LINKEDIN_ADMIN_STRATEGY_NAME, LinkedinAuthOptions, Profile } from './types'; import { PassportStrategy } from '../../core/Strategy'; -import { buildCallbackHandler } from '../../utils/build-callback-handler'; +import { buildCallbackHandler } from '../../core/utils/build-callback-handler'; export class LinkedinAdminStrategy extends PassportStrategy(LinkedinStrategy, LINKEDIN_ADMIN_STRATEGY_NAME) { constructor( @@ -97,6 +97,7 @@ export function getLinkedinAdminAuthRouter(linkedin: LinkedinAuthOptions, config const expiresIn = linkedin.admin.expiresIn ?? TWENTY_FOUR_HOURS_IN_MS; const callbackHandler = buildCallbackHandler( + "admin", ADMIN_AUTH_TOKEN_COOKIE_NAME, configModule.projectConfig.jwt_secret, expiresIn, diff --git a/packages/medusa-plugin-auth/src/auth-strategies/linkedin/store.ts b/packages/medusa-plugin-auth/src/auth-strategies/linkedin/store.ts index 2606a5c..cec9051 100644 --- a/packages/medusa-plugin-auth/src/auth-strategies/linkedin/store.ts +++ b/packages/medusa-plugin-auth/src/auth-strategies/linkedin/store.ts @@ -10,7 +10,7 @@ import { EntityManager } from 'typeorm'; import { CUSTOMER_METADATA_KEY, STORE_AUTH_TOKEN_COOKIE_NAME, TWENTY_FOUR_HOURS_IN_MS } from '../../types'; import { PassportStrategy } from '../../core/Strategy'; import { LINKEDIN_STORE_STRATEGY_NAME, LinkedinAuthOptions, Profile } from './types'; -import { buildCallbackHandler } from '../../utils/build-callback-handler'; +import { buildCallbackHandler } from '../../core/utils/build-callback-handler'; export class LinkedinStoreStrategy extends PassportStrategy(LinkedinStrategy, LINKEDIN_STORE_STRATEGY_NAME) { constructor( @@ -122,6 +122,7 @@ export function getLinkedinStoreAuthRouter(linkedin: LinkedinAuthOptions, config const expiresIn = linkedin.store.expiresIn ?? TWENTY_FOUR_HOURS_IN_MS; const callbackHandler = buildCallbackHandler( + "store", STORE_AUTH_TOKEN_COOKIE_NAME, configModule.projectConfig.jwt_secret, expiresIn, diff --git a/packages/medusa-plugin-auth/src/core/utils/build-callback-handler.ts b/packages/medusa-plugin-auth/src/core/utils/build-callback-handler.ts new file mode 100644 index 0000000..e219f4e --- /dev/null +++ b/packages/medusa-plugin-auth/src/core/utils/build-callback-handler.ts @@ -0,0 +1,10 @@ +import jwt from 'jsonwebtoken'; +import { getCookieOptions } from './get-cookie-options'; + +export function buildCallbackHandler(domain: "admin" | "store", cookieName: string, secret: string, expiresIn: number, successRedirect: string) { + return (req, res) => { + const tokenData = domain === "admin" ? { userId: req.user.id } : { customer_id: req.user.id } + const token = jwt.sign(tokenData, secret, { expiresIn }); + res.cookie(cookieName, token, getCookieOptions(expiresIn)).redirect(successRedirect); + }; +} diff --git a/packages/medusa-plugin-auth/src/utils/get-cookie-options.ts b/packages/medusa-plugin-auth/src/core/utils/get-cookie-options.ts similarity index 100% rename from packages/medusa-plugin-auth/src/utils/get-cookie-options.ts rename to packages/medusa-plugin-auth/src/core/utils/get-cookie-options.ts diff --git a/packages/medusa-plugin-auth/src/utils/build-callback-handler.ts b/packages/medusa-plugin-auth/src/utils/build-callback-handler.ts deleted file mode 100644 index 30d3cdb..0000000 --- a/packages/medusa-plugin-auth/src/utils/build-callback-handler.ts +++ /dev/null @@ -1,11 +0,0 @@ -import jwt from 'jsonwebtoken'; -import { getCookieOptions } from './get-cookie-options'; - -export function buildCallbackHandler(tokenName: string, secret: string, expiresIn: number, successRedirect: string) { - return (req, res) => { - const token = jwt.sign({ customer_id: req.user.id }, secret, { - expiresIn, - }); - res.cookie(tokenName, token, getCookieOptions(expiresIn)).redirect(successRedirect); - }; -} diff --git a/packages/medusa-plugin-sentry/src/api/handlers/sentry-web-hook.ts b/packages/medusa-plugin-sentry/src/api/handlers/sentry-web-hook.ts index 1164712..f730eca 100644 --- a/packages/medusa-plugin-sentry/src/api/handlers/sentry-web-hook.ts +++ b/packages/medusa-plugin-sentry/src/api/handlers/sentry-web-hook.ts @@ -1,6 +1,6 @@ import { Request, Response } from 'express'; import SentryService from '../../services/sentry'; -import { verifySignature } from '../../utils'; +import { verifySignature } from '../../core/utils'; import { SentryWebHookOptions } from '../../types'; export default (webHookOptions: SentryWebHookOptions) => { diff --git a/yarn.lock b/yarn.lock index 20e2086..276d309 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4531,6 +4531,11 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-18.11.5.tgz#1bc94cf2f9ab5fe33353bc7c79c797dcc5325bef" integrity sha512-3JRwhbjI+cHLAkUorhf8RnqUbFXajvzX4q6fMn5JwkgtuwfYtRQYI3u4V92vI6NJuTsbBQWWh3RZjFsuevyMGQ== +"@types/node@^18.11.10": + version "18.11.10" + resolved "https://registry.yarnpkg.com/@types/node/-/node-18.11.10.tgz#4c64759f3c2343b7e6c4b9caf761c7a3a05cee34" + integrity sha512-juG3RWMBOqcOuXC643OAdSA525V44cVgGV6dUDuiFtss+8Fk5x1hI93Rsld43VeJVIeqlP9I7Fn9/qaVqoEAuQ== + "@types/node@^8.5.7": version "8.10.66" resolved "https://registry.yarnpkg.com/@types/node/-/node-8.10.66.tgz#dd035d409df322acc83dff62a602f12a5783bbb3"