forked from adrien2p/medusa-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Auth0): Add Auth0 Authentication (adrien2p#27)
* feat(Auth0): Add Auth0 Authentication The following PR enables authentication via Auth0. Required: `auth0Domain` in the MedusaConfig. * Add Store Authentication * Update Auth0 Admin and Store authentication * . * Rebase and add legacy authentication * Update README * . * Update Authentication Plugin tests * Update README * Updated Tests and re-added Legacy authentication default * . * Add API Route and Further testing * Update ReadMe * . * Update tests * . * . * . * . * .
- Loading branch information
Showing
27 changed files
with
1,104 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.idea | ||
.vscode | ||
|
||
/api | ||
/core | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...ages/medusa-plugin-auth/src/auth-strategies/auth0/__tests__/admin/verify-callback.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
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'; | ||
|
||
describe('Auth0 admin strategy verify callback', function () { | ||
const existsEmail = 'exists@test.fr'; | ||
const existsEmailWithProviderKey = 'exist3s@test.fr'; | ||
const existsEmailWithWrongProviderKey = 'exist4s@test.fr'; | ||
|
||
let container: MedusaContainer; | ||
let req: Request; | ||
let accessToken: string; | ||
let refreshToken: string; | ||
let profile: Profile; | ||
let extraParams: ExtraParams; | ||
let auth0AdminStrategy: Auth0AdminStrategy; | ||
|
||
beforeEach(() => { | ||
profile = { | ||
emails: [{ value: existsEmail }], | ||
}; | ||
|
||
extraParams = {}; | ||
|
||
container = { | ||
resolve: (name: string) => { | ||
const container_ = { | ||
userService: { | ||
retrieveByEmail: jest.fn().mockImplementation(async (email: string) => { | ||
if (email === existsEmail) { | ||
return { | ||
id: 'test', | ||
}; | ||
} | ||
|
||
if (email === existsEmailWithProviderKey) { | ||
return { | ||
id: 'test2', | ||
metadata: { | ||
[AUTH_PROVIDER_KEY]: AUTH0_ADMIN_STRATEGY_NAME | ||
}, | ||
}; | ||
} | ||
|
||
if (email === existsEmailWithWrongProviderKey) { | ||
return { | ||
id: 'test3', | ||
metadata: { | ||
[AUTH_PROVIDER_KEY]: 'fake_provider_key' | ||
}, | ||
}; | ||
} | ||
|
||
return; | ||
}), | ||
}, | ||
}; | ||
|
||
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`)); | ||
}); | ||
|
||
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`)); | ||
}); | ||
}); |
Oops, something went wrong.