Revamp auth module to support multiple providers linked to a single auth identity (#7521)

This commit is contained in:
Stevche Radevski
2024-06-05 07:47:16 +00:00
committed by GitHub
parent 20cd6a7b51
commit fafc92b875
23 changed files with 640 additions and 216 deletions
@@ -1,34 +1,36 @@
import { IAuthModuleService } from "@medusajs/types"
import { AuthIdentity } from "@models"
import { SqlEntityManager } from "@mikro-orm/postgresql"
export async function createAuthIdentities(
manager: SqlEntityManager,
service: IAuthModuleService,
userData: any[] = [
{
id: "test-id",
entity_id: "test-id",
provider: "manual",
provider_identities: [
{
entity_id: "test-id",
provider: "manual",
},
],
},
{
id: "test-id-1",
entity_id: "test-id-1",
provider: "manual",
provider_identities: [
{
entity_id: "test-id-1",
provider: "manual",
},
],
},
{
entity_id: "test-id-2",
provider: "store",
provider_identities: [
{
entity_id: "test-id-2",
provider: "store",
},
],
},
]
): Promise<AuthIdentity[]> {
const authIdentities: AuthIdentity[] = []
for (const user of userData) {
const authIdentity = manager.create(AuthIdentity, user)
authIdentities.push(authIdentity)
}
await manager.persistAndFlush(authIdentities)
return authIdentities
return await service.create(userData)
}
@@ -23,10 +23,14 @@ export class AuthServiceFixtures extends AbstractAuthModuleProvider {
try {
authIdentity = await service.retrieve({
entity_id: email,
provider: this.provider,
})
if (authIdentity.provider_metadata?.password === password) {
// The provider has to be present, guaranteed by the retrieve filter above.
const providerIdentity = authIdentity.provider_identities?.find(
(pi) => pi.provider === this.provider
)!
if (providerIdentity.provider_metadata?.password === password) {
return {
success: true,
authIdentity,
@@ -36,7 +40,6 @@ export class AuthServiceFixtures extends AbstractAuthModuleProvider {
if (error.type === MedusaError.Types.NOT_FOUND) {
const createdAuthIdentity = await service.create({
entity_id: email,
provider: this.provider,
provider_metadata: {
password,
},
@@ -13,22 +13,31 @@ moduleIntegrationTestRunner({
}: SuiteOptions<IAuthModuleService>) => {
describe("AuthModuleService - AuthIdentity", () => {
beforeEach(async () => {
await createAuthIdentities(MikroOrmWrapper.forkManager())
await createAuthIdentities(service)
})
describe("listAuthIdentities", () => {
it("should list authIdentities", async () => {
const authIdentities = await service.list()
const authIdentities = await service.list(
{},
{ relations: ["provider_identities"] }
)
expect(authIdentities).toEqual([
expect.objectContaining({
provider: "store",
provider_identities: [
expect.objectContaining({ provider: "store" }),
],
}),
expect.objectContaining({
provider: "manual",
provider_identities: [
expect.objectContaining({ provider: "manual" }),
],
}),
expect.objectContaining({
provider: "manual",
provider_identities: [
expect.objectContaining({ provider: "manual" }),
],
}),
])
})
@@ -47,7 +56,9 @@ moduleIntegrationTestRunner({
it("should list authIdentities by provider", async () => {
const authIdentities = await service.list({
provider: "manual",
provider_identities: {
provider: "manual",
},
})
expect(authIdentities).toEqual([
@@ -63,25 +74,34 @@ moduleIntegrationTestRunner({
describe("listAndCountAuthIdentities", () => {
it("should list and count authIdentities", async () => {
const [authIdentities, count] = await service.listAndCount()
const [authIdentities, count] = await service.listAndCount(
{},
{ relations: ["provider_identities"] }
)
expect(count).toEqual(3)
expect(authIdentities).toEqual([
expect.objectContaining({
provider: "store",
provider_identities: [
expect.objectContaining({ provider: "store" }),
],
}),
expect.objectContaining({
provider: "manual",
provider_identities: [
expect.objectContaining({ provider: "manual" }),
],
}),
expect.objectContaining({
provider: "manual",
provider_identities: [
expect.objectContaining({ provider: "manual" }),
],
}),
])
})
it("should listAndCount authIdentities by provider_id", async () => {
const [authIdentities, count] = await service.listAndCount({
provider: "manual",
provider_identities: { provider: "manual" },
})
expect(count).toEqual(2)
@@ -131,7 +151,11 @@ moduleIntegrationTestRunner({
id: "test-id-1",
})
)
expect(authIdentity["password_hash"]).toEqual(undefined)
expect(
authIdentity.provider_identities?.[0].provider_metadata?.[
"password_hash"
]
).toEqual(undefined)
})
it("should throw an error when a authIdentityId is not provided", async () => {
@@ -196,14 +220,14 @@ moduleIntegrationTestRunner({
await service.update([
{
id,
provider_metadata: { email: "test@email.com" },
app_metadata: { email: "test@email.com" },
},
])
const [authIdentity] = await service.list({ id: [id] })
expect(authIdentity).toEqual(
expect.objectContaining({
provider_metadata: { email: "test@email.com" },
app_metadata: { email: "test@email.com" },
})
)
})
@@ -214,8 +238,12 @@ moduleIntegrationTestRunner({
await service.create([
{
id: "test",
provider: "manual",
entity_id: "test",
provider_identities: [
{
provider: "manual",
entity_id: "test",
},
],
},
])
@@ -28,11 +28,15 @@ moduleIntegrationTestRunner({
describe("Auth Module Service", () => {
beforeEach(async () => {
await service.create({
entity_id: "test@admin.com",
provider: "plaintextpass",
provider_metadata: {
password: "plaintext",
},
provider_identities: [
{
entity_id: "test@admin.com",
provider: "plaintextpass",
provider_metadata: {
password: "plaintext",
},
},
],
})
})
@@ -65,7 +69,9 @@ moduleIntegrationTestRunner({
success: true,
authIdentity: expect.objectContaining({
id: expect.any(String),
entity_id: "test@admin.com",
provider_identities: [
expect.objectContaining({ entity_id: "test@admin.com" }),
],
}),
})
)
@@ -97,12 +103,17 @@ moduleIntegrationTestRunner({
},
})
const dbAuthIdentity = await service.retrieve(result.authIdentity.id)
const dbAuthIdentity = await service.retrieve(
result.authIdentity?.id!,
{ relations: ["provider_identities"] }
)
expect(dbAuthIdentity).toEqual(
expect.objectContaining({
id: expect.any(String),
entity_id: "new@admin.com",
provider_identities: [
expect.objectContaining({ entity_id: "new@admin.com" }),
],
})
)
})