Skip to content

Commit

Permalink
feat(auth): added new array-like structure
Browse files Browse the repository at this point in the history
  • Loading branch information
stephane-segning committed Dec 1, 2023
1 parent 4dde834 commit 27c6fc1
Show file tree
Hide file tree
Showing 50 changed files with 1,294 additions and 1,026 deletions.
5 changes: 5 additions & 0 deletions packages/medusa-plugin-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
"@medusajs/medusa": ">=1.17.x",
"@types/express": "^4.17.17",
"@types/jest": "^29.1.2",
"@types/passport-auth0": "^1.0.9",
"@types/passport-azure-ad": "^4.3.5",
"@types/passport-facebook": "^3.0.3",
"@types/passport-google-oauth2": "^0.1.8",
"@types/passport-linkedin-oauth2": "^1.5.6",
"@types/passport-oauth2": "^1.4.15",
"jest": "^29.1.2",
"passport": "^0.6.0",
Expand Down
42 changes: 31 additions & 11 deletions packages/medusa-plugin-auth/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,43 @@ import FirebaseStrategy from '../auth-strategies/firebase';
import Auth0Strategy from '../auth-strategies/auth0';
import AzureStrategy from '../auth-strategies/azure-oidc';

import { AuthOptions } from '../types';
import { AuthOptions, AuthOptionsWrapper, handleOption } from '../types';

export default function (rootDirectory, pluginOptions: AuthOptions): Router[] {
const configModule = loadConfig(rootDirectory) as ConfigModule;
export default async function(rootDirectory, pluginOptions: AuthOptions[]): Promise<Router[]> {
const configModule = loadConfig(rootDirectory);
return loadRouters(configModule, pluginOptions);
}

function loadRouters(configModule: ConfigModule, options: AuthOptions): Router[] {
async function loadRouters(configModule: ConfigModule, options: AuthOptionsWrapper[]): Promise<Router[]> {
const routers: Router[] = [];

routers.push(...OAuth2Strategy.getRouter(configModule, options));
routers.push(...GoogleStrategy.getRouter(configModule, options));
routers.push(...FacebookStrategy.getRouter(configModule, options));
routers.push(...LinkedinStrategy.getRouter(configModule, options));
routers.push(...FirebaseStrategy.getRouter(configModule, options));
routers.push(...Auth0Strategy.getRouter(configModule, options));
routers.push(...AzureStrategy.getRouter(configModule, options));
for (const opt of options) {
const option = await handleOption(opt, configModule);

switch (option.type) {
case 'azure_oidc':
routers.push(...AzureStrategy.getRouter(configModule, option));
break;
case 'google':
routers.push(...GoogleStrategy.getRouter(configModule, option));
break;
case 'facebook':
routers.push(...FacebookStrategy.getRouter(configModule, option));
break;
case 'linkedin':
routers.push(...LinkedinStrategy.getRouter(configModule, option));
break;
case 'firebase':
routers.push(...FirebaseStrategy.getRouter(configModule, option));
break;
case 'auth0':
routers.push(...Auth0Strategy.getRouter(configModule, option));
break;
case 'oauth2':
routers.push(...OAuth2Strategy.getRouter(configModule, option));
break;
}
}

return routers;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ConfigModule, MedusaContainer } from '@medusajs/medusa/dist/types/global';
import { Auth0AdminStrategy } from '../../admin';
import { AUTH_PROVIDER_KEY } from '../../../../types';
import { Auth0Options, AUTH0_ADMIN_STRATEGY_NAME, Profile, ExtraParams } from '../../types';
import { AUTH_PROVIDER_KEY, IStrategy } from '../../../../types';
import { AUTH0_ADMIN_STRATEGY_NAME, Auth0Options, ExtraParams } from '../../types';
import { Profile } from 'passport-auth0';
import { getAuth0AdminStrategy } from '../../admin';

describe('Auth0 admin strategy verify callback', function () {
describe('Auth0 admin strategy verify callback', function() {
const existsEmail = '[email protected]';
const existsEmailWithProviderKey = '[email protected]';
const existsEmailWithWrongProviderKey = '[email protected]';
Expand All @@ -12,9 +13,9 @@ describe('Auth0 admin strategy verify callback', function () {
let req: Request;
let accessToken: string;
let refreshToken: string;
let profile: Profile;
let profile: Partial<Profile>;
let extraParams: ExtraParams;
let auth0AdminStrategy: Auth0AdminStrategy;
let auth0AdminStrategy: IStrategy;

beforeEach(() => {
profile = {
Expand All @@ -38,7 +39,7 @@ describe('Auth0 admin strategy verify callback', function () {
return {
id: 'test2',
metadata: {
[AUTH_PROVIDER_KEY]: AUTH0_ADMIN_STRATEGY_NAME,
[AUTH_PROVIDER_KEY]: AUTH0_ADMIN_STRATEGY_NAME + '_test',
},
};
}
Expand All @@ -62,8 +63,9 @@ describe('Auth0 admin strategy verify callback', function () {
} as MedusaContainer;
});

describe('when strict is set to admin', function () {
describe('when strict is set to admin', function() {
beforeEach(() => {
const Auth0AdminStrategy = getAuth0AdminStrategy('test');
auth0AdminStrategy = new Auth0AdminStrategy(
container,
{} as ConfigModule,
Expand All @@ -73,7 +75,7 @@ describe('Auth0 admin strategy verify callback', function () {
clientSecret: 'fake',
admin: { callbackUrl: '/fakeCallbackUrl' },
} as Auth0Options,
'admin'
'admin',
);
});

Expand All @@ -90,7 +92,7 @@ describe('Auth0 admin strategy verify callback', function () {
expect(data).toEqual(
expect.objectContaining({
id: 'test2',
})
}),
);
});

Expand Down Expand Up @@ -128,8 +130,9 @@ describe('Auth0 admin strategy verify callback', function () {
});
});

describe('when strict is set for store only', function () {
describe('when strict is set for store only', function() {
beforeEach(() => {
const Auth0AdminStrategy = getAuth0AdminStrategy('test');
auth0AdminStrategy = new Auth0AdminStrategy(
container,
{} as ConfigModule,
Expand All @@ -139,7 +142,7 @@ describe('Auth0 admin strategy verify callback', function () {
clientSecret: 'fake',
admin: { callbackUrl: '/fakeCallbackUrl' },
} as Auth0Options,
'store'
'store',
);
});

Expand All @@ -156,7 +159,7 @@ describe('Auth0 admin strategy verify callback', function () {
expect(data).toEqual(
expect.objectContaining({
id: 'test2',
})
}),
);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Auth0StoreStrategy } from '../../store';
import { ConfigModule, MedusaContainer } from '@medusajs/medusa/dist/types/global';
import { AUTH_PROVIDER_KEY, CUSTOMER_METADATA_KEY } from '../../../../types';
import { Auth0Options, AUTH0_STORE_STRATEGY_NAME, Profile, ExtraParams } from '../../types';
import { AUTH_PROVIDER_KEY, CUSTOMER_METADATA_KEY, IStrategy } from '../../../../types';
import { AUTH0_STORE_STRATEGY_NAME, Auth0Options, ExtraParams } from '../../types';
import { Profile } from 'passport-auth0';
import { getAuth0StoreStrategy } from '../../store';

describe('Auth0 store strategy verify callback', function () {
describe('Auth0 store strategy verify callback', function() {
const existsEmail = '[email protected]';
const existsEmailWithMeta = '[email protected]';
const existsEmailWithMetaAndProviderKey = '[email protected]';
Expand All @@ -13,9 +14,9 @@ describe('Auth0 store strategy verify callback', function () {
let req: Request;
let accessToken: string;
let refreshToken: string;
let profile: Profile;
let profile: Partial<Profile>;
let extraParams: ExtraParams;
let auth0StoreStrategy: Auth0StoreStrategy;
let auth0StoreStrategy: IStrategy;
let updateFn;
let createFn;

Expand All @@ -36,12 +37,12 @@ describe('Auth0 store strategy verify callback', function () {
resolve: <T>(name: string): T => {
const container_ = {
manager: {
transaction: function (cb) {
transaction: function(cb) {
return cb();
},
},
customerService: {
withTransaction: function () {
withTransaction: function() {
return this;
},
create: createFn,
Expand All @@ -67,7 +68,7 @@ describe('Auth0 store strategy verify callback', function () {
id: 'test3',
metadata: {
[CUSTOMER_METADATA_KEY]: true,
[AUTH_PROVIDER_KEY]: AUTH0_STORE_STRATEGY_NAME,
[AUTH_PROVIDER_KEY]: AUTH0_STORE_STRATEGY_NAME + '_test',
},
};
}
Expand All @@ -92,8 +93,9 @@ describe('Auth0 store strategy verify callback', function () {
} as MedusaContainer;
});

describe('when strict is set to store', function () {
describe('when strict is set to store', function() {
beforeEach(() => {
const Auth0StoreStrategy = getAuth0StoreStrategy('test');
auth0StoreStrategy = new Auth0StoreStrategy(
container,
{} as ConfigModule,
Expand All @@ -103,7 +105,7 @@ describe('Auth0 store strategy verify callback', function () {
clientSecret: 'fake',
store: { callbackUrl: '/fakeCallbackUrl' },
} as Auth0Options,
'store'
'store',
);
});

Expand All @@ -120,7 +122,7 @@ describe('Auth0 store strategy verify callback', function () {
expect(data).toEqual(
expect.objectContaining({
id: 'test3',
})
}),
);
});

Expand All @@ -144,7 +146,7 @@ describe('Auth0 store strategy verify callback', function () {
expect(data).toEqual(
expect.objectContaining({
id: 'test2',
})
}),
);
expect(updateFn).toHaveBeenCalledTimes(1);
});
Expand All @@ -158,7 +160,7 @@ describe('Auth0 store strategy verify callback', function () {
.validate(req, accessToken, refreshToken, extraParams, profile)
.catch((err) => err);
expect(err).toEqual(
new Error(`Customer with email ${existsEmailWithMetaButWrongProviderKey} already exists`)
new Error(`Customer with email ${existsEmailWithMetaButWrongProviderKey} already exists`),
);
});

Expand All @@ -175,14 +177,15 @@ describe('Auth0 store strategy verify callback', function () {
expect(data).toEqual(
expect.objectContaining({
id: 'test',
})
}),
);
expect(createFn).toHaveBeenCalledTimes(1);
});
});

describe('when strict is set to admin', function () {
describe('when strict is set to admin', function() {
beforeEach(() => {
const Auth0StoreStrategy = getAuth0StoreStrategy('test');
auth0StoreStrategy = new Auth0StoreStrategy(
container,
{} as ConfigModule,
Expand All @@ -192,7 +195,7 @@ describe('Auth0 store strategy verify callback', function () {
clientSecret: 'fake',
store: { callbackUrl: '/fakeCallbackUrl' },
} as Auth0Options,
'admin'
'admin',
);
});

Expand All @@ -209,7 +212,7 @@ describe('Auth0 store strategy verify callback', function () {
expect(data).toEqual(
expect.objectContaining({
id: 'test3',
})
}),
);
});

Expand All @@ -222,7 +225,7 @@ describe('Auth0 store strategy verify callback', function () {
expect(data).toEqual(
expect.objectContaining({
id: 'test',
})
}),
);
});

Expand All @@ -235,7 +238,7 @@ describe('Auth0 store strategy verify callback', function () {
expect(data).toEqual(
expect.objectContaining({
id: 'test2',
})
}),
);
expect(updateFn).toHaveBeenCalledTimes(1);
});
Expand All @@ -249,7 +252,7 @@ describe('Auth0 store strategy verify callback', function () {
expect(data).toEqual(
expect.objectContaining({
id: 'test4',
})
}),
);
});

Expand All @@ -266,7 +269,7 @@ describe('Auth0 store strategy verify callback', function () {
expect(data).toEqual(
expect.objectContaining({
id: 'test',
})
}),
);
expect(createFn).toHaveBeenCalledTimes(1);
});
Expand Down
Loading

0 comments on commit 27c6fc1

Please sign in to comment.