Feat(auth): Remove auth provider entity (#6314)
**What** - remove auth provider entity **Why** - The auth provider entity was not really used anywhere **How** - Keeping loader behavior as is but removing the Co-authored-by: Sebastian Rindom <7554214+srindom@users.noreply.github.com>
This commit is contained in:
1 parent
b2eaac8cb1
commit
882aa549bd
37 files changed
+179
-1223
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/utils": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat(utils): return array or single entity based on input for modules created by internal module service factory
|
||||||
@@ -40,12 +40,7 @@ describe("POST /store/customers/me/addresses", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("should create a customer address", async () => {
|
it("should create a customer address", async () => {
|
||||||
const { jwt_secret } = appContainer.resolve("configModule").projectConfig
|
const { customer, jwt } = await createAuthenticatedCustomer(appContainer)
|
||||||
const { customer, jwt } = await createAuthenticatedCustomer(
|
|
||||||
customerModuleService,
|
|
||||||
appContainer.resolve(ModuleRegistrationName.AUTH),
|
|
||||||
jwt_secret
|
|
||||||
)
|
|
||||||
|
|
||||||
const api = useApi() as any
|
const api = useApi() as any
|
||||||
const response = await api.post(
|
const response = await api.post(
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ describe("POST /store/customers", () => {
|
|||||||
ModuleRegistrationName.AUTH
|
ModuleRegistrationName.AUTH
|
||||||
)
|
)
|
||||||
const { jwt_secret } = appContainer.resolve("configModule").projectConfig
|
const { jwt_secret } = appContainer.resolve("configModule").projectConfig
|
||||||
const authUser = await authService.createAuthUser({
|
const authUser = await authService.create({
|
||||||
entity_id: "store_user",
|
entity_id: "store_user",
|
||||||
provider: "emailpass",
|
provider: "emailpass",
|
||||||
scope: "store",
|
scope: "store",
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ import { useApi } from "../../../../environment-helpers/use-api"
|
|||||||
|
|
||||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||||
|
|
||||||
|
jest.setTimeout(50000)
|
||||||
|
|
||||||
describe("DELETE /store/customers/me/addresses/:address_id", () => {
|
describe("DELETE /store/customers/me/addresses/:address_id", () => {
|
||||||
let dbConnection
|
let dbConnection
|
||||||
let appContainer
|
let appContainer
|
||||||
@@ -26,14 +28,6 @@ describe("DELETE /store/customers/me/addresses/:address_id", () => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO: delete with removal of authProvider
|
|
||||||
beforeEach(async () => {
|
|
||||||
const onStart =
|
|
||||||
appContainer.resolve(ModuleRegistrationName.AUTH).__hooks
|
|
||||||
.onApplicationStart ?? (() => Promise.resolve())
|
|
||||||
await onStart()
|
|
||||||
})
|
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
const db = useDb()
|
const db = useDb()
|
||||||
await db.shutdown()
|
await db.shutdown()
|
||||||
@@ -46,12 +40,7 @@ describe("DELETE /store/customers/me/addresses/:address_id", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("should delete a customer address", async () => {
|
it("should delete a customer address", async () => {
|
||||||
const { jwt_secret } = appContainer.resolve("configModule").projectConfig
|
const { customer, jwt } = await createAuthenticatedCustomer(appContainer)
|
||||||
const { customer, jwt } = await createAuthenticatedCustomer(
|
|
||||||
customerModuleService,
|
|
||||||
appContainer.resolve(ModuleRegistrationName.AUTH),
|
|
||||||
jwt_secret
|
|
||||||
)
|
|
||||||
|
|
||||||
const address = await customerModuleService.addAddresses({
|
const address = await customerModuleService.addAddresses({
|
||||||
customer_id: customer.id,
|
customer_id: customer.id,
|
||||||
@@ -76,12 +65,7 @@ describe("DELETE /store/customers/me/addresses/:address_id", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("should fail to delete another customer's address", async () => {
|
it("should fail to delete another customer's address", async () => {
|
||||||
const { jwt_secret } = appContainer.resolve("configModule").projectConfig
|
const { jwt } = await createAuthenticatedCustomer(appContainer)
|
||||||
const { jwt } = await createAuthenticatedCustomer(
|
|
||||||
customerModuleService,
|
|
||||||
appContainer.resolve(ModuleRegistrationName.AUTH),
|
|
||||||
jwt_secret
|
|
||||||
)
|
|
||||||
|
|
||||||
const otherCustomer = await customerModuleService.create({
|
const otherCustomer = await customerModuleService.create({
|
||||||
first_name: "Jane",
|
first_name: "Jane",
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
|||||||
import { ICustomerModuleService } from "@medusajs/types"
|
import { ICustomerModuleService } from "@medusajs/types"
|
||||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||||
import { createAuthenticatedCustomer } from "../../../helpers/create-authenticated-customer"
|
import { createAuthenticatedCustomer } from "../../../helpers/create-authenticated-customer"
|
||||||
import customer from "../../../../development/database/customer"
|
|
||||||
import { getContainer } from "../../../../environment-helpers/use-container"
|
import { getContainer } from "../../../../environment-helpers/use-container"
|
||||||
import path from "path"
|
import path from "path"
|
||||||
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
|
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
|
||||||
@@ -41,12 +40,7 @@ describe("GET /store/customers", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("should retrieve auth user's customer", async () => {
|
it("should retrieve auth user's customer", async () => {
|
||||||
const { jwt_secret } = appContainer.resolve("configModule").projectConfig
|
const { customer, jwt } = await createAuthenticatedCustomer(appContainer)
|
||||||
const { customer, jwt } = await createAuthenticatedCustomer(
|
|
||||||
customerModuleService,
|
|
||||||
appContainer.resolve(ModuleRegistrationName.AUTH),
|
|
||||||
jwt_secret
|
|
||||||
)
|
|
||||||
|
|
||||||
const api = useApi() as any
|
const api = useApi() as any
|
||||||
const response = await api.get(`/store/customers/me`, {
|
const response = await api.get(`/store/customers/me`, {
|
||||||
|
|||||||
@@ -44,12 +44,7 @@ describe("GET /store/customers/me/addresses", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("should get all customer addresses and its count", async () => {
|
it("should get all customer addresses and its count", async () => {
|
||||||
const { jwt_secret } = appContainer.resolve("configModule").projectConfig
|
const { customer, jwt } = await createAuthenticatedCustomer(appContainer)
|
||||||
const { customer, jwt } = await createAuthenticatedCustomer(
|
|
||||||
customerModuleService,
|
|
||||||
appContainer.resolve(ModuleRegistrationName.AUTH),
|
|
||||||
jwt_secret
|
|
||||||
)
|
|
||||||
|
|
||||||
await customerModuleService.addAddresses([
|
await customerModuleService.addAddresses([
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -26,14 +26,6 @@ describe("POST /store/customers/:id/addresses/:address_id", () => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO: delete with removal of authProvider
|
|
||||||
beforeEach(async () => {
|
|
||||||
const onStart =
|
|
||||||
appContainer.resolve(ModuleRegistrationName.AUTH).__hooks
|
|
||||||
.onApplicationStart ?? (() => Promise.resolve())
|
|
||||||
await onStart()
|
|
||||||
})
|
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
const db = useDb()
|
const db = useDb()
|
||||||
await db.shutdown()
|
await db.shutdown()
|
||||||
@@ -46,13 +38,7 @@ describe("POST /store/customers/:id/addresses/:address_id", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("should update a customer address", async () => {
|
it("should update a customer address", async () => {
|
||||||
const { jwt_secret } = appContainer.resolve("configModule").projectConfig
|
const { customer, jwt } = await createAuthenticatedCustomer(appContainer)
|
||||||
|
|
||||||
const { customer, jwt } = await createAuthenticatedCustomer(
|
|
||||||
customerModuleService,
|
|
||||||
appContainer.resolve(ModuleRegistrationName.AUTH),
|
|
||||||
jwt_secret
|
|
||||||
)
|
|
||||||
|
|
||||||
const address = await customerModuleService.addAddresses({
|
const address = await customerModuleService.addAddresses({
|
||||||
customer_id: customer.id,
|
customer_id: customer.id,
|
||||||
@@ -81,13 +67,7 @@ describe("POST /store/customers/:id/addresses/:address_id", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("should fail to update another customer's address", async () => {
|
it("should fail to update another customer's address", async () => {
|
||||||
const { jwt_secret } = appContainer.resolve("configModule").projectConfig
|
const { jwt } = await createAuthenticatedCustomer(appContainer)
|
||||||
|
|
||||||
const { jwt } = await createAuthenticatedCustomer(
|
|
||||||
customerModuleService,
|
|
||||||
appContainer.resolve(ModuleRegistrationName.AUTH),
|
|
||||||
jwt_secret
|
|
||||||
)
|
|
||||||
|
|
||||||
const otherCustomer = await customerModuleService.create({
|
const otherCustomer = await customerModuleService.create({
|
||||||
first_name: "Jane",
|
first_name: "Jane",
|
||||||
|
|||||||
@@ -1,26 +1,33 @@
|
|||||||
import { IAuthModuleService, ICustomerModuleService } from "@medusajs/types"
|
import { CreateCustomerDTO, MedusaContainer } from "@medusajs/types"
|
||||||
|
|
||||||
|
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||||
import jwt from "jsonwebtoken"
|
import jwt from "jsonwebtoken"
|
||||||
|
|
||||||
export const createAuthenticatedCustomer = async (
|
export const createAuthenticatedCustomer = async (
|
||||||
customerModuleService: ICustomerModuleService,
|
appContainer: MedusaContainer,
|
||||||
authService: IAuthModuleService,
|
customerData: Partial<CreateCustomerDTO> = {}
|
||||||
jwtSecret: string
|
|
||||||
) => {
|
) => {
|
||||||
|
const { jwt_secret } = appContainer.resolve("configModule").projectConfig
|
||||||
|
const authService = appContainer.resolve(ModuleRegistrationName.AUTH)
|
||||||
|
const customerModuleService = appContainer.resolve(
|
||||||
|
ModuleRegistrationName.CUSTOMER
|
||||||
|
)
|
||||||
|
|
||||||
const customer = await customerModuleService.create({
|
const customer = await customerModuleService.create({
|
||||||
first_name: "John",
|
first_name: "John",
|
||||||
last_name: "Doe",
|
last_name: "Doe",
|
||||||
email: "john@me.com",
|
email: "john@me.com",
|
||||||
|
...customerData,
|
||||||
})
|
})
|
||||||
|
|
||||||
const authUser = await authService.createAuthUser({
|
const authUser = await authService.create({
|
||||||
entity_id: "store_user",
|
entity_id: "store_user",
|
||||||
provider: "emailpass",
|
provider: "emailpass",
|
||||||
scope: "store",
|
scope: "store",
|
||||||
app_metadata: { customer_id: customer.id },
|
app_metadata: { customer_id: customer.id },
|
||||||
})
|
})
|
||||||
|
|
||||||
const token = jwt.sign(authUser, jwtSecret)
|
const token = jwt.sign(authUser, jwt_secret)
|
||||||
|
|
||||||
return { customer, authUser, jwt: token }
|
return { customer, authUser, jwt: token }
|
||||||
}
|
}
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
|
||||||
import { AuthProvider } from "@models"
|
|
||||||
|
|
||||||
export async function createAuthProviders(
|
|
||||||
manager: SqlEntityManager,
|
|
||||||
providerData: any[] = [
|
|
||||||
{
|
|
||||||
provider: "manual",
|
|
||||||
name: "manual",
|
|
||||||
is_active: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
provider: "disabled",
|
|
||||||
name: "disabled",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
provider: "store",
|
|
||||||
name: "store",
|
|
||||||
domain: "store",
|
|
||||||
is_active: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
provider: "admin",
|
|
||||||
name: "admin",
|
|
||||||
domain: "admin",
|
|
||||||
is_active: true,
|
|
||||||
},
|
|
||||||
]
|
|
||||||
): Promise<AuthProvider[]> {
|
|
||||||
const authProviders: AuthProvider[] = []
|
|
||||||
|
|
||||||
for (const provider of providerData) {
|
|
||||||
const authProvider = manager.create(AuthProvider, provider)
|
|
||||||
|
|
||||||
authProviders.push(authProvider)
|
|
||||||
}
|
|
||||||
|
|
||||||
await manager.persistAndFlush(authProviders)
|
|
||||||
|
|
||||||
return authProviders
|
|
||||||
}
|
|
||||||
@@ -1,271 +0,0 @@
|
|||||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
|
||||||
|
|
||||||
import { MikroOrmWrapper } from "../../../utils"
|
|
||||||
import { createAuthProviders } from "../../../__fixtures__/auth-provider"
|
|
||||||
import { createMedusaContainer } from "@medusajs/utils"
|
|
||||||
import { asValue } from "awilix"
|
|
||||||
import ContainerLoader from "../../../../src/loaders/container"
|
|
||||||
import { ModulesSdkTypes } from "@medusajs/types"
|
|
||||||
|
|
||||||
jest.setTimeout(30000)
|
|
||||||
|
|
||||||
describe("AuthProvider Service", () => {
|
|
||||||
let service: ModulesSdkTypes.InternalModuleService<any>
|
|
||||||
let testManager: SqlEntityManager
|
|
||||||
let repositoryManager: SqlEntityManager
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
await MikroOrmWrapper.setupDatabase()
|
|
||||||
repositoryManager = await MikroOrmWrapper.forkManager()
|
|
||||||
testManager = await MikroOrmWrapper.forkManager()
|
|
||||||
|
|
||||||
const container = createMedusaContainer()
|
|
||||||
container.register("manager", asValue(repositoryManager))
|
|
||||||
|
|
||||||
await ContainerLoader({ container })
|
|
||||||
|
|
||||||
service = container.resolve("authProviderService")
|
|
||||||
|
|
||||||
await createAuthProviders(testManager)
|
|
||||||
})
|
|
||||||
|
|
||||||
afterEach(async () => {
|
|
||||||
await MikroOrmWrapper.clearDatabase()
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("list", () => {
|
|
||||||
it("should list AuthProviders", async () => {
|
|
||||||
const authProviders = await service.list()
|
|
||||||
const serialized = JSON.parse(JSON.stringify(authProviders))
|
|
||||||
|
|
||||||
expect(serialized).toEqual([
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "manual",
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "disabled",
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "store",
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "admin",
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should list authProviders by provider id", async () => {
|
|
||||||
const authProviders = await service.list({
|
|
||||||
provider: ["manual"],
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(authProviders).toEqual([
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "manual",
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should list active authProviders", async () => {
|
|
||||||
const authProviders = await service.list({
|
|
||||||
is_active: true,
|
|
||||||
})
|
|
||||||
|
|
||||||
const serialized = JSON.parse(JSON.stringify(authProviders))
|
|
||||||
|
|
||||||
expect(serialized).toEqual([
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "manual",
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "store",
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "admin",
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("listAndCount", () => {
|
|
||||||
it("should list AuthProviders", async () => {
|
|
||||||
const [authProviders, count] = await service.listAndCount()
|
|
||||||
const serialized = JSON.parse(JSON.stringify(authProviders))
|
|
||||||
|
|
||||||
expect(count).toEqual(4)
|
|
||||||
expect(serialized).toEqual([
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "manual",
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "disabled",
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "store",
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "admin",
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should listAndCount authProviders by provider", async () => {
|
|
||||||
const [authProviders, count] = await service.listAndCount({
|
|
||||||
provider: ["manual"],
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(count).toEqual(1)
|
|
||||||
expect(authProviders).toEqual([
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "manual",
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should listAndCount active authProviders", async () => {
|
|
||||||
const [authProviders, count] = await service.listAndCount({
|
|
||||||
is_active: true,
|
|
||||||
})
|
|
||||||
|
|
||||||
const serialized = JSON.parse(JSON.stringify(authProviders))
|
|
||||||
|
|
||||||
expect(count).toEqual(3)
|
|
||||||
expect(serialized).toEqual([
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "manual",
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "store",
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "admin",
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("retrieve", () => {
|
|
||||||
const provider = "manual"
|
|
||||||
|
|
||||||
it("should return an authProvider for the given provider", async () => {
|
|
||||||
const authProvider = await service.retrieve(provider)
|
|
||||||
|
|
||||||
expect(authProvider).toEqual(
|
|
||||||
expect.objectContaining({
|
|
||||||
provider,
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should throw an error when an authProvider with the given provider does not exist", async () => {
|
|
||||||
let error
|
|
||||||
|
|
||||||
try {
|
|
||||||
await service.retrieve("does-not-exist")
|
|
||||||
} catch (e) {
|
|
||||||
error = e
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(error.message).toEqual(
|
|
||||||
"AuthProvider with provider: does-not-exist was not found"
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should throw an error when a provider is not provided", async () => {
|
|
||||||
let error
|
|
||||||
|
|
||||||
try {
|
|
||||||
await service.retrieve(undefined as unknown as string)
|
|
||||||
} catch (e) {
|
|
||||||
error = e
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(error.message).toEqual("authProvider - provider must be defined")
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should return authProvider based on config select param", async () => {
|
|
||||||
const authProvider = await service.retrieve(provider, {
|
|
||||||
select: ["provider"],
|
|
||||||
})
|
|
||||||
|
|
||||||
const serialized = JSON.parse(JSON.stringify(authProvider))
|
|
||||||
|
|
||||||
expect(serialized).toEqual({
|
|
||||||
provider,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("delete", () => {
|
|
||||||
const provider = "manual"
|
|
||||||
|
|
||||||
it("should delete the authProviders given a provider successfully", async () => {
|
|
||||||
await service.delete([provider])
|
|
||||||
|
|
||||||
const authProviders = await service.list({
|
|
||||||
provider: [provider],
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(authProviders).toHaveLength(0)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("update", () => {
|
|
||||||
const provider = "manual"
|
|
||||||
|
|
||||||
it("should throw an error when a id does not exist", async () => {
|
|
||||||
let error
|
|
||||||
|
|
||||||
try {
|
|
||||||
await service.update([
|
|
||||||
{
|
|
||||||
provider: "does-not-exist",
|
|
||||||
},
|
|
||||||
])
|
|
||||||
} catch (e) {
|
|
||||||
error = e
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(error.message).toEqual(
|
|
||||||
'AuthProvider with provider "does-not-exist" not found'
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should update authProvider", async () => {
|
|
||||||
await service.update([
|
|
||||||
{
|
|
||||||
provider: "manual",
|
|
||||||
name: "test",
|
|
||||||
},
|
|
||||||
])
|
|
||||||
|
|
||||||
const [provider] = await service.list({ provider: ["manual"] })
|
|
||||||
expect(provider).toEqual(
|
|
||||||
expect.objectContaining({
|
|
||||||
name: "test",
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("create", () => {
|
|
||||||
it("should create a authProvider successfully", async () => {
|
|
||||||
await service.create([
|
|
||||||
{
|
|
||||||
provider: "test",
|
|
||||||
name: "test provider",
|
|
||||||
},
|
|
||||||
])
|
|
||||||
|
|
||||||
const [authProvider] = await service.list({
|
|
||||||
provider: ["test"],
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(authProvider).toEqual(
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "test",
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -3,7 +3,6 @@ import ContainerLoader from "../../../../src/loaders/container"
|
|||||||
import { MikroOrmWrapper } from "../../../utils"
|
import { MikroOrmWrapper } from "../../../utils"
|
||||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||||
import { asValue } from "awilix"
|
import { asValue } from "awilix"
|
||||||
import { createAuthProviders } from "../../../__fixtures__/auth-provider"
|
|
||||||
import { createAuthUsers } from "../../../__fixtures__/auth-user"
|
import { createAuthUsers } from "../../../__fixtures__/auth-user"
|
||||||
import { createMedusaContainer } from "@medusajs/utils"
|
import { createMedusaContainer } from "@medusajs/utils"
|
||||||
|
|
||||||
@@ -26,7 +25,6 @@ describe("AuthUser Service", () => {
|
|||||||
|
|
||||||
service = container.resolve("authUserService")
|
service = container.resolve("authUserService")
|
||||||
|
|
||||||
await createAuthProviders(testManager)
|
|
||||||
await createAuthUsers(testManager)
|
await createAuthUsers(testManager)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -66,7 +64,7 @@ describe("AuthUser Service", () => {
|
|||||||
|
|
||||||
it("should list authUsers by provider_id", async () => {
|
it("should list authUsers by provider_id", async () => {
|
||||||
const authUsers = await service.list({
|
const authUsers = await service.list({
|
||||||
provider_id: "manual",
|
provider: "manual",
|
||||||
})
|
})
|
||||||
|
|
||||||
const serialized = JSON.parse(JSON.stringify(authUsers))
|
const serialized = JSON.parse(JSON.stringify(authUsers))
|
||||||
@@ -103,7 +101,7 @@ describe("AuthUser Service", () => {
|
|||||||
|
|
||||||
it("should listAndCount authUsers by provider_id", async () => {
|
it("should listAndCount authUsers by provider_id", async () => {
|
||||||
const [authUsers, count] = await service.listAndCount({
|
const [authUsers, count] = await service.listAndCount({
|
||||||
provider_id: "manual",
|
provider: "manual",
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(count).toEqual(2)
|
expect(count).toEqual(2)
|
||||||
@@ -227,7 +225,7 @@ describe("AuthUser Service", () => {
|
|||||||
await service.create([
|
await service.create([
|
||||||
{
|
{
|
||||||
id: "test",
|
id: "test",
|
||||||
provider_id: "manual",
|
provider: "manual",
|
||||||
entity_id: "test",
|
entity_id: "test",
|
||||||
scope: "store",
|
scope: "store",
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,274 +0,0 @@
|
|||||||
import { Modules } from "@medusajs/modules-sdk"
|
|
||||||
import { IAuthModuleService } from "@medusajs/types"
|
|
||||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
|
||||||
import { initModules } from "medusa-test-utils"
|
|
||||||
import { createAuthProviders } from "../../../__fixtures__/auth-provider"
|
|
||||||
import { createAuthUsers } from "../../../__fixtures__/auth-user"
|
|
||||||
import { MikroOrmWrapper } from "../../../utils"
|
|
||||||
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
|
||||||
|
|
||||||
jest.setTimeout(30000)
|
|
||||||
|
|
||||||
describe("AuthModuleService - AuthProvider", () => {
|
|
||||||
let service: IAuthModuleService
|
|
||||||
let testManager: SqlEntityManager
|
|
||||||
let shutdownFunc: () => Promise<void>
|
|
||||||
|
|
||||||
beforeAll(async () => {
|
|
||||||
const initModulesConfig = getInitModuleConfig()
|
|
||||||
|
|
||||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
|
||||||
|
|
||||||
service = medusaApp.modules[Modules.AUTH]
|
|
||||||
|
|
||||||
shutdownFunc = shutdown
|
|
||||||
})
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
await MikroOrmWrapper.setupDatabase()
|
|
||||||
testManager = MikroOrmWrapper.forkManager()
|
|
||||||
|
|
||||||
await createAuthProviders(testManager)
|
|
||||||
await createAuthUsers(testManager)
|
|
||||||
})
|
|
||||||
|
|
||||||
afterAll(async () => {
|
|
||||||
await shutdownFunc()
|
|
||||||
})
|
|
||||||
|
|
||||||
afterEach(async () => {
|
|
||||||
await MikroOrmWrapper.clearDatabase()
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("listAuthProviders", () => {
|
|
||||||
it("should list AuthProviders", async () => {
|
|
||||||
const authProviders = await service.listAuthProviders()
|
|
||||||
|
|
||||||
expect(authProviders).toEqual(
|
|
||||||
expect.arrayContaining([
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "manual",
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "disabled",
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "store",
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "admin",
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should list authProviders by id", async () => {
|
|
||||||
const authProviders = await service.listAuthProviders({
|
|
||||||
provider: ["manual"],
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(authProviders).toEqual([
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "manual",
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should list active authProviders", async () => {
|
|
||||||
const authProviders = await service.listAuthProviders({
|
|
||||||
is_active: true,
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(authProviders).toEqual([
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "manual",
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "store",
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "admin",
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("listAndCountAuthProviders", () => {
|
|
||||||
it("should list and count AuthProviders", async () => {
|
|
||||||
const [authProviders, count] = await service.listAndCountAuthProviders()
|
|
||||||
|
|
||||||
expect(count).toEqual(4)
|
|
||||||
expect(authProviders).toEqual([
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "manual",
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "disabled",
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "store",
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "admin",
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should list and count authProviders by provider", async () => {
|
|
||||||
const [authProviders, count] = await service.listAndCountAuthProviders({
|
|
||||||
provider: ["manual"],
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(count).toEqual(1)
|
|
||||||
expect(authProviders).toEqual([
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "manual",
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should list and count active authProviders", async () => {
|
|
||||||
const [authProviders, count] = await service.listAndCountAuthProviders({
|
|
||||||
is_active: true,
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(count).toEqual(3)
|
|
||||||
expect(authProviders).toEqual([
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "manual",
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "store",
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "admin",
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("retrieveAuthProvider", () => {
|
|
||||||
const provider = "manual"
|
|
||||||
|
|
||||||
it("should return an authProvider for the given provider", async () => {
|
|
||||||
const authProvider = await service.retrieveAuthProvider(provider)
|
|
||||||
|
|
||||||
expect(authProvider).toEqual(
|
|
||||||
expect.objectContaining({
|
|
||||||
provider,
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should return authProvider based on config select param", async () => {
|
|
||||||
const authProvider = await service.retrieveAuthProvider(provider, {
|
|
||||||
select: ["provider"],
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(authProvider).toEqual({
|
|
||||||
provider,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should throw an error when an authProvider with the given provider does not exist", async () => {
|
|
||||||
let error
|
|
||||||
|
|
||||||
try {
|
|
||||||
await service.retrieveAuthProvider("does-not-exist")
|
|
||||||
} catch (e) {
|
|
||||||
error = e
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(error.message).toEqual(
|
|
||||||
"AuthProvider with provider: does-not-exist was not found"
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should throw an error when a provider is not provided", async () => {
|
|
||||||
let error
|
|
||||||
|
|
||||||
try {
|
|
||||||
await service.retrieveAuthProvider(undefined as unknown as string)
|
|
||||||
} catch (e) {
|
|
||||||
error = e
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(error.message).toEqual("authProvider - provider must be defined")
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("deleteAuthProvider", () => {
|
|
||||||
const provider = "manual"
|
|
||||||
|
|
||||||
it("should delete the authProviders given a provider successfully", async () => {
|
|
||||||
await service.deleteAuthProviders([provider])
|
|
||||||
|
|
||||||
const authProviders = await service.listAuthProviders({
|
|
||||||
provider: [provider],
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(authProviders).toHaveLength(0)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("updateAuthProvider", () => {
|
|
||||||
const provider = "manual"
|
|
||||||
|
|
||||||
it("should throw an error when a id does not exist", async () => {
|
|
||||||
let error
|
|
||||||
|
|
||||||
try {
|
|
||||||
await service.updateAuthProvider([
|
|
||||||
{
|
|
||||||
provider: "does-not-exist",
|
|
||||||
},
|
|
||||||
])
|
|
||||||
} catch (e) {
|
|
||||||
error = e
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(error.message).toEqual(
|
|
||||||
'AuthProvider with provider "does-not-exist" not found'
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should update authProvider", async () => {
|
|
||||||
await service.updateAuthProvider([
|
|
||||||
{
|
|
||||||
provider: "manual",
|
|
||||||
name: "test",
|
|
||||||
},
|
|
||||||
])
|
|
||||||
|
|
||||||
const [provider] = await service.listAuthProviders({
|
|
||||||
provider: ["manual"],
|
|
||||||
})
|
|
||||||
expect(provider).toEqual(
|
|
||||||
expect.objectContaining({
|
|
||||||
name: "test",
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("createAuthProvider", () => {
|
|
||||||
it("should create a authProvider successfully", async () => {
|
|
||||||
await service.createAuthProvider([
|
|
||||||
{
|
|
||||||
provider: "test",
|
|
||||||
name: "test provider",
|
|
||||||
},
|
|
||||||
])
|
|
||||||
|
|
||||||
const [authProvider] = await service.listAuthProviders({
|
|
||||||
provider: ["test"],
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(authProvider).toEqual(
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "test",
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -1,11 +1,10 @@
|
|||||||
import { Modules } from "@medusajs/modules-sdk"
|
|
||||||
import { IAuthModuleService } from "@medusajs/types"
|
import { IAuthModuleService } from "@medusajs/types"
|
||||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
|
||||||
import { initModules } from "medusa-test-utils"
|
|
||||||
import { createAuthProviders } from "../../../__fixtures__/auth-provider"
|
|
||||||
import { createAuthUsers } from "../../../__fixtures__/auth-user"
|
|
||||||
import { MikroOrmWrapper } from "../../../utils"
|
import { MikroOrmWrapper } from "../../../utils"
|
||||||
|
import { Modules } from "@medusajs/modules-sdk"
|
||||||
|
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||||
|
import { createAuthUsers } from "../../../__fixtures__/auth-user"
|
||||||
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
||||||
|
import { initModules } from "medusa-test-utils"
|
||||||
|
|
||||||
jest.setTimeout(30000)
|
jest.setTimeout(30000)
|
||||||
|
|
||||||
@@ -28,7 +27,6 @@ describe("AuthModuleService - AuthUser", () => {
|
|||||||
await MikroOrmWrapper.setupDatabase()
|
await MikroOrmWrapper.setupDatabase()
|
||||||
testManager = MikroOrmWrapper.forkManager()
|
testManager = MikroOrmWrapper.forkManager()
|
||||||
|
|
||||||
await createAuthProviders(testManager)
|
|
||||||
await createAuthUsers(testManager)
|
await createAuthUsers(testManager)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -42,23 +40,23 @@ describe("AuthModuleService - AuthUser", () => {
|
|||||||
|
|
||||||
describe("listAuthUsers", () => {
|
describe("listAuthUsers", () => {
|
||||||
it("should list authUsers", async () => {
|
it("should list authUsers", async () => {
|
||||||
const authUsers = await service.listAuthUsers()
|
const authUsers = await service.list()
|
||||||
|
|
||||||
expect(authUsers).toEqual([
|
expect(authUsers).toEqual([
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
provider: { provider: "manual" },
|
provider: "manual",
|
||||||
}),
|
}),
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
provider: { provider: "manual" },
|
provider: "manual",
|
||||||
}),
|
}),
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
provider: { provider: "store" },
|
provider: "store",
|
||||||
}),
|
}),
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should list authUsers by id", async () => {
|
it("should list authUsers by id", async () => {
|
||||||
const authUsers = await service.listAuthUsers({
|
const authUsers = await service.list({
|
||||||
id: ["test-id"],
|
id: ["test-id"],
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -69,9 +67,9 @@ describe("AuthModuleService - AuthUser", () => {
|
|||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should list authUsers by provider_id", async () => {
|
it("should list authUsers by provider", async () => {
|
||||||
const authUsers = await service.listAuthUsers({
|
const authUsers = await service.list({
|
||||||
provider_id: "manual",
|
provider: "manual",
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(authUsers).toEqual([
|
expect(authUsers).toEqual([
|
||||||
@@ -87,25 +85,25 @@ describe("AuthModuleService - AuthUser", () => {
|
|||||||
|
|
||||||
describe("listAndCountAuthUsers", () => {
|
describe("listAndCountAuthUsers", () => {
|
||||||
it("should list and count authUsers", async () => {
|
it("should list and count authUsers", async () => {
|
||||||
const [authUsers, count] = await service.listAndCountAuthUsers()
|
const [authUsers, count] = await service.listAndCount()
|
||||||
|
|
||||||
expect(count).toEqual(3)
|
expect(count).toEqual(3)
|
||||||
expect(authUsers).toEqual([
|
expect(authUsers).toEqual([
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
provider: { provider: "manual" },
|
provider: "manual",
|
||||||
}),
|
}),
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
provider: { provider: "manual" },
|
provider: "manual",
|
||||||
}),
|
}),
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
provider: { provider: "store" },
|
provider: "store",
|
||||||
}),
|
}),
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should listAndCount authUsers by provider_id", async () => {
|
it("should listAndCount authUsers by provider_id", async () => {
|
||||||
const [authUsers, count] = await service.listAndCountAuthUsers({
|
const [authUsers, count] = await service.listAndCount({
|
||||||
provider_id: "manual",
|
provider: "manual",
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(count).toEqual(2)
|
expect(count).toEqual(2)
|
||||||
@@ -124,7 +122,7 @@ describe("AuthModuleService - AuthUser", () => {
|
|||||||
const id = "test-id"
|
const id = "test-id"
|
||||||
|
|
||||||
it("should return an authUser for the given id", async () => {
|
it("should return an authUser for the given id", async () => {
|
||||||
const authUser = await service.retrieveAuthUser(id)
|
const authUser = await service.retrieve(id)
|
||||||
|
|
||||||
expect(authUser).toEqual(
|
expect(authUser).toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
@@ -137,7 +135,7 @@ describe("AuthModuleService - AuthUser", () => {
|
|||||||
let error
|
let error
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await service.retrieveAuthUser("does-not-exist")
|
await service.retrieve("does-not-exist")
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
error = e
|
error = e
|
||||||
}
|
}
|
||||||
@@ -148,7 +146,7 @@ describe("AuthModuleService - AuthUser", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("should not return an authUser with password hash", async () => {
|
it("should not return an authUser with password hash", async () => {
|
||||||
const authUser = await service.retrieveAuthUser("test-id-1")
|
const authUser = await service.retrieve("test-id-1")
|
||||||
|
|
||||||
expect(authUser).toEqual(
|
expect(authUser).toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
@@ -162,7 +160,7 @@ describe("AuthModuleService - AuthUser", () => {
|
|||||||
let error
|
let error
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await service.retrieveAuthUser(undefined as unknown as string)
|
await service.retrieve(undefined as unknown as string)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
error = e
|
error = e
|
||||||
}
|
}
|
||||||
@@ -171,7 +169,7 @@ describe("AuthModuleService - AuthUser", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("should return authUser based on config select param", async () => {
|
it("should return authUser based on config select param", async () => {
|
||||||
const authUser = await service.retrieveAuthUser(id, {
|
const authUser = await service.retrieve(id, {
|
||||||
select: ["id"],
|
select: ["id"],
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -185,9 +183,9 @@ describe("AuthModuleService - AuthUser", () => {
|
|||||||
const id = "test-id"
|
const id = "test-id"
|
||||||
|
|
||||||
it("should delete the authUsers given an id successfully", async () => {
|
it("should delete the authUsers given an id successfully", async () => {
|
||||||
await service.deleteAuthUsers([id])
|
await service.delete([id])
|
||||||
|
|
||||||
const authUsers = await service.listAuthUsers({
|
const authUsers = await service.list({
|
||||||
id: [id],
|
id: [id],
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -202,7 +200,7 @@ describe("AuthModuleService - AuthUser", () => {
|
|||||||
let error
|
let error
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await service.updateAuthUser([
|
await service.update([
|
||||||
{
|
{
|
||||||
id: "does-not-exist",
|
id: "does-not-exist",
|
||||||
},
|
},
|
||||||
@@ -217,14 +215,14 @@ describe("AuthModuleService - AuthUser", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("should update authUser", async () => {
|
it("should update authUser", async () => {
|
||||||
await service.updateAuthUser([
|
await service.update([
|
||||||
{
|
{
|
||||||
id,
|
id,
|
||||||
provider_metadata: { email: "test@email.com" },
|
provider_metadata: { email: "test@email.com" },
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
|
|
||||||
const [authUser] = await service.listAuthUsers({ id: [id] })
|
const [authUser] = await service.list({ id: [id] })
|
||||||
expect(authUser).toEqual(
|
expect(authUser).toEqual(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
provider_metadata: { email: "test@email.com" },
|
provider_metadata: { email: "test@email.com" },
|
||||||
@@ -235,16 +233,16 @@ describe("AuthModuleService - AuthUser", () => {
|
|||||||
|
|
||||||
describe("createAuthUser", () => {
|
describe("createAuthUser", () => {
|
||||||
it("should create a authUser successfully", async () => {
|
it("should create a authUser successfully", async () => {
|
||||||
await service.createAuthUser([
|
await service.create([
|
||||||
{
|
{
|
||||||
id: "test",
|
id: "test",
|
||||||
provider_id: "manual",
|
provider: "manual",
|
||||||
entity_id: "test",
|
entity_id: "test",
|
||||||
scope: "store",
|
scope: "store",
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
|
|
||||||
const [authUser, count] = await service.listAndCountAuthUsers({
|
const [authUser, count] = await service.listAndCount({
|
||||||
id: ["test"],
|
id: ["test"],
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import { MedusaModule, Modules } from "@medusajs/modules-sdk"
|
import { MedusaModule, Modules } from "@medusajs/modules-sdk"
|
||||||
|
|
||||||
import { IAuthModuleService } from "@medusajs/types"
|
import { IAuthModuleService } from "@medusajs/types"
|
||||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
|
||||||
import { initModules } from "medusa-test-utils"
|
|
||||||
import { createAuthProviders } from "../../../__fixtures__/auth-provider"
|
|
||||||
import { MikroOrmWrapper } from "../../../utils"
|
import { MikroOrmWrapper } from "../../../utils"
|
||||||
|
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||||
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
||||||
|
import { initModules } from "medusa-test-utils"
|
||||||
|
|
||||||
jest.setTimeout(30000)
|
jest.setTimeout(30000)
|
||||||
|
|
||||||
@@ -41,34 +41,8 @@ describe("AuthModuleService - AuthProvider", () => {
|
|||||||
await shutdownFunc()
|
await shutdownFunc()
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("listAuthProviders", () => {
|
|
||||||
it("should list default AuthProviders registered by loaders", async () => {
|
|
||||||
const authProviders = await service.listAuthProviders()
|
|
||||||
|
|
||||||
expect(authProviders).toEqual(
|
|
||||||
expect.arrayContaining([
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "emailpass",
|
|
||||||
name: "Email/Password Authentication",
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
|
||||||
provider: "google",
|
|
||||||
name: "Google Authentication",
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("authenticate", () => {
|
describe("authenticate", () => {
|
||||||
it("authenticate validates that a provider is registered in container", async () => {
|
it("authenticate validates that a provider is registered in container", async () => {
|
||||||
await createAuthProviders(testManager, [
|
|
||||||
{
|
|
||||||
provider: "notRegistered",
|
|
||||||
name: "test",
|
|
||||||
},
|
|
||||||
])
|
|
||||||
|
|
||||||
const { success, error } = await service.authenticate(
|
const { success, error } = await service.authenticate(
|
||||||
"notRegistered",
|
"notRegistered",
|
||||||
{} as any
|
{} as any
|
||||||
@@ -76,7 +50,7 @@ describe("AuthModuleService - AuthProvider", () => {
|
|||||||
|
|
||||||
expect(success).toBe(false)
|
expect(success).toBe(false)
|
||||||
expect(error).toEqual(
|
expect(error).toEqual(
|
||||||
"AuthenticationProvider with for provider: notRegistered wasn't registered in the module. Have you configured your options correctly?"
|
"AuthenticationProvider: notRegistered wasn't registered in the module. Have you configured your options correctly?"
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -4,14 +4,12 @@ import { IAuthModuleService } from "@medusajs/types"
|
|||||||
import { MikroOrmWrapper } from "../../../utils"
|
import { MikroOrmWrapper } from "../../../utils"
|
||||||
import Scrypt from "scrypt-kdf"
|
import Scrypt from "scrypt-kdf"
|
||||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||||
import { createAuthProviders } from "../../../__fixtures__/auth-provider"
|
|
||||||
import { createAuthUsers } from "../../../__fixtures__/auth-user"
|
import { createAuthUsers } from "../../../__fixtures__/auth-user"
|
||||||
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
||||||
import { initModules } from "medusa-test-utils"
|
import { initModules } from "medusa-test-utils"
|
||||||
|
|
||||||
jest.setTimeout(30000)
|
jest.setTimeout(30000)
|
||||||
const seedDefaultData = async (testManager) => {
|
const seedDefaultData = async (testManager) => {
|
||||||
await createAuthProviders(testManager)
|
|
||||||
await createAuthUsers(testManager)
|
await createAuthUsers(testManager)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,8 +85,6 @@ describe("AuthModuleService - AuthProvider", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("fails when no password is given", async () => {
|
it("fails when no password is given", async () => {
|
||||||
const email = "test@test.com"
|
|
||||||
|
|
||||||
await seedDefaultData(testManager)
|
await seedDefaultData(testManager)
|
||||||
|
|
||||||
const res = await service.authenticate("emailpass", {
|
const res = await service.authenticate("emailpass", {
|
||||||
|
|||||||
@@ -4,71 +4,6 @@
|
|||||||
],
|
],
|
||||||
"name": "public",
|
"name": "public",
|
||||||
"tables": [
|
"tables": [
|
||||||
{
|
|
||||||
"columns": {
|
|
||||||
"provider": {
|
|
||||||
"name": "provider",
|
|
||||||
"type": "text",
|
|
||||||
"unsigned": false,
|
|
||||||
"autoincrement": false,
|
|
||||||
"primary": false,
|
|
||||||
"nullable": false,
|
|
||||||
"mappedType": "text"
|
|
||||||
},
|
|
||||||
"name": {
|
|
||||||
"name": "name",
|
|
||||||
"type": "text",
|
|
||||||
"unsigned": false,
|
|
||||||
"autoincrement": false,
|
|
||||||
"primary": false,
|
|
||||||
"nullable": false,
|
|
||||||
"mappedType": "text"
|
|
||||||
},
|
|
||||||
"scope": {
|
|
||||||
"name": "scope",
|
|
||||||
"type": "text",
|
|
||||||
"unsigned": false,
|
|
||||||
"autoincrement": false,
|
|
||||||
"primary": false,
|
|
||||||
"nullable": true,
|
|
||||||
"mappedType": "text"
|
|
||||||
},
|
|
||||||
"config": {
|
|
||||||
"name": "config",
|
|
||||||
"type": "jsonb",
|
|
||||||
"unsigned": false,
|
|
||||||
"autoincrement": false,
|
|
||||||
"primary": false,
|
|
||||||
"nullable": true,
|
|
||||||
"mappedType": "json"
|
|
||||||
},
|
|
||||||
"is_active": {
|
|
||||||
"name": "is_active",
|
|
||||||
"type": "boolean",
|
|
||||||
"unsigned": false,
|
|
||||||
"autoincrement": false,
|
|
||||||
"primary": false,
|
|
||||||
"nullable": false,
|
|
||||||
"default": "false",
|
|
||||||
"mappedType": "boolean"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"name": "auth_provider",
|
|
||||||
"schema": "public",
|
|
||||||
"indexes": [
|
|
||||||
{
|
|
||||||
"keyName": "auth_provider_pkey",
|
|
||||||
"columnNames": [
|
|
||||||
"provider"
|
|
||||||
],
|
|
||||||
"composite": false,
|
|
||||||
"primary": true,
|
|
||||||
"unique": true
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"checks": [],
|
|
||||||
"foreignKeys": {}
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"columns": {
|
"columns": {
|
||||||
"id": {
|
"id": {
|
||||||
@@ -89,13 +24,13 @@
|
|||||||
"nullable": false,
|
"nullable": false,
|
||||||
"mappedType": "text"
|
"mappedType": "text"
|
||||||
},
|
},
|
||||||
"provider_id": {
|
"provider": {
|
||||||
"name": "provider_id",
|
"name": "provider",
|
||||||
"type": "text",
|
"type": "text",
|
||||||
"unsigned": false,
|
"unsigned": false,
|
||||||
"autoincrement": false,
|
"autoincrement": false,
|
||||||
"primary": false,
|
"primary": false,
|
||||||
"nullable": true,
|
"nullable": false,
|
||||||
"mappedType": "text"
|
"mappedType": "text"
|
||||||
},
|
},
|
||||||
"scope": {
|
"scope": {
|
||||||
@@ -141,7 +76,7 @@
|
|||||||
{
|
{
|
||||||
"keyName": "IDX_auth_user_provider_scope_entity_id",
|
"keyName": "IDX_auth_user_provider_scope_entity_id",
|
||||||
"columnNames": [
|
"columnNames": [
|
||||||
"provider_id",
|
"provider",
|
||||||
"scope",
|
"scope",
|
||||||
"entity_id"
|
"entity_id"
|
||||||
],
|
],
|
||||||
@@ -160,20 +95,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"checks": [],
|
"checks": [],
|
||||||
"foreignKeys": {
|
"foreignKeys": {}
|
||||||
"auth_user_provider_id_foreign": {
|
|
||||||
"constraintName": "auth_user_provider_id_foreign",
|
|
||||||
"columnNames": [
|
|
||||||
"provider_id"
|
|
||||||
],
|
|
||||||
"localTableName": "public.auth_user",
|
|
||||||
"referencedColumnNames": [
|
|
||||||
"provider"
|
|
||||||
],
|
|
||||||
"referencedTableName": "public.auth_provider",
|
|
||||||
"deleteRule": "cascade"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
import { Migration } from '@mikro-orm/migrations';
|
|
||||||
|
|
||||||
export class Migration20240201100135 extends Migration {
|
|
||||||
|
|
||||||
async up(): Promise<void> {
|
|
||||||
this.addSql('create table "auth_provider" ("provider" text not null, "name" text not null, "scope" text null, "config" jsonb null, "is_active" boolean not null default false, constraint "auth_provider_pkey" primary key ("provider"));');
|
|
||||||
|
|
||||||
this.addSql('create table "auth_user" ("id" text not null, "entity_id" text not null, "provider_id" text null, "scope" text not null, "user_metadata" jsonb null, "app_metadata" jsonb not null, "provider_metadata" jsonb null, constraint "auth_user_pkey" primary key ("id"));');
|
|
||||||
this.addSql('alter table "auth_user" add constraint "IDX_auth_user_provider_scope_entity_id" unique ("provider_id", "scope", "entity_id");');
|
|
||||||
|
|
||||||
this.addSql('alter table "auth_user" add constraint "auth_user_provider_id_foreign" foreign key ("provider_id") references "auth_provider" ("provider") on delete cascade;');
|
|
||||||
}
|
|
||||||
|
|
||||||
async down(): Promise<void> {
|
|
||||||
this.addSql('alter table "auth_user" drop constraint "auth_user_provider_id_foreign";');
|
|
||||||
|
|
||||||
this.addSql('drop table if exists "auth_provider" cascade;');
|
|
||||||
|
|
||||||
this.addSql('drop table if exists "auth_user" cascade;');
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import { Migration } from "@mikro-orm/migrations"
|
||||||
|
|
||||||
|
export class Migration20240205025924 extends Migration {
|
||||||
|
async up(): Promise<void> {
|
||||||
|
this.addSql(
|
||||||
|
'create table if not exists "auth_user" ("id" text not null, "entity_id" text not null, "provider" text not null, "scope" text not null, "user_metadata" jsonb null, "app_metadata" jsonb not null, "provider_metadata" jsonb null, constraint "auth_user_pkey" primary key ("id"));'
|
||||||
|
)
|
||||||
|
this.addSql(
|
||||||
|
'alter table "auth_user" add constraint "IDX_auth_user_provider_scope_entity_id" unique ("provider", "scope", "entity_id");'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
async down(): Promise<void> {
|
||||||
|
this.addSql('drop table if exists "auth_user" cascade;')
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
import {
|
|
||||||
Entity,
|
|
||||||
Enum,
|
|
||||||
OptionalProps,
|
|
||||||
PrimaryKey,
|
|
||||||
Property,
|
|
||||||
} from "@mikro-orm/core"
|
|
||||||
|
|
||||||
import { ProviderDomain } from "../types/repositories/auth-provider"
|
|
||||||
|
|
||||||
type OptionalFields = "domain" | "is_active" | "config"
|
|
||||||
|
|
||||||
@Entity()
|
|
||||||
export default class AuthProvider {
|
|
||||||
[OptionalProps]: OptionalFields
|
|
||||||
|
|
||||||
@PrimaryKey({ columnType: "text" })
|
|
||||||
provider!: string
|
|
||||||
|
|
||||||
@Property({ columnType: "text" })
|
|
||||||
name: string
|
|
||||||
|
|
||||||
@Property({ columnType: "text", nullable: true })
|
|
||||||
scope: string
|
|
||||||
|
|
||||||
@Property({ columnType: "jsonb", nullable: true })
|
|
||||||
config: Record<string, unknown> | null = null
|
|
||||||
|
|
||||||
@Property({ columnType: "boolean", default: false })
|
|
||||||
is_active = false
|
|
||||||
}
|
|
||||||
@@ -11,7 +11,6 @@ import {
|
|||||||
Unique,
|
Unique,
|
||||||
} from "@mikro-orm/core"
|
} from "@mikro-orm/core"
|
||||||
|
|
||||||
import AuthProvider from "./auth-provider"
|
|
||||||
import { generateEntityId } from "@medusajs/utils"
|
import { generateEntityId } from "@medusajs/utils"
|
||||||
|
|
||||||
type OptionalFields = "provider_metadata" | "app_metadata" | "user_metadata"
|
type OptionalFields = "provider_metadata" | "app_metadata" | "user_metadata"
|
||||||
@@ -30,12 +29,8 @@ export default class AuthUser {
|
|||||||
@Property({ columnType: "text" })
|
@Property({ columnType: "text" })
|
||||||
entity_id: string
|
entity_id: string
|
||||||
|
|
||||||
@ManyToOne(() => AuthProvider, {
|
@Property({ columnType: "text" })
|
||||||
joinColumn: "provider",
|
provider: string
|
||||||
fieldName: "provider_id",
|
|
||||||
cascade: [Cascade.REMOVE],
|
|
||||||
})
|
|
||||||
provider: AuthProvider
|
|
||||||
|
|
||||||
@Property({ columnType: "text" })
|
@Property({ columnType: "text" })
|
||||||
scope: string
|
scope: string
|
||||||
|
|||||||
@@ -1,2 +1 @@
|
|||||||
export { default as AuthUser } from "./auth-user"
|
export { default as AuthUser } from "./auth-user"
|
||||||
export { default as AuthProvider } from "./auth-provider"
|
|
||||||
@@ -15,13 +15,16 @@ class EmailPasswordProvider extends AbstractAuthModuleProvider {
|
|||||||
protected readonly authUserSerivce_: AuthUserService
|
protected readonly authUserSerivce_: AuthUserService
|
||||||
|
|
||||||
constructor({ authUserService }: { authUserService: AuthUserService }) {
|
constructor({ authUserService }: { authUserService: AuthUserService }) {
|
||||||
super(arguments[0])
|
super(arguments[0], {
|
||||||
|
provider: EmailPasswordProvider.PROVIDER,
|
||||||
|
displayName: EmailPasswordProvider.DISPLAY_NAME,
|
||||||
|
})
|
||||||
|
|
||||||
this.authUserSerivce_ = authUserService
|
this.authUserSerivce_ = authUserService
|
||||||
}
|
}
|
||||||
|
|
||||||
private getHashConfig(scope: string) {
|
private getHashConfig() {
|
||||||
const scopeConfig = this.scopes_[scope].hashConfig as
|
const scopeConfig = this.scopeConfig_.hashConfig as
|
||||||
| Scrypt.ScryptParams
|
| Scrypt.ScryptParams
|
||||||
| undefined
|
| undefined
|
||||||
|
|
||||||
@@ -58,16 +61,13 @@ class EmailPasswordProvider extends AbstractAuthModuleProvider {
|
|||||||
)
|
)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.type === MedusaError.Types.NOT_FOUND) {
|
if (error.type === MedusaError.Types.NOT_FOUND) {
|
||||||
const password_hash = await Scrypt.kdf(
|
const password_hash = await Scrypt.kdf(password, this.getHashConfig())
|
||||||
password,
|
|
||||||
this.getHashConfig(userData.authScope)
|
|
||||||
)
|
|
||||||
|
|
||||||
const [createdAuthUser] = await this.authUserSerivce_.create([
|
const [createdAuthUser] = await this.authUserSerivce_.create([
|
||||||
{
|
{
|
||||||
entity_id: email,
|
entity_id: email,
|
||||||
provider: EmailPasswordProvider.PROVIDER,
|
provider: EmailPasswordProvider.PROVIDER,
|
||||||
scope: userData.authScope,
|
scope: this.scope_,
|
||||||
provider_metadata: {
|
provider_metadata: {
|
||||||
password: password_hash.toString("base64"),
|
password: password_hash.toString("base64"),
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ import {
|
|||||||
AuthenticationResponse,
|
AuthenticationResponse,
|
||||||
ModulesSdkTypes,
|
ModulesSdkTypes,
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
import { AuthUserService } from "@services"
|
|
||||||
import jwt, { JwtPayload } from "jsonwebtoken"
|
import jwt, { JwtPayload } from "jsonwebtoken"
|
||||||
|
|
||||||
|
import { AuthUserService } from "@services"
|
||||||
import { AuthorizationCode } from "simple-oauth2"
|
import { AuthorizationCode } from "simple-oauth2"
|
||||||
import url from "url"
|
import url from "url"
|
||||||
|
|
||||||
@@ -30,7 +30,10 @@ class GoogleProvider extends AbstractAuthModuleProvider {
|
|||||||
protected readonly authProviderService_: ModulesSdkTypes.InternalModuleService<any>
|
protected readonly authProviderService_: ModulesSdkTypes.InternalModuleService<any>
|
||||||
|
|
||||||
constructor({ authUserService, authProviderService }: InjectedDependencies) {
|
constructor({ authUserService, authProviderService }: InjectedDependencies) {
|
||||||
super(arguments[0])
|
super(arguments[0], {
|
||||||
|
provider: GoogleProvider.PROVIDER,
|
||||||
|
displayName: GoogleProvider.DISPLAY_NAME,
|
||||||
|
})
|
||||||
|
|
||||||
this.authUserService_ = authUserService
|
this.authUserService_ = authUserService
|
||||||
this.authProviderService_ = authProviderService
|
this.authProviderService_ = authProviderService
|
||||||
@@ -77,11 +80,11 @@ class GoogleProvider extends AbstractAuthModuleProvider {
|
|||||||
|
|
||||||
const code = req.query?.code ?? req.body?.code
|
const code = req.query?.code ?? req.body?.code
|
||||||
|
|
||||||
return await this.validateCallbackToken(code, req.authScope, config)
|
return await this.validateCallbackToken(code, config)
|
||||||
}
|
}
|
||||||
|
|
||||||
// abstractable
|
// abstractable
|
||||||
async verify_(refreshToken: string, scope: string) {
|
async verify_(refreshToken: string) {
|
||||||
const jwtData = jwt.decode(refreshToken, {
|
const jwtData = jwt.decode(refreshToken, {
|
||||||
complete: true,
|
complete: true,
|
||||||
}) as JwtPayload
|
}) as JwtPayload
|
||||||
@@ -101,7 +104,7 @@ class GoogleProvider extends AbstractAuthModuleProvider {
|
|||||||
entity_id,
|
entity_id,
|
||||||
provider: GoogleProvider.PROVIDER,
|
provider: GoogleProvider.PROVIDER,
|
||||||
user_metadata: jwtData!.payload,
|
user_metadata: jwtData!.payload,
|
||||||
scope,
|
scope: this.scope_,
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
authUser = createdAuthUser
|
authUser = createdAuthUser
|
||||||
@@ -116,7 +119,6 @@ class GoogleProvider extends AbstractAuthModuleProvider {
|
|||||||
// abstractable
|
// abstractable
|
||||||
private async validateCallbackToken(
|
private async validateCallbackToken(
|
||||||
code: string,
|
code: string,
|
||||||
scope: string,
|
|
||||||
{ clientID, callbackURL, clientSecret }: ProviderConfig
|
{ clientID, callbackURL, clientSecret }: ProviderConfig
|
||||||
) {
|
) {
|
||||||
const client = this.getAuthorizationCodeHandler({ clientID, clientSecret })
|
const client = this.getAuthorizationCodeHandler({ clientID, clientSecret })
|
||||||
@@ -129,30 +131,28 @@ class GoogleProvider extends AbstractAuthModuleProvider {
|
|||||||
try {
|
try {
|
||||||
const accessToken = await client.getToken(tokenParams)
|
const accessToken = await client.getToken(tokenParams)
|
||||||
|
|
||||||
return await this.verify_(accessToken.token.id_token, scope)
|
return await this.verify_(accessToken.token.id_token)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return { success: false, error: error.message }
|
return { success: false, error: error.message }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private getConfigFromScope(
|
private getConfigFromScope(): ProviderConfig {
|
||||||
config: AuthProviderScope & Partial<ProviderConfig>
|
const config: Partial<ProviderConfig> = { ...this.scopeConfig_ }
|
||||||
): ProviderConfig {
|
|
||||||
const providerConfig: Partial<ProviderConfig> = { ...config }
|
|
||||||
|
|
||||||
if (!providerConfig.clientID) {
|
if (!config.clientID) {
|
||||||
throw new Error("Google clientID is required")
|
throw new Error("Google clientID is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!providerConfig.clientSecret) {
|
if (!config.clientSecret) {
|
||||||
throw new Error("Google clientSecret is required")
|
throw new Error("Google clientSecret is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!providerConfig.callbackURL) {
|
if (!config.callbackURL) {
|
||||||
throw new Error("Google callbackUrl is required")
|
throw new Error("Google callbackUrl is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
return providerConfig as ProviderConfig
|
return config as ProviderConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
private originalURL(req: AuthenticationInput) {
|
private originalURL(req: AuthenticationInput) {
|
||||||
@@ -168,11 +168,9 @@ class GoogleProvider extends AbstractAuthModuleProvider {
|
|||||||
): Promise<ProviderConfig> {
|
): Promise<ProviderConfig> {
|
||||||
await this.authProviderService_.retrieve(GoogleProvider.PROVIDER)
|
await this.authProviderService_.retrieve(GoogleProvider.PROVIDER)
|
||||||
|
|
||||||
const scopeConfig = this.scopes_[req.authScope]
|
const config = this.getConfigFromScope()
|
||||||
|
|
||||||
const config = this.getConfigFromScope(scopeConfig)
|
const callbackURL = config.callbackURL
|
||||||
|
|
||||||
const { callbackURL } = config
|
|
||||||
|
|
||||||
const parsedCallbackUrl = !url.parse(callbackURL).protocol
|
const parsedCallbackUrl = !url.parse(callbackURL).protocol
|
||||||
? url.resolve(this.originalURL(req), callbackURL)
|
? url.resolve(this.originalURL(req), callbackURL)
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
import {
|
import {
|
||||||
AuthenticationInput,
|
AuthenticationInput,
|
||||||
AuthenticationResponse,
|
AuthenticationResponse,
|
||||||
AuthProviderDTO,
|
|
||||||
AuthTypes,
|
AuthTypes,
|
||||||
AuthUserDTO,
|
AuthUserDTO,
|
||||||
Context,
|
Context,
|
||||||
CreateAuthProviderDTO,
|
|
||||||
CreateAuthUserDTO,
|
CreateAuthUserDTO,
|
||||||
DAL,
|
DAL,
|
||||||
InternalModuleDeclaration,
|
InternalModuleDeclaration,
|
||||||
@@ -14,7 +12,7 @@ import {
|
|||||||
UpdateAuthUserDTO,
|
UpdateAuthUserDTO,
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
|
|
||||||
import { AuthProvider, AuthUser } from "@models"
|
import { AuthUser } from "@models"
|
||||||
|
|
||||||
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
|
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
|
||||||
|
|
||||||
@@ -26,43 +24,29 @@ import {
|
|||||||
MedusaError,
|
MedusaError,
|
||||||
ModulesSdkUtils,
|
ModulesSdkUtils,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import { ServiceTypes } from "@types"
|
|
||||||
|
|
||||||
type InjectedDependencies = {
|
type InjectedDependencies = {
|
||||||
baseRepository: DAL.RepositoryService
|
baseRepository: DAL.RepositoryService
|
||||||
authUserService: ModulesSdkTypes.InternalModuleService<any>
|
authUserService: ModulesSdkTypes.InternalModuleService<any>
|
||||||
authProviderService: ModulesSdkTypes.InternalModuleService<any>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const generateMethodForModels = [AuthProvider, AuthUser]
|
const generateMethodForModels = [AuthUser]
|
||||||
|
|
||||||
export default class AuthModuleService<
|
export default class AuthModuleService<TAuthUser extends AuthUser = AuthUser>
|
||||||
TAuthUser extends AuthUser = AuthUser,
|
|
||||||
TAuthProvider extends AuthProvider = AuthProvider
|
|
||||||
>
|
|
||||||
extends ModulesSdkUtils.abstractModuleServiceFactory<
|
extends ModulesSdkUtils.abstractModuleServiceFactory<
|
||||||
InjectedDependencies,
|
InjectedDependencies,
|
||||||
AuthTypes.AuthProviderDTO,
|
AuthTypes.AuthUserDTO,
|
||||||
{
|
{
|
||||||
AuthUser: { dto: AuthUserDTO }
|
AuthUser: { dto: AuthUserDTO }
|
||||||
AuthProvider: { dto: AuthProviderDTO }
|
|
||||||
}
|
}
|
||||||
>(AuthProvider, generateMethodForModels, entityNameToLinkableKeysMap)
|
>(AuthUser, generateMethodForModels, entityNameToLinkableKeysMap)
|
||||||
implements AuthTypes.IAuthModuleService
|
implements AuthTypes.IAuthModuleService
|
||||||
{
|
{
|
||||||
__hooks = {
|
|
||||||
onApplicationStart: async () => await this.createProvidersOnLoad(),
|
|
||||||
}
|
|
||||||
protected baseRepository_: DAL.RepositoryService
|
protected baseRepository_: DAL.RepositoryService
|
||||||
protected authUserService_: ModulesSdkTypes.InternalModuleService<TAuthUser>
|
protected authUserService_: ModulesSdkTypes.InternalModuleService<TAuthUser>
|
||||||
protected authProviderService_: ModulesSdkTypes.InternalModuleService<TAuthProvider>
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
{
|
{ authUserService, baseRepository }: InjectedDependencies,
|
||||||
authUserService,
|
|
||||||
authProviderService,
|
|
||||||
baseRepository,
|
|
||||||
}: InjectedDependencies,
|
|
||||||
protected readonly moduleDeclaration: InternalModuleDeclaration
|
protected readonly moduleDeclaration: InternalModuleDeclaration
|
||||||
) {
|
) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
@@ -70,94 +54,25 @@ export default class AuthModuleService<
|
|||||||
|
|
||||||
this.baseRepository_ = baseRepository
|
this.baseRepository_ = baseRepository
|
||||||
this.authUserService_ = authUserService
|
this.authUserService_ = authUserService
|
||||||
this.authProviderService_ = authProviderService
|
|
||||||
}
|
}
|
||||||
|
|
||||||
__joinerConfig(): ModuleJoinerConfig {
|
__joinerConfig(): ModuleJoinerConfig {
|
||||||
return joinerConfig
|
return joinerConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
async createAuthProvider(
|
create(
|
||||||
data: CreateAuthProviderDTO[],
|
|
||||||
sharedContext?: Context
|
|
||||||
): Promise<AuthProviderDTO[]>
|
|
||||||
|
|
||||||
async createAuthProvider(
|
|
||||||
data: CreateAuthProviderDTO,
|
|
||||||
sharedContext?: Context
|
|
||||||
): Promise<AuthProviderDTO>
|
|
||||||
|
|
||||||
@InjectManager("baseRepository_")
|
|
||||||
async createAuthProvider(
|
|
||||||
data: CreateAuthProviderDTO | CreateAuthProviderDTO[],
|
|
||||||
@MedusaContext() sharedContext: Context = {}
|
|
||||||
): Promise<AuthTypes.AuthProviderDTO | AuthTypes.AuthProviderDTO[]> {
|
|
||||||
const input = Array.isArray(data) ? data : [data]
|
|
||||||
|
|
||||||
const providers = await this.createAuthProviders_(input, sharedContext)
|
|
||||||
|
|
||||||
const serializedProviders = await this.baseRepository_.serialize<
|
|
||||||
AuthTypes.AuthProviderDTO[]
|
|
||||||
>(providers, {
|
|
||||||
populate: true,
|
|
||||||
})
|
|
||||||
|
|
||||||
return Array.isArray(data) ? serializedProviders : serializedProviders[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
updateAuthProvider(
|
|
||||||
data: AuthTypes.UpdateAuthProviderDTO[],
|
|
||||||
sharedContext?: Context
|
|
||||||
): Promise<AuthProviderDTO[]>
|
|
||||||
|
|
||||||
updateAuthProvider(
|
|
||||||
data: AuthTypes.UpdateAuthProviderDTO,
|
|
||||||
sharedContext?: Context
|
|
||||||
): Promise<AuthProviderDTO>
|
|
||||||
|
|
||||||
@InjectManager("baseRepository_")
|
|
||||||
async updateAuthProvider(
|
|
||||||
data: AuthTypes.UpdateAuthProviderDTO[] | AuthTypes.UpdateAuthProviderDTO,
|
|
||||||
@MedusaContext() sharedContext: Context = {}
|
|
||||||
): Promise<AuthTypes.AuthProviderDTO | AuthTypes.AuthProviderDTO[]> {
|
|
||||||
const input = Array.isArray(data) ? data : [data]
|
|
||||||
|
|
||||||
const providers = await this.updateAuthProvider_(input, sharedContext)
|
|
||||||
|
|
||||||
const serializedProviders = await this.baseRepository_.serialize<
|
|
||||||
AuthTypes.AuthProviderDTO[]
|
|
||||||
>(providers, {
|
|
||||||
populate: true,
|
|
||||||
})
|
|
||||||
|
|
||||||
return Array.isArray(data) ? serializedProviders : serializedProviders[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
async updateAuthProvider_(
|
|
||||||
data: AuthTypes.UpdateAuthProviderDTO[],
|
|
||||||
@MedusaContext() sharedContext: Context = {}
|
|
||||||
): Promise<TAuthProvider[]> {
|
|
||||||
return await this.authProviderService_.update(data, sharedContext)
|
|
||||||
}
|
|
||||||
|
|
||||||
createAuthUser(
|
|
||||||
data: CreateAuthUserDTO[],
|
data: CreateAuthUserDTO[],
|
||||||
sharedContext?: Context
|
sharedContext?: Context
|
||||||
): Promise<AuthUserDTO[]>
|
): Promise<AuthUserDTO[]>
|
||||||
|
|
||||||
createAuthUser(
|
create(data: CreateAuthUserDTO, sharedContext?: Context): Promise<AuthUserDTO>
|
||||||
data: CreateAuthUserDTO,
|
|
||||||
sharedContext?: Context
|
|
||||||
): Promise<AuthUserDTO>
|
|
||||||
|
|
||||||
@InjectManager("baseRepository_")
|
@InjectManager("baseRepository_")
|
||||||
async createAuthUser(
|
async create(
|
||||||
data: CreateAuthUserDTO[] | CreateAuthUserDTO,
|
data: CreateAuthUserDTO[] | CreateAuthUserDTO,
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
): Promise<AuthTypes.AuthUserDTO | AuthTypes.AuthUserDTO[]> {
|
): Promise<AuthTypes.AuthUserDTO | AuthTypes.AuthUserDTO[]> {
|
||||||
const input = Array.isArray(data) ? data : [data]
|
const authUsers = await this.authUserService_.create(data, sharedContext)
|
||||||
|
|
||||||
const authUsers = await this.createAuthUsers_(input, sharedContext)
|
|
||||||
|
|
||||||
const serializedUsers = await this.baseRepository_.serialize<
|
const serializedUsers = await this.baseRepository_.serialize<
|
||||||
AuthTypes.AuthUserDTO[]
|
AuthTypes.AuthUserDTO[]
|
||||||
@@ -165,28 +80,23 @@ export default class AuthModuleService<
|
|||||||
populate: true,
|
populate: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
return Array.isArray(data) ? serializedUsers : serializedUsers[0]
|
return serializedUsers
|
||||||
}
|
}
|
||||||
|
|
||||||
updateAuthUser(
|
update(
|
||||||
data: UpdateAuthUserDTO[],
|
data: UpdateAuthUserDTO[],
|
||||||
sharedContext?: Context
|
sharedContext?: Context
|
||||||
): Promise<AuthUserDTO[]>
|
): Promise<AuthUserDTO[]>
|
||||||
|
|
||||||
updateAuthUser(
|
update(data: UpdateAuthUserDTO, sharedContext?: Context): Promise<AuthUserDTO>
|
||||||
data: UpdateAuthUserDTO,
|
|
||||||
sharedContext?: Context
|
|
||||||
): Promise<AuthUserDTO>
|
|
||||||
|
|
||||||
// TODO: should be pluralized, see convention about the methods naming or the abstract module service interface definition @engineering
|
// TODO: should be pluralized, see convention about the methods naming or the abstract module service interface definition @engineering
|
||||||
@InjectManager("baseRepository_")
|
@InjectManager("baseRepository_")
|
||||||
async updateAuthUser(
|
async update(
|
||||||
data: UpdateAuthUserDTO | UpdateAuthUserDTO[],
|
data: UpdateAuthUserDTO | UpdateAuthUserDTO[],
|
||||||
@MedusaContext() sharedContext: Context = {}
|
@MedusaContext() sharedContext: Context = {}
|
||||||
): Promise<AuthTypes.AuthUserDTO | AuthTypes.AuthUserDTO[]> {
|
): Promise<AuthTypes.AuthUserDTO | AuthTypes.AuthUserDTO[]> {
|
||||||
const input = Array.isArray(data) ? data : [data]
|
const updatedUsers = await this.authUserService_.update(data, sharedContext)
|
||||||
|
|
||||||
const updatedUsers = await this.updateAuthUsers_(input, sharedContext)
|
|
||||||
|
|
||||||
const serializedUsers = await this.baseRepository_.serialize<
|
const serializedUsers = await this.baseRepository_.serialize<
|
||||||
AuthTypes.AuthUserDTO[]
|
AuthTypes.AuthUserDTO[]
|
||||||
@@ -197,14 +107,6 @@ export default class AuthModuleService<
|
|||||||
return Array.isArray(data) ? serializedUsers : serializedUsers[0]
|
return Array.isArray(data) ? serializedUsers : serializedUsers[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager("baseRepository_")
|
|
||||||
protected async updateAuthUsers_(
|
|
||||||
data: UpdateAuthUserDTO[],
|
|
||||||
@MedusaContext() sharedContext: Context
|
|
||||||
): Promise<TAuthUser[]> {
|
|
||||||
return await this.authUserService_.update(data, sharedContext)
|
|
||||||
}
|
|
||||||
|
|
||||||
protected getRegisteredAuthenticationProvider(
|
protected getRegisteredAuthenticationProvider(
|
||||||
provider: string,
|
provider: string,
|
||||||
{ authScope }: AuthenticationInput
|
{ authScope }: AuthenticationInput
|
||||||
@@ -215,13 +117,11 @@ export default class AuthModuleService<
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new MedusaError(
|
throw new MedusaError(
|
||||||
MedusaError.Types.NOT_FOUND,
|
MedusaError.Types.NOT_FOUND,
|
||||||
`AuthenticationProvider with for provider: ${provider} wasn't registered in the module. Have you configured your options correctly?`
|
`AuthenticationProvider: ${provider} wasn't registered in the module. Have you configured your options correctly?`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
containerProvider.validateScope(authScope)
|
return containerProvider.withScope(authScope)
|
||||||
|
|
||||||
return containerProvider
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async authenticate(
|
async authenticate(
|
||||||
@@ -229,8 +129,6 @@ export default class AuthModuleService<
|
|||||||
authenticationData: AuthenticationInput
|
authenticationData: AuthenticationInput
|
||||||
): Promise<AuthenticationResponse> {
|
): Promise<AuthenticationResponse> {
|
||||||
try {
|
try {
|
||||||
await this.retrieveAuthProvider(provider, {})
|
|
||||||
|
|
||||||
const registeredProvider = this.getRegisteredAuthenticationProvider(
|
const registeredProvider = this.getRegisteredAuthenticationProvider(
|
||||||
provider,
|
provider,
|
||||||
authenticationData
|
authenticationData
|
||||||
@@ -247,8 +145,6 @@ export default class AuthModuleService<
|
|||||||
authenticationData: AuthenticationInput
|
authenticationData: AuthenticationInput
|
||||||
): Promise<AuthenticationResponse> {
|
): Promise<AuthenticationResponse> {
|
||||||
try {
|
try {
|
||||||
await this.retrieveAuthProvider(provider, {})
|
|
||||||
|
|
||||||
const registeredProvider = this.getRegisteredAuthenticationProvider(
|
const registeredProvider = this.getRegisteredAuthenticationProvider(
|
||||||
provider,
|
provider,
|
||||||
authenticationData
|
authenticationData
|
||||||
@@ -259,45 +155,4 @@ export default class AuthModuleService<
|
|||||||
return { success: false, error: error.message }
|
return { success: false, error: error.message }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@InjectTransactionManager("baseRepository_")
|
|
||||||
protected async createAuthProviders_(
|
|
||||||
data: any[],
|
|
||||||
@MedusaContext() sharedContext: Context
|
|
||||||
): Promise<TAuthProvider[]> {
|
|
||||||
return await this.authProviderService_.create(data, sharedContext)
|
|
||||||
}
|
|
||||||
|
|
||||||
@InjectTransactionManager("baseRepository_")
|
|
||||||
protected async createAuthUsers_(
|
|
||||||
data: CreateAuthUserDTO[],
|
|
||||||
@MedusaContext() sharedContext: Context
|
|
||||||
): Promise<TAuthUser[]> {
|
|
||||||
return await this.authUserService_.create(data, sharedContext)
|
|
||||||
}
|
|
||||||
|
|
||||||
private async createProvidersOnLoad() {
|
|
||||||
const providersToLoad = this.__container__["auth_providers"]
|
|
||||||
|
|
||||||
const providers = await this.authProviderService_.list({
|
|
||||||
provider: providersToLoad.map((p) => p.provider),
|
|
||||||
})
|
|
||||||
|
|
||||||
const loadedProvidersMap = new Map(providers.map((p) => [p.provider, p]))
|
|
||||||
|
|
||||||
const providersToCreate: ServiceTypes.CreateAuthProviderDTO[] = []
|
|
||||||
|
|
||||||
for (const provider of providersToLoad) {
|
|
||||||
if (loadedProvidersMap.has(provider.provider)) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
providersToCreate.push({
|
|
||||||
provider: provider.provider,
|
|
||||||
name: provider.displayName,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.authProviderService_.create(providersToCreate)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
import { AuthProvider } from "@models"
|
|
||||||
|
|
||||||
export type CreateAuthProviderDTO = {
|
|
||||||
provider: string
|
|
||||||
name: string
|
|
||||||
domain?: ProviderDomain
|
|
||||||
is_active?: boolean
|
|
||||||
config?: Record<string, unknown>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type UpdateAuthProviderDTO = {
|
|
||||||
update: {
|
|
||||||
provider: string
|
|
||||||
name?: string
|
|
||||||
domain?: ProviderDomain
|
|
||||||
is_active?: boolean
|
|
||||||
config?: Record<string, unknown>
|
|
||||||
}
|
|
||||||
provider: AuthProvider
|
|
||||||
}
|
|
||||||
|
|
||||||
export enum ProviderDomain {
|
|
||||||
ALL = "all",
|
|
||||||
STORE = "store",
|
|
||||||
ADMIN = "admin",
|
|
||||||
}
|
|
||||||
@@ -1,2 +1 @@
|
|||||||
export * from "./auth-user"
|
export * from "./auth-user"
|
||||||
export * from "./auth-provider"
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
export type AuthProviderDTO = {
|
|
||||||
provider: string
|
|
||||||
name: string
|
|
||||||
scope: string
|
|
||||||
is_active: boolean
|
|
||||||
config: Record<string, unknown>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type CreateAuthProviderDTO = {
|
|
||||||
provider: string
|
|
||||||
name: string
|
|
||||||
scope?: string
|
|
||||||
is_active?: boolean
|
|
||||||
config?: Record<string, unknown>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type UpdateAuthProviderDTO = {
|
|
||||||
provider: string
|
|
||||||
name?: string
|
|
||||||
is_active?: boolean
|
|
||||||
config?: Record<string, unknown>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type FilterableAuthProviderProps = {}
|
|
||||||
@@ -1,11 +1,9 @@
|
|||||||
import { AuthProviderDTO } from "./auth-provider"
|
|
||||||
|
|
||||||
export type AuthUserDTO = {
|
export type AuthUserDTO = {
|
||||||
id: string
|
id: string
|
||||||
provider_id: string
|
provider_id: string
|
||||||
entity_id: string
|
entity_id: string
|
||||||
scope: string
|
scope: string
|
||||||
provider: AuthProviderDTO
|
provider: string
|
||||||
provider_metadata?: Record<string, unknown>
|
provider_metadata?: Record<string, unknown>
|
||||||
user_metadata: Record<string, unknown>
|
user_metadata: Record<string, unknown>
|
||||||
app_metadata: Record<string, unknown>
|
app_metadata: Record<string, unknown>
|
||||||
|
|||||||
@@ -1,2 +1 @@
|
|||||||
export * from "./auth-user"
|
export * from "./auth-user"
|
||||||
export * from "./auth-provider"
|
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||||
|
|
||||||
import { IAuthModuleService } from "@medusajs/types"
|
import { IAuthModuleService } from "@medusajs/types"
|
||||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||||
import { isDefined } from "@medusajs/utils"
|
import { isDefined } from "@medusajs/utils"
|
||||||
@@ -17,7 +18,7 @@ export const setAuthAppMetadataStep = createStep(
|
|||||||
ModuleRegistrationName.AUTH
|
ModuleRegistrationName.AUTH
|
||||||
)
|
)
|
||||||
|
|
||||||
const authUser = await service.retrieveAuthUser(data.authUserId)
|
const authUser = await service.retrieve(data.authUserId)
|
||||||
|
|
||||||
const appMetadata = authUser.app_metadata || {}
|
const appMetadata = authUser.app_metadata || {}
|
||||||
if (isDefined(appMetadata[data.key])) {
|
if (isDefined(appMetadata[data.key])) {
|
||||||
@@ -26,7 +27,7 @@ export const setAuthAppMetadataStep = createStep(
|
|||||||
|
|
||||||
appMetadata[data.key] = data.value
|
appMetadata[data.key] = data.value
|
||||||
|
|
||||||
await service.updateAuthUser({
|
await service.update({
|
||||||
id: authUser.id,
|
id: authUser.id,
|
||||||
app_metadata: appMetadata,
|
app_metadata: appMetadata,
|
||||||
})
|
})
|
||||||
@@ -44,14 +45,14 @@ export const setAuthAppMetadataStep = createStep(
|
|||||||
ModuleRegistrationName.AUTH
|
ModuleRegistrationName.AUTH
|
||||||
)
|
)
|
||||||
|
|
||||||
const authUser = await service.retrieveAuthUser(id)
|
const authUser = await service.retrieve(id)
|
||||||
|
|
||||||
const appMetadata = authUser.app_metadata || {}
|
const appMetadata = authUser.app_metadata || {}
|
||||||
if (isDefined(appMetadata[key])) {
|
if (isDefined(appMetadata[key])) {
|
||||||
delete appMetadata[key]
|
delete appMetadata[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
await service.updateAuthUser({
|
await service.update({
|
||||||
id: authUser.id,
|
id: authUser.id,
|
||||||
app_metadata: appMetadata,
|
app_metadata: appMetadata,
|
||||||
})
|
})
|
||||||
|
|||||||
+1
-1
@@ -1,8 +1,8 @@
|
|||||||
import { ICustomerModuleService } from "@medusajs/types"
|
import { ICustomerModuleService } from "@medusajs/types"
|
||||||
import { MikroOrmWrapper } from "../../../utils"
|
import { MikroOrmWrapper } from "../../../utils"
|
||||||
import { Modules } from "@medusajs/modules-sdk"
|
import { Modules } from "@medusajs/modules-sdk"
|
||||||
import { initModules } from "medusa-test-utils"
|
|
||||||
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
||||||
|
import { initModules } from "medusa-test-utils"
|
||||||
|
|
||||||
jest.setTimeout(30000)
|
jest.setTimeout(30000)
|
||||||
|
|
||||||
|
|||||||
@@ -1,33 +0,0 @@
|
|||||||
import { BaseFilterable } from "../../dal"
|
|
||||||
|
|
||||||
export type AuthProviderDTO = {
|
|
||||||
provider: string
|
|
||||||
name: string
|
|
||||||
scope?: string
|
|
||||||
is_active: boolean
|
|
||||||
config: Record<string, unknown> | null
|
|
||||||
}
|
|
||||||
|
|
||||||
export type CreateAuthProviderDTO = {
|
|
||||||
provider: string
|
|
||||||
name: string
|
|
||||||
scope?: string
|
|
||||||
is_active?: boolean
|
|
||||||
config?: Record<string, unknown>
|
|
||||||
}
|
|
||||||
|
|
||||||
export type UpdateAuthProviderDTO = {
|
|
||||||
provider: string
|
|
||||||
name?: string
|
|
||||||
is_active?: boolean
|
|
||||||
config?: Record<string, unknown>
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface FilterableAuthProviderProps
|
|
||||||
extends BaseFilterable<FilterableAuthProviderProps> {
|
|
||||||
id?: string | string[]
|
|
||||||
provider?: string[]
|
|
||||||
is_active?: boolean
|
|
||||||
scope?: string[]
|
|
||||||
name?: string[]
|
|
||||||
}
|
|
||||||
@@ -1,18 +1,17 @@
|
|||||||
import { AuthProviderDTO } from "./auth-provider"
|
|
||||||
import { BaseFilterable } from "../../dal"
|
import { BaseFilterable } from "../../dal"
|
||||||
|
|
||||||
export type AuthUserDTO = {
|
export type AuthUserDTO = {
|
||||||
id: string
|
id: string
|
||||||
provider_id: string
|
provider: string
|
||||||
entity_id: string
|
entity_id: string
|
||||||
scope: string
|
scope: string
|
||||||
provider: AuthProviderDTO
|
|
||||||
provider_metadata?: Record<string, unknown>
|
provider_metadata?: Record<string, unknown>
|
||||||
user_metadata: Record<string, unknown>
|
user_metadata: Record<string, unknown>
|
||||||
app_metadata: Record<string, unknown>
|
app_metadata: Record<string, unknown>
|
||||||
}
|
}
|
||||||
|
|
||||||
export type CreateAuthUserDTO = {
|
export type CreateAuthUserDTO = {
|
||||||
|
id?: string
|
||||||
provider: string
|
provider: string
|
||||||
entity_id: string
|
entity_id: string
|
||||||
scope: string
|
scope: string
|
||||||
|
|||||||
@@ -1,3 +1,2 @@
|
|||||||
export * from "./auth-user"
|
export * from "./auth-user"
|
||||||
export * from "./auth-provider"
|
|
||||||
export * from "./provider"
|
export * from "./provider"
|
||||||
@@ -1,13 +1,9 @@
|
|||||||
import {
|
import {
|
||||||
|
AuthUserDTO,
|
||||||
AuthenticationInput,
|
AuthenticationInput,
|
||||||
AuthenticationResponse,
|
AuthenticationResponse,
|
||||||
AuthProviderDTO,
|
|
||||||
AuthUserDTO,
|
|
||||||
CreateAuthProviderDTO,
|
|
||||||
CreateAuthUserDTO,
|
CreateAuthUserDTO,
|
||||||
FilterableAuthProviderProps,
|
|
||||||
FilterableAuthUserProps,
|
FilterableAuthUserProps,
|
||||||
UpdateAuthProviderDTO,
|
|
||||||
UpdateAuthUserDTO,
|
UpdateAuthUserDTO,
|
||||||
} from "./common"
|
} from "./common"
|
||||||
|
|
||||||
@@ -30,83 +26,37 @@ export interface IAuthModuleService extends IModuleService {
|
|||||||
providerData: AuthenticationInput
|
providerData: AuthenticationInput
|
||||||
): Promise<AuthenticationResponse>
|
): Promise<AuthenticationResponse>
|
||||||
|
|
||||||
retrieveAuthProvider(
|
retrieve(
|
||||||
provider: string,
|
|
||||||
config?: FindConfig<AuthProviderDTO>,
|
|
||||||
sharedContext?: Context
|
|
||||||
): Promise<AuthProviderDTO>
|
|
||||||
|
|
||||||
listAuthProviders(
|
|
||||||
filters?: FilterableAuthProviderProps,
|
|
||||||
config?: FindConfig<AuthProviderDTO>,
|
|
||||||
sharedContext?: Context
|
|
||||||
): Promise<AuthProviderDTO[]>
|
|
||||||
|
|
||||||
listAndCountAuthProviders(
|
|
||||||
filters?: FilterableAuthProviderProps,
|
|
||||||
config?: FindConfig<AuthProviderDTO>,
|
|
||||||
sharedContext?: Context
|
|
||||||
): Promise<[AuthProviderDTO[], number]>
|
|
||||||
|
|
||||||
createAuthProvider(
|
|
||||||
data: CreateAuthProviderDTO[],
|
|
||||||
sharedContext?: Context
|
|
||||||
): Promise<AuthProviderDTO[]>
|
|
||||||
|
|
||||||
createAuthProvider(
|
|
||||||
data: CreateAuthProviderDTO,
|
|
||||||
sharedContext?: Context
|
|
||||||
): Promise<AuthProviderDTO>
|
|
||||||
|
|
||||||
updateAuthProvider(
|
|
||||||
data: UpdateAuthProviderDTO[],
|
|
||||||
sharedContext?: Context
|
|
||||||
): Promise<AuthProviderDTO[]>
|
|
||||||
|
|
||||||
updateAuthProvider(
|
|
||||||
data: UpdateAuthProviderDTO,
|
|
||||||
sharedContext?: Context
|
|
||||||
): Promise<AuthProviderDTO>
|
|
||||||
|
|
||||||
deleteAuthProviders(ids: string[], sharedContext?: Context): Promise<void>
|
|
||||||
|
|
||||||
retrieveAuthUser(
|
|
||||||
id: string,
|
id: string,
|
||||||
config?: FindConfig<AuthUserDTO>,
|
config?: FindConfig<AuthUserDTO>,
|
||||||
sharedContext?: Context
|
sharedContext?: Context
|
||||||
): Promise<AuthUserDTO>
|
): Promise<AuthUserDTO>
|
||||||
|
|
||||||
listAuthUsers(
|
list(
|
||||||
filters?: FilterableAuthProviderProps,
|
filters?: FilterableAuthUserProps,
|
||||||
config?: FindConfig<AuthUserDTO>,
|
config?: FindConfig<AuthUserDTO>,
|
||||||
sharedContext?: Context
|
sharedContext?: Context
|
||||||
): Promise<AuthUserDTO[]>
|
): Promise<AuthUserDTO[]>
|
||||||
|
|
||||||
listAndCountAuthUsers(
|
listAndCount(
|
||||||
filters?: FilterableAuthUserProps,
|
filters?: FilterableAuthUserProps,
|
||||||
config?: FindConfig<AuthUserDTO>,
|
config?: FindConfig<AuthUserDTO>,
|
||||||
sharedContext?: Context
|
sharedContext?: Context
|
||||||
): Promise<[AuthUserDTO[], number]>
|
): Promise<[AuthUserDTO[], number]>
|
||||||
|
|
||||||
createAuthUser(
|
create(
|
||||||
data: CreateAuthUserDTO[],
|
data: CreateAuthUserDTO[],
|
||||||
sharedContext?: Context
|
sharedContext?: Context
|
||||||
): Promise<AuthUserDTO[]>
|
): Promise<AuthUserDTO[]>
|
||||||
|
|
||||||
createAuthUser(
|
create(data: CreateAuthUserDTO, sharedContext?: Context): Promise<AuthUserDTO>
|
||||||
data: CreateAuthUserDTO,
|
|
||||||
sharedContext?: Context
|
|
||||||
): Promise<AuthUserDTO>
|
|
||||||
|
|
||||||
updateAuthUser(
|
update(
|
||||||
data: UpdateAuthUserDTO[],
|
data: UpdateAuthUserDTO[],
|
||||||
sharedContext?: Context
|
sharedContext?: Context
|
||||||
): Promise<AuthUserDTO[]>
|
): Promise<AuthUserDTO[]>
|
||||||
|
|
||||||
updateAuthUser(
|
update(data: UpdateAuthUserDTO, sharedContext?: Context): Promise<AuthUserDTO>
|
||||||
data: UpdateAuthUserDTO,
|
|
||||||
sharedContext?: Context
|
|
||||||
): Promise<AuthUserDTO>
|
|
||||||
|
|
||||||
deleteAuthUsers(ids: string[], sharedContext?: Context): Promise<void>
|
delete(ids: string[], sharedContext?: Context): Promise<void>
|
||||||
}
|
}
|
||||||
@@ -5,22 +5,34 @@ import { MedusaError } from "../common"
|
|||||||
export abstract class AbstractAuthModuleProvider {
|
export abstract class AbstractAuthModuleProvider {
|
||||||
public static PROVIDER: string
|
public static PROVIDER: string
|
||||||
public static DISPLAY_NAME: string
|
public static DISPLAY_NAME: string
|
||||||
protected readonly scopes_: Record<string, AuthProviderScope>
|
|
||||||
|
protected readonly container_: any
|
||||||
|
protected scopeConfig_: AuthProviderScope
|
||||||
|
protected scope_: string
|
||||||
|
|
||||||
|
private readonly scopes_: Record<string, AuthProviderScope>
|
||||||
|
|
||||||
public get provider() {
|
public get provider() {
|
||||||
return (this.constructor as Function & { PROVIDER: string }).PROVIDER
|
return (this.constructor as typeof AbstractAuthModuleProvider).PROVIDER
|
||||||
}
|
}
|
||||||
|
|
||||||
public get displayName() {
|
public get displayName() {
|
||||||
return (this.constructor as Function & { DISPLAY_NAME: string })
|
return (this.constructor as typeof AbstractAuthModuleProvider).DISPLAY_NAME
|
||||||
.DISPLAY_NAME
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected constructor({ scopes }) {
|
protected constructor(
|
||||||
|
{ scopes },
|
||||||
|
config: { provider: string; displayName: string }
|
||||||
|
) {
|
||||||
|
this.container_ = arguments[0]
|
||||||
this.scopes_ = scopes
|
this.scopes_ = scopes
|
||||||
|
;(this.constructor as typeof AbstractAuthModuleProvider).PROVIDER ??=
|
||||||
|
config.provider
|
||||||
|
;(this.constructor as typeof AbstractAuthModuleProvider).DISPLAY_NAME ??=
|
||||||
|
config.displayName
|
||||||
}
|
}
|
||||||
|
|
||||||
public validateScope(scope) {
|
private validateScope(scope) {
|
||||||
if (!this.scopes_[scope]) {
|
if (!this.scopes_[scope]) {
|
||||||
throw new MedusaError(
|
throw new MedusaError(
|
||||||
MedusaError.Types.INVALID_ARGUMENT,
|
MedusaError.Types.INVALID_ARGUMENT,
|
||||||
@@ -29,6 +41,16 @@ export abstract class AbstractAuthModuleProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public withScope(scope: string) {
|
||||||
|
this.validateScope(scope)
|
||||||
|
|
||||||
|
const cloned = new (this.constructor as any)(this.container_)
|
||||||
|
cloned.scope_ = scope
|
||||||
|
cloned.scopeConfg_ = this.scopes_[scope]
|
||||||
|
|
||||||
|
return cloned
|
||||||
|
}
|
||||||
|
|
||||||
abstract authenticate(
|
abstract authenticate(
|
||||||
data: Record<string, unknown>
|
data: Record<string, unknown>
|
||||||
): Promise<AuthenticationResponse>
|
): Promise<AuthenticationResponse>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { DAL, FindConfig } from "@medusajs/types"
|
import { DAL, FindConfig } from "@medusajs/types"
|
||||||
|
|
||||||
import { deduplicate, isDefined, isObject } from "../common"
|
import { deduplicate, isDefined, isObject } from "../common"
|
||||||
|
|
||||||
import { SoftDeletableFilterKey } from "../dal"
|
import { SoftDeletableFilterKey } from "../dal"
|
||||||
|
|
||||||
export function buildQuery<T = any, TDto = any>(
|
export function buildQuery<T = any, TDto = any>(
|
||||||
|
|||||||
Reference in new issue
Block a user