Skip to content

Commit

Permalink
feat: add support for strict options (adrien2p#84)
Browse files Browse the repository at this point in the history
* feat: add support for strict options

* chore: Make strict optional

* feat: update strict values

* more tests

* update docs

* update last tests

* lint
  • Loading branch information
adrien2p authored Jun 26, 2023
1 parent d1642cc commit 3e18cb3
Show file tree
Hide file tree
Showing 36 changed files with 1,613 additions and 617 deletions.
3 changes: 2 additions & 1 deletion docs/pages/authentication/auth0.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ newly added plugins. To do so here are the steps
{
resolve: "medusa-plugin-auth",
options: {
// strict: "all", // or "none" or "store" or "admin"
auth0: {
clientID: Auth0ClientId,
clientSecret: Auth0ClientSecret,
Expand Down Expand Up @@ -84,7 +85,7 @@ newly added plugins. To do so here are the steps

### Default behaviour

The default `verifyCallback` flow looks as follow,
The default `verifyCallback` flow looks as follow (unless the `strict` option is changed to `none` or `store` or `admin` depending on the targeted domain)
- for the `admin`
- if the user trying to authenticate exists
- then we are looking in the metadata to find if the strategy identifier is present in `authProvider`.
Expand Down
3 changes: 2 additions & 1 deletion docs/pages/authentication/azureoidc.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ newly added plugins. To do so here are the steps
resolve: "medusa-plugin-auth",
options: {
azure_oidc: {
// strict: "all", // or "none" or "store" or "admin"
admin: {
identityMetadata: AzureIdentityMetadata,
clientID: AzureClientId,
Expand Down Expand Up @@ -115,7 +116,7 @@ It has only been tested with default options. As of now only ResponseType.Code a

### Default behaviour

The default `verifyCallback` flow looks as follow,
The default `verifyCallback` flow looks as follow (unless the `strict` option is changed to `none` or `store` or `admin` depending on the targeted domain)
- for the `admin`
- if the user trying to authenticate exists
- then we are looking in the metadata to find if the strategy identifier is present in `authProvider`.
Expand Down
3 changes: 2 additions & 1 deletion docs/pages/authentication/facebook.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ newly added plugins. To do so here are the steps
{
resolve: "medusa-plugin-auth",
options: {
// strict: "all", // or "none" or "store" or "admin"
facebook: {
clientID: FacebookClientId,
clientSecret: FacebookClientSecret,
Expand Down Expand Up @@ -90,7 +91,7 @@ newly added plugins. To do so here are the steps

### Default behaviour

The default `verifyCallback` flow looks as follow,
The default `verifyCallback` flow looks as follow (unless the `strict` option is changed to `none` or `store` or `admin` depending on the targeted domain)
- for the `admin`
- if the user trying to authenticate exists
- then we are looking in the metadata to find if the strategy identifier is present in `authProvider`.
Expand Down
3 changes: 2 additions & 1 deletion docs/pages/authentication/firebase.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ newly added plugins. To do so here are the steps
{
resolve: "medusa-plugin-auth",
options: {
// strict: "all", // or "none" or "store" or "admin"
firebase: {
credentialJsonPath: CredentialJsonPath,

Expand Down Expand Up @@ -78,7 +79,7 @@ newly added plugins. To do so here are the steps

### Default behaviour

The default `verifyCallback` flow looks as follow,
The default `verifyCallback` flow looks as follow (unless the `strict` option is changed to `none` or `store` or `admin` depending on the targeted domain)
- for the `admin`
- if the user trying to authenticate exists
- then we are looking in the metadata to find if the strategy identifier is present in `authProvider`.
Expand Down
3 changes: 2 additions & 1 deletion docs/pages/authentication/google.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ newly added plugins. To do so here are the steps
{
resolve: "medusa-plugin-auth",
options: {
// strict: "all", // or "none" or "store" or "admin"
google: {
clientID: GoogleClientId,
clientSecret: GoogleClientSecret,
Expand Down Expand Up @@ -90,7 +91,7 @@ newly added plugins. To do so here are the steps

### Default behaviour

The default `verifyCallback` flow looks as follow,
The default `verifyCallback` flow looks as follow (unless the `strict` option is changed to `none` or `store` or `admin` depending on the targeted domain)
- for the `admin`
- if the user trying to authenticate exists
- then we are looking in the metadata to find if the strategy identifier is present in `authProvider`.
Expand Down
3 changes: 2 additions & 1 deletion docs/pages/authentication/linkedin.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ newly added plugins. To do so here are the steps
```js
{
resolve: "medusa-plugin-auth",
// strict: "all", // or "none" or "store" or "admin"
options: {
linkedin: {
clientID: LinkedinClientId,
Expand Down Expand Up @@ -90,7 +91,7 @@ newly added plugins. To do so here are the steps

### Default behaviour

The default `verifyCallback` flow looks as follow,
The default `verifyCallback` flow looks as follow (unless the `strict` option is changed to `none` or `store` or `admin` depending on the targeted domain)
- for the `admin`
- if the user trying to authenticate exists
- then we are looking in the metadata to find if the strategy identifier is present in `authProvider`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,66 +60,139 @@ describe('Auth0 admin strategy verify callback', function () {
return container_[name];
},
} as MedusaContainer;

auth0AdminStrategy = new Auth0AdminStrategy(
container,
{} as ConfigModule,
{
auth0Domain: 'fakeDomain',
clientID: 'fake',
clientSecret: 'fake',
admin: { callbackUrl: '/fakeCallbackUrl' },
} as Auth0Options
);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should succeed', async () => {
profile = {
emails: [{ value: existsEmailWithProviderKey }],
};

const data = await auth0AdminStrategy.validate(req, accessToken, refreshToken, extraParams, profile);
expect(data).toEqual(
expect.objectContaining({
id: 'test2',
})
);
});

it('should fail when a user exists without the auth provider metadata', async () => {
profile = {
emails: [{ value: existsEmail }],
};

const err = await auth0AdminStrategy
.validate(req, accessToken, refreshToken, extraParams, profile)
.catch((err) => err);
expect(err).toEqual(new Error(`Admin with email ${existsEmail} already exists`));
});

it('should fail when a user exists with the wrong auth provider key', async () => {
profile = {
emails: [{ value: existsEmailWithWrongProviderKey }],
};

const err = await auth0AdminStrategy
.validate(req, accessToken, refreshToken, extraParams, profile)
.catch((err) => err);
expect(err).toEqual(new Error(`Admin with email ${existsEmailWithWrongProviderKey} already exists`));
describe('when strict is set to admin', function () {
beforeEach(() => {
auth0AdminStrategy = new Auth0AdminStrategy(
container,
{} as ConfigModule,
{
auth0Domain: 'fakeDomain',
clientID: 'fake',
clientSecret: 'fake',
admin: { callbackUrl: '/fakeCallbackUrl' },
} as Auth0Options,
'admin'
);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should succeed', async () => {
profile = {
emails: [{ value: existsEmailWithProviderKey }],
};

const data = await auth0AdminStrategy.validate(req, accessToken, refreshToken, extraParams, profile);
expect(data).toEqual(
expect.objectContaining({
id: 'test2',
})
);
});

it('should fail when a user exists without the auth provider metadata', async () => {
profile = {
emails: [{ value: existsEmail }],
};

const err = await auth0AdminStrategy
.validate(req, accessToken, refreshToken, extraParams, profile)
.catch((err) => err);
expect(err).toEqual(new Error(`Admin with email ${existsEmail} already exists`));
});

it('should fail when a user exists with the wrong auth provider key', async () => {
profile = {
emails: [{ value: existsEmailWithWrongProviderKey }],
};

const err = await auth0AdminStrategy
.validate(req, accessToken, refreshToken, extraParams, profile)
.catch((err) => err);
expect(err).toEqual(new Error(`Admin with email ${existsEmailWithWrongProviderKey} already exists`));
});

it('should fail when the user does not exist', async () => {
profile = {
emails: [{ value: 'fake' }],
};

const err = await auth0AdminStrategy
.validate(req, accessToken, refreshToken, extraParams, profile)
.catch((err) => err);
expect(err).toEqual(new Error(`Unable to authenticate the user with the email fake`));
});
});

it('should fail when the user does not exist', async () => {
profile = {
emails: [{ value: 'fake' }],
};

const err = await auth0AdminStrategy
.validate(req, accessToken, refreshToken, extraParams, profile)
.catch((err) => err);
expect(err).toEqual(new Error(`Unable to authenticate the user with the email fake`));
describe('when strict is set for store only', function () {
beforeEach(() => {
auth0AdminStrategy = new Auth0AdminStrategy(
container,
{} as ConfigModule,
{
auth0Domain: 'fakeDomain',
clientID: 'fake',
clientSecret: 'fake',
admin: { callbackUrl: '/fakeCallbackUrl' },
} as Auth0Options,
'store'
);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should succeed', async () => {
profile = {
emails: [{ value: existsEmailWithProviderKey }],
};

const data = await auth0AdminStrategy.validate(req, accessToken, refreshToken, extraParams, profile);
expect(data).toEqual(
expect.objectContaining({
id: 'test2',
})
);
});

it('should succeed when a user exists without the auth provider metadata', async () => {
profile = {
emails: [{ value: existsEmail }],
};

const data = await auth0AdminStrategy.validate(req, accessToken, refreshToken, extraParams, profile);
expect(data).toEqual({
accessToken: undefined,
id: 'test',
});
});

it('should succeed when a user exists with the wrong auth provider key', async () => {
profile = {
emails: [{ value: existsEmailWithWrongProviderKey }],
};

const data = await auth0AdminStrategy.validate(req, accessToken, refreshToken, extraParams, profile);
expect(data).toEqual({
accessToken: undefined,
id: 'test3',
});
});

it('should fail when the user does not exist', async () => {
profile = {
emails: [{ value: 'fake' }],
};

const err = await auth0AdminStrategy
.validate(req, accessToken, refreshToken, extraParams, profile)
.catch((err) => err);
expect(err).toEqual(new Error(`Unable to authenticate the user with the email fake`));
});
});
});
Loading

0 comments on commit 3e18cb3

Please sign in to comment.