Feat(auth): Rename authentication to auth (#6229)
**What** - rename `authenticationModule` -> `authModule`
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
# @medusajs/authentication
|
||||
# @medusajs/auth
|
||||
|
||||
## 0.0.2
|
||||
|
||||
3
packages/auth/README.md
Normal file
3
packages/auth/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# Auth Module
|
||||
|
||||
The Auth Module is Medusa’s authentication engine engine. It provides functions to authenticate users through identity providers and store metadata about users that can be used for authorization purposes.
|
||||
@@ -1,16 +1,16 @@
|
||||
import { IAuthenticationModuleService } from "@medusajs/types"
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { MikroOrmWrapper } from "../../../utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { createAuthProviders } from "../../../__fixtures__/auth-provider"
|
||||
import { createAuthUsers } from "../../../__fixtures__/auth-user"
|
||||
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
||||
import { initModules } from "medusa-test-utils/dist"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("AuthenticationModuleService - AuthProvider", () => {
|
||||
let service: IAuthenticationModuleService
|
||||
describe("AuthModuleService - AuthProvider", () => {
|
||||
let service: IAuthModuleService
|
||||
let testManager: SqlEntityManager
|
||||
let shutdownFunc: () => Promise<void>
|
||||
|
||||
@@ -19,7 +19,7 @@ describe("AuthenticationModuleService - AuthProvider", () => {
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
service = medusaApp.modules[Modules.AUTHENTICATION]
|
||||
service = medusaApp.modules[Modules.AUTH]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
@@ -1,17 +1,16 @@
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { MikroOrmWrapper } from "../../../utils"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { createAuthProviders } from "../../../__fixtures__/auth-provider"
|
||||
import { createAuthUsers } from "../../../__fixtures__/auth-user"
|
||||
import { IAuthenticationModuleService } from "@medusajs/types"
|
||||
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
||||
import { initModules } from "medusa-test-utils/dist"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("AuthenticationModuleService - AuthUser", () => {
|
||||
let service: IAuthenticationModuleService
|
||||
describe("AuthModuleService - AuthUser", () => {
|
||||
let service: IAuthModuleService
|
||||
let testManager: SqlEntityManager
|
||||
let shutdownFunc: () => Promise<void>
|
||||
|
||||
@@ -20,7 +19,7 @@ describe("AuthenticationModuleService - AuthUser", () => {
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
service = medusaApp.modules[Modules.AUTHENTICATION]
|
||||
service = medusaApp.modules[Modules.AUTH]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
@@ -1,16 +1,16 @@
|
||||
import { MedusaModule, Modules } from "@medusajs/modules-sdk"
|
||||
|
||||
import { IAuthenticationModuleService } from "@medusajs/types"
|
||||
import { IAuthModuleService } from "@medusajs/types"
|
||||
import { MikroOrmWrapper } from "../../../utils"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { createAuthProviders } from "../../../__fixtures__/auth-provider"
|
||||
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
|
||||
import { initModules } from "medusa-test-utils/dist"
|
||||
import { initModules } from "medusa-test-utils"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("AuthenticationModuleService - AuthProvider", () => {
|
||||
let service: IAuthenticationModuleService
|
||||
describe("AuthModuleService - AuthProvider", () => {
|
||||
let service: IAuthModuleService
|
||||
let testManager: SqlEntityManager
|
||||
let shutdownFunc: () => Promise<void>
|
||||
|
||||
@@ -19,7 +19,7 @@ describe("AuthenticationModuleService - AuthProvider", () => {
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
service = medusaApp.modules[Modules.AUTHENTICATION]
|
||||
service = medusaApp.modules[Modules.AUTH]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
@@ -50,8 +50,8 @@ describe("AuthenticationModuleService - AuthProvider", () => {
|
||||
expect(serialized).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
provider: "usernamePassword",
|
||||
name: "Username/Password Authentication",
|
||||
provider: "emailpass",
|
||||
name: "Email/Password Authentication",
|
||||
}),
|
||||
expect.objectContaining({
|
||||
provider: "google",
|
||||
@@ -71,7 +71,7 @@ describe("AuthenticationModuleService - AuthProvider", () => {
|
||||
},
|
||||
])
|
||||
|
||||
const { success, error } = await service.authenticate("notRegistered", {})
|
||||
const { success, error } = await service.authenticate("notRegistered", {} as any)
|
||||
|
||||
expect(success).toBe(false)
|
||||
expect(error).toEqual(
|
||||
@@ -81,15 +81,15 @@ describe("AuthenticationModuleService - AuthProvider", () => {
|
||||
|
||||
it("fails to authenticate using a valid provider with an invalid scope", async () => {
|
||||
const { success, error } = await service.authenticate(
|
||||
"usernamePassword",
|
||||
"emailpass",
|
||||
{
|
||||
scope: "non-existing",
|
||||
}
|
||||
} as any
|
||||
)
|
||||
|
||||
expect(success).toBe(false)
|
||||
expect(error).toEqual(
|
||||
`Scope "non-existing" is not valid for provider usernamePassword`
|
||||
`Scope "non-existing" is not valid for provider emailpass`
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -1,6 +1,6 @@
|
||||
import { AuthenticationInput, IAuthModuleService } from "@medusajs/types"
|
||||
import { MedusaModule, Modules } from "@medusajs/modules-sdk"
|
||||
|
||||
import { IAuthenticationModuleService } from "@medusajs/types"
|
||||
import { MikroOrmWrapper } from "../../../utils"
|
||||
import Scrypt from "scrypt-kdf"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
@@ -15,8 +15,8 @@ const seedDefaultData = async (testManager) => {
|
||||
await createAuthUsers(testManager)
|
||||
}
|
||||
|
||||
describe("AuthenticationModuleService - AuthProvider", () => {
|
||||
let service: IAuthenticationModuleService
|
||||
describe("AuthModuleService - AuthProvider", () => {
|
||||
let service: IAuthModuleService
|
||||
let testManager: SqlEntityManager
|
||||
let shutdownFunc: () => Promise<void>
|
||||
|
||||
@@ -25,7 +25,7 @@ describe("AuthenticationModuleService - AuthProvider", () => {
|
||||
|
||||
const { medusaApp, shutdown } = await initModules(initModulesConfig)
|
||||
|
||||
service = medusaApp.modules[Modules.AUTHENTICATION]
|
||||
service = medusaApp.modules[Modules.AUTH]
|
||||
|
||||
shutdownFunc = shutdown
|
||||
})
|
||||
@@ -60,7 +60,7 @@ describe("AuthenticationModuleService - AuthProvider", () => {
|
||||
await createAuthUsers(testManager, [
|
||||
// Add authenticated user
|
||||
{
|
||||
provider: "usernamePassword",
|
||||
provider: "emailpass",
|
||||
entity_id: email,
|
||||
provider_metadata: {
|
||||
password: passwordHash,
|
||||
@@ -68,7 +68,7 @@ describe("AuthenticationModuleService - AuthProvider", () => {
|
||||
},
|
||||
])
|
||||
|
||||
const res = await service.authenticate("usernamePassword", {
|
||||
const res = await service.authenticate("emailpass", {
|
||||
body: {
|
||||
email: "test@test.com",
|
||||
password: password,
|
||||
@@ -90,7 +90,7 @@ describe("AuthenticationModuleService - AuthProvider", () => {
|
||||
|
||||
await seedDefaultData(testManager)
|
||||
|
||||
const res = await service.authenticate("usernamePassword", {
|
||||
const res = await service.authenticate("emailpass", {
|
||||
body: { email: "test@test.com" },
|
||||
scope: "store",
|
||||
})
|
||||
@@ -104,7 +104,7 @@ describe("AuthenticationModuleService - AuthProvider", () => {
|
||||
it("fails when no email is given", async () => {
|
||||
await seedDefaultData(testManager)
|
||||
|
||||
const res = await service.authenticate("usernamePassword", {
|
||||
const res = await service.authenticate("emailpass", {
|
||||
body: { password: "supersecret" },
|
||||
scope: "store",
|
||||
})
|
||||
@@ -126,7 +126,7 @@ describe("AuthenticationModuleService - AuthProvider", () => {
|
||||
await createAuthUsers(testManager, [
|
||||
// Add authenticated user
|
||||
{
|
||||
provider: "usernamePassword",
|
||||
provider: "emailpass",
|
||||
entity_id: email,
|
||||
provider_metadata: {
|
||||
password_hash: passwordHash,
|
||||
@@ -134,7 +134,7 @@ describe("AuthenticationModuleService - AuthProvider", () => {
|
||||
},
|
||||
])
|
||||
|
||||
const res = await service.authenticate("usernamePassword", {
|
||||
const res = await service.authenticate("emailpass", {
|
||||
body: {
|
||||
email: "test@test.com",
|
||||
password: "password",
|
||||
6
packages/auth/integration-tests/setup-env.js
Normal file
6
packages/auth/integration-tests/setup-env.js
Normal file
@@ -0,0 +1,6 @@
|
||||
if (typeof process.env.DB_TEMP_NAME === "undefined") {
|
||||
const tempName = parseInt(process.env.JEST_WORKER_ID || "1")
|
||||
process.env.DB_TEMP_NAME = `medusa-auth-integration-${tempName}`
|
||||
}
|
||||
|
||||
process.env.MEDUSA_AUTH_DB_SCHEMA = "public"
|
||||
@@ -2,5 +2,5 @@ import { ModuleServiceInitializeOptions } from "@medusajs/types"
|
||||
|
||||
export const databaseOptions: ModuleServiceInitializeOptions["database"] = {
|
||||
schema: "public",
|
||||
clientUrl: "medusa-authentication-test",
|
||||
clientUrl: "medusa-auth-test",
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
import * as AuthModels from "@models"
|
||||
|
||||
import { TestDatabaseUtils } from "medusa-test-utils"
|
||||
|
||||
import * as AuthenticationModels from "@models"
|
||||
|
||||
const pathToMigrations = "../../src/migrations"
|
||||
const mikroOrmEntities = AuthenticationModels as unknown as any[]
|
||||
const mikroOrmEntities = AuthModels as unknown as any[]
|
||||
|
||||
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper(
|
||||
mikroOrmEntities,
|
||||
@@ -7,12 +7,12 @@ export function getInitModuleConfig() {
|
||||
defaultAdapterOptions: {
|
||||
database: {
|
||||
clientUrl: DB_URL,
|
||||
schema: process.env.MEDUSA_AUTHENTICATION_DB_SCHEMA,
|
||||
schema: process.env.MEDUSA_AUTH_DB_SCHEMA,
|
||||
},
|
||||
},
|
||||
providers: [
|
||||
{
|
||||
name: "usernamePassword",
|
||||
name: "emailpass",
|
||||
scopes: {
|
||||
admin: {},
|
||||
store: {},
|
||||
@@ -24,8 +24,8 @@ export function getInitModuleConfig() {
|
||||
const injectedDependencies = {}
|
||||
|
||||
const modulesConfig_ = {
|
||||
[Modules.AUTHENTICATION]: {
|
||||
definition: ModulesDefinition[Modules.AUTHENTICATION],
|
||||
[Modules.AUTH]: {
|
||||
definition: ModulesDefinition[Modules.AUTH],
|
||||
options: moduleOptions,
|
||||
},
|
||||
}
|
||||
@@ -35,7 +35,7 @@ export function getInitModuleConfig() {
|
||||
modulesConfig: modulesConfig_,
|
||||
databaseConfig: {
|
||||
clientUrl: DB_URL,
|
||||
schema: process.env.MEDUSA_AUTHENTICATION_DB_SCHEMA,
|
||||
schema: process.env.MEDUSA_AUTH_DB_SCHEMA,
|
||||
},
|
||||
joinerConfig: [],
|
||||
}
|
||||
@@ -3,6 +3,6 @@ import * as entities from "./src/models"
|
||||
module.exports = {
|
||||
entities: Object.values(entities),
|
||||
schema: "public",
|
||||
clientUrl: "postgres://postgres@localhost/medusa-authentication",
|
||||
clientUrl: "postgres://postgres@localhost/medusa-auth",
|
||||
type: "postgresql",
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@medusajs/authentication",
|
||||
"name": "@medusajs/auth",
|
||||
"version": "0.0.2",
|
||||
"description": "Medusa Authentication module",
|
||||
"description": "Medusa Auth module",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"files": [
|
||||
@@ -11,12 +11,12 @@
|
||||
"node": ">=16"
|
||||
},
|
||||
"bin": {
|
||||
"medusa-authentication-seed": "dist/scripts/bin/run-seed.js"
|
||||
"medusa-auth-seed": "dist/scripts/bin/run-seed.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/medusajs/medusa",
|
||||
"directory": "packages/authentication"
|
||||
"directory": "packages/auth"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
MedusaModule,
|
||||
Modules,
|
||||
} from "@medusajs/modules-sdk"
|
||||
import { IAuthenticationModuleService, ModulesSdkTypes } from "@medusajs/types"
|
||||
import { IAuthModuleService, ModulesSdkTypes } from "@medusajs/types"
|
||||
|
||||
import { InitializeModuleInjectableDependencies } from "../types"
|
||||
import { moduleDefinition } from "../module-definition"
|
||||
@@ -16,10 +16,10 @@ export const initialize = async (
|
||||
| ModulesSdkTypes.ModuleServiceInitializeOptions
|
||||
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions,
|
||||
injectedDependencies?: InitializeModuleInjectableDependencies
|
||||
): Promise<IAuthenticationModuleService> => {
|
||||
const loaded = await MedusaModule.bootstrap<IAuthenticationModuleService>({
|
||||
moduleKey: Modules.AUTHENTICATION,
|
||||
defaultPath: MODULE_PACKAGE_NAMES[Modules.AUTHENTICATION],
|
||||
): Promise<IAuthModuleService> => {
|
||||
const loaded = await MedusaModule.bootstrap<IAuthModuleService>({
|
||||
moduleKey: Modules.AUTH,
|
||||
defaultPath: MODULE_PACKAGE_NAMES[Modules.AUTH],
|
||||
declaration: options as
|
||||
| InternalModuleDeclaration
|
||||
| ExternalModuleDeclaration, // TODO: Add provider configuration
|
||||
@@ -27,5 +27,5 @@ export const initialize = async (
|
||||
moduleExports: moduleDefinition,
|
||||
})
|
||||
|
||||
return loaded[Modules.AUTHENTICATION]
|
||||
return loaded[Modules.AUTH]
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ModuleJoinerConfig } from "@medusajs/types"
|
||||
import { MapToConfig } from "@medusajs/utils"
|
||||
import { AuthUser } from "@models"
|
||||
import { MapToConfig } from "@medusajs/utils"
|
||||
import { ModuleJoinerConfig } from "@medusajs/types"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
|
||||
export const LinkableKeys = {
|
||||
auth_user_id: AuthUser.name,
|
||||
@@ -19,7 +19,7 @@ Object.entries(LinkableKeys).forEach(([key, value]) => {
|
||||
export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap
|
||||
|
||||
export const joinerConfig: ModuleJoinerConfig = {
|
||||
serviceName: Modules.AUTHENTICATION,
|
||||
serviceName: Modules.AUTH,
|
||||
primaryKeys: ["id"],
|
||||
linkableKeys: LinkableKeys,
|
||||
alias: {
|
||||
@@ -1,12 +1,14 @@
|
||||
import * as AuthModels from "../models"
|
||||
|
||||
import {
|
||||
InternalModuleDeclaration,
|
||||
LoaderOptions,
|
||||
Modules,
|
||||
} from "@medusajs/modules-sdk"
|
||||
|
||||
import { EntitySchema } from "@mikro-orm/core"
|
||||
import { ModulesSdkTypes } from "@medusajs/types"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { EntitySchema } from "@mikro-orm/core"
|
||||
import * as AuthenticationModules from "../models"
|
||||
|
||||
export default async (
|
||||
{
|
||||
@@ -20,12 +22,12 @@ export default async (
|
||||
moduleDeclaration?: InternalModuleDeclaration
|
||||
): Promise<void> => {
|
||||
const entities = Object.values(
|
||||
AuthenticationModules
|
||||
AuthModels
|
||||
) as unknown as EntitySchema[]
|
||||
const pathToMigrations = __dirname + "/../migrations"
|
||||
|
||||
await ModulesSdkUtils.mikroOrmConnectionLoader({
|
||||
moduleName: Modules.AUTHENTICATION,
|
||||
moduleName: Modules.AUTH,
|
||||
entities,
|
||||
container,
|
||||
options,
|
||||
71
packages/auth/src/loaders/providers.ts
Normal file
71
packages/auth/src/loaders/providers.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import * as defaultProviders from "@providers"
|
||||
|
||||
import {
|
||||
asClass,
|
||||
AwilixContainer,
|
||||
ClassOrFunctionReturning,
|
||||
Constructor,
|
||||
Resolver,
|
||||
} from "awilix"
|
||||
import {
|
||||
AuthModuleProviderConfig,
|
||||
AuthProviderScope,
|
||||
LoaderOptions,
|
||||
ModulesSdkTypes,
|
||||
} from "@medusajs/types"
|
||||
|
||||
type AuthModuleProviders = {
|
||||
providers: AuthModuleProviderConfig[]
|
||||
}
|
||||
|
||||
export default async ({
|
||||
container,
|
||||
options,
|
||||
}: LoaderOptions<
|
||||
(
|
||||
| ModulesSdkTypes.ModuleServiceInitializeOptions
|
||||
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions
|
||||
) &
|
||||
AuthModuleProviders
|
||||
>): Promise<void> => {
|
||||
const providerMap = new Map(
|
||||
options?.providers?.map((provider) => [provider.name, provider.scopes]) ??
|
||||
[]
|
||||
)
|
||||
// if(options?.providers?.length) {
|
||||
// TODO: implement plugin provider registration
|
||||
// }
|
||||
|
||||
const providersToLoad = Object.values(defaultProviders)
|
||||
|
||||
for (const provider of providersToLoad) {
|
||||
container.register({
|
||||
[`auth_provider_${provider.PROVIDER}`]: asClass(
|
||||
provider as Constructor<any>
|
||||
)
|
||||
.singleton()
|
||||
.inject(() => ({ scopes: providerMap.get(provider.PROVIDER) ?? {} })),
|
||||
})
|
||||
}
|
||||
|
||||
container.register({
|
||||
[`auth_providers`]: asArray(providersToLoad, providerMap),
|
||||
})
|
||||
}
|
||||
|
||||
function asArray(
|
||||
resolvers: (ClassOrFunctionReturning<unknown> | Resolver<unknown>)[],
|
||||
providerScopeMap: Map<string, Record<string, AuthProviderScope>>
|
||||
): { resolve: (container: AwilixContainer) => unknown[] } {
|
||||
return {
|
||||
resolve: (container: AwilixContainer) =>
|
||||
resolvers.map((resolver) =>
|
||||
asClass(resolver as Constructor<any>)
|
||||
.inject(() => ({
|
||||
// @ts-ignore
|
||||
scopes: providerScopeMap.get(resolver.PROVIDER) ?? {},
|
||||
}))
|
||||
.resolve(container)
|
||||
),
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,15 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import * as Models from "@models"
|
||||
import { AuthenticationModuleService } from "@services"
|
||||
|
||||
import { AuthModuleService } from "@services"
|
||||
import { ModuleExports } from "@medusajs/types"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ModulesSdkUtils } from "@medusajs/utils"
|
||||
import loadConnection from "./loaders/connection"
|
||||
import loadContainer from "./loaders/container"
|
||||
import loadProviders from "./loaders/providers"
|
||||
|
||||
const migrationScriptOptions = {
|
||||
moduleName: Modules.AUTHENTICATION,
|
||||
moduleName: Modules.AUTH,
|
||||
models: Models,
|
||||
pathToMigrations: __dirname + "/migrations",
|
||||
}
|
||||
@@ -20,7 +21,7 @@ export const revertMigration = ModulesSdkUtils.buildRevertMigrationScript(
|
||||
migrationScriptOptions
|
||||
)
|
||||
|
||||
const service = AuthenticationModuleService
|
||||
const service = AuthModuleService
|
||||
const loaders = [loadContainer, loadConnection, loadProviders] as any
|
||||
|
||||
export const moduleDefinition: ModuleExports = {
|
||||
@@ -1,12 +1,12 @@
|
||||
import { AbstractAuthenticationModuleProvider, isString } from "@medusajs/utils"
|
||||
import { AbstractAuthModuleProvider, isString } from "@medusajs/utils"
|
||||
import { AuthenticationInput, AuthenticationResponse } from "@medusajs/types"
|
||||
|
||||
import { AuthUserService } from "@services"
|
||||
import { AuthenticationInput, AuthenticationResponse } from "@medusajs/types"
|
||||
import Scrypt from "scrypt-kdf"
|
||||
|
||||
class UsernamePasswordProvider extends AbstractAuthenticationModuleProvider {
|
||||
public static PROVIDER = "usernamePassword"
|
||||
public static DISPLAY_NAME = "Username/Password Authentication"
|
||||
class EmailPasswordProvider extends AbstractAuthModuleProvider {
|
||||
public static PROVIDER = "emailpass"
|
||||
public static DISPLAY_NAME = "Email/Password Authentication"
|
||||
|
||||
protected readonly authUserSerivce_: AuthUserService
|
||||
|
||||
@@ -37,7 +37,7 @@ class UsernamePasswordProvider extends AbstractAuthenticationModuleProvider {
|
||||
|
||||
const authUser = await this.authUserSerivce_.retrieveByProviderAndEntityId(
|
||||
email,
|
||||
UsernamePasswordProvider.PROVIDER
|
||||
EmailPasswordProvider.PROVIDER
|
||||
)
|
||||
|
||||
const password_hash = authUser.provider_metadata?.password
|
||||
@@ -61,4 +61,4 @@ class UsernamePasswordProvider extends AbstractAuthenticationModuleProvider {
|
||||
}
|
||||
}
|
||||
|
||||
export default UsernamePasswordProvider
|
||||
export default EmailPasswordProvider
|
||||
@@ -1,14 +1,14 @@
|
||||
import {
|
||||
AbstractAuthenticationModuleProvider,
|
||||
AbstractAuthModuleProvider,
|
||||
MedusaError,
|
||||
} from "@medusajs/utils"
|
||||
import { AuthProviderService, AuthUserService } from "@services"
|
||||
import jwt, { JwtPayload } from "jsonwebtoken"
|
||||
import {
|
||||
AuthProviderScope,
|
||||
AuthenticationInput,
|
||||
AuthenticationResponse,
|
||||
AuthProviderScope,
|
||||
} from "@medusajs/types"
|
||||
import { AuthProviderService, AuthUserService } from "@services"
|
||||
import jwt, { JwtPayload } from "jsonwebtoken"
|
||||
import { AuthorizationCode } from "simple-oauth2"
|
||||
import url from "url"
|
||||
|
||||
@@ -23,7 +23,7 @@ type ProviderConfig = {
|
||||
callbackURL: string
|
||||
}
|
||||
|
||||
class GoogleProvider extends AbstractAuthenticationModuleProvider {
|
||||
class GoogleProvider extends AbstractAuthModuleProvider {
|
||||
public static PROVIDER = "google"
|
||||
public static DISPLAY_NAME = "Google Authentication"
|
||||
|
||||
2
packages/auth/src/providers/index.ts
Normal file
2
packages/auth/src/providers/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export { default as EmailPasswordProvider } from "./email-password"
|
||||
export { default as GoogleProvider } from "./google"
|
||||
@@ -11,7 +11,7 @@ export default (async () => {
|
||||
config()
|
||||
if (!path) {
|
||||
throw new Error(
|
||||
`filePath is required.${EOL}Example: medusa-authentication-seed <filePath>`
|
||||
`filePath is required.${EOL}Example: medusa-auth-seed <filePath>`
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { LoaderOptions, Logger, ModulesSdkTypes } from "@medusajs/types"
|
||||
import * as AuthModels from "@models"
|
||||
|
||||
import { DALUtils, ModulesSdkUtils } from "@medusajs/utils"
|
||||
import { EntitySchema } from "@mikro-orm/core"
|
||||
import * as AuthenticationModels from "@models"
|
||||
import { LoaderOptions, Logger, ModulesSdkTypes } from "@medusajs/types"
|
||||
|
||||
import { EOL } from "os"
|
||||
import { EntitySchema } from "@mikro-orm/core"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { resolve } from "path"
|
||||
|
||||
export async function run({
|
||||
@@ -32,11 +34,11 @@ export async function run({
|
||||
})
|
||||
|
||||
const dbData = ModulesSdkUtils.loadDatabaseConfig(
|
||||
Modules.AUTHENTICATION,
|
||||
Modules.AUTH,
|
||||
options
|
||||
)!
|
||||
const entities = Object.values(
|
||||
AuthenticationModels
|
||||
AuthModels
|
||||
) as unknown as EntitySchema[]
|
||||
const pathToMigrations = __dirname + "/../migrations"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import {
|
||||
AuthenticationInput,
|
||||
AuthenticationResponse,
|
||||
AuthenticationTypes,
|
||||
AuthTypes,
|
||||
Context,
|
||||
DAL,
|
||||
FindConfig,
|
||||
@@ -16,7 +16,7 @@ import { joinerConfig } from "../joiner-config"
|
||||
import { AuthProviderService, AuthUserService } from "@services"
|
||||
|
||||
import {
|
||||
AbstractAuthenticationModuleProvider,
|
||||
AbstractAuthModuleProvider,
|
||||
InjectManager,
|
||||
InjectTransactionManager,
|
||||
MedusaContext,
|
||||
@@ -30,7 +30,7 @@ import {
|
||||
FilterableAuthProviderProps,
|
||||
FilterableAuthUserProps,
|
||||
UpdateAuthUserDTO,
|
||||
} from "@medusajs/types/dist/authentication/common"
|
||||
} from "@medusajs/types"
|
||||
import { ServiceTypes } from "@types"
|
||||
|
||||
type InjectedDependencies = {
|
||||
@@ -39,10 +39,10 @@ type InjectedDependencies = {
|
||||
authProviderService: AuthProviderService<any>
|
||||
}
|
||||
|
||||
export default class AuthenticationModuleService<
|
||||
export default class AuthModuleService<
|
||||
TAuthUser extends AuthUser = AuthUser,
|
||||
TAuthProvider extends AuthProvider = AuthProvider
|
||||
> implements AuthenticationTypes.IAuthenticationModuleService
|
||||
> implements AuthTypes.IAuthModuleService
|
||||
{
|
||||
__joinerConfig(): ModuleJoinerConfig {
|
||||
return joinerConfig
|
||||
@@ -83,7 +83,7 @@ export default class AuthenticationModuleService<
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return await this.baseRepository_.serialize<AuthenticationTypes.AuthProviderDTO>(
|
||||
return await this.baseRepository_.serialize<AuthTypes.AuthProviderDTO>(
|
||||
authProvider,
|
||||
{ populate: true }
|
||||
)
|
||||
@@ -101,7 +101,7 @@ export default class AuthenticationModuleService<
|
||||
)
|
||||
|
||||
return await this.baseRepository_.serialize<
|
||||
AuthenticationTypes.AuthProviderDTO[]
|
||||
AuthTypes.AuthProviderDTO[]
|
||||
>(authProviders, { populate: true })
|
||||
}
|
||||
|
||||
@@ -110,7 +110,7 @@ export default class AuthenticationModuleService<
|
||||
filters: FilterableAuthProviderProps = {},
|
||||
config: FindConfig<AuthProviderDTO>,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<[AuthenticationTypes.AuthProviderDTO[], number]> {
|
||||
): Promise<[AuthTypes.AuthProviderDTO[], number]> {
|
||||
const [authProviders, count] = await this.authProviderService_.listAndCount(
|
||||
filters,
|
||||
config,
|
||||
@@ -119,7 +119,7 @@ export default class AuthenticationModuleService<
|
||||
|
||||
return [
|
||||
await this.baseRepository_.serialize<
|
||||
AuthenticationTypes.AuthProviderDTO[]
|
||||
AuthTypes.AuthProviderDTO[]
|
||||
>(authProviders, { populate: true }),
|
||||
count,
|
||||
]
|
||||
@@ -140,14 +140,14 @@ export default class AuthenticationModuleService<
|
||||
data: CreateAuthProviderDTO | CreateAuthProviderDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<
|
||||
AuthenticationTypes.AuthProviderDTO | AuthenticationTypes.AuthProviderDTO[]
|
||||
AuthTypes.AuthProviderDTO | AuthTypes.AuthProviderDTO[]
|
||||
> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
|
||||
const providers = await this.createAuthProviders_(input, sharedContext)
|
||||
|
||||
const serializedProviders = await this.baseRepository_.serialize<
|
||||
AuthenticationTypes.AuthProviderDTO[]
|
||||
AuthTypes.AuthProviderDTO[]
|
||||
>(providers, {
|
||||
populate: true,
|
||||
})
|
||||
@@ -164,29 +164,29 @@ export default class AuthenticationModuleService<
|
||||
}
|
||||
|
||||
updateAuthProvider(
|
||||
data: AuthenticationTypes.UpdateAuthProviderDTO[],
|
||||
data: AuthTypes.UpdateAuthProviderDTO[],
|
||||
sharedContext?: Context
|
||||
): Promise<AuthProviderDTO[]>
|
||||
updateAuthProvider(
|
||||
data: AuthenticationTypes.UpdateAuthProviderDTO,
|
||||
data: AuthTypes.UpdateAuthProviderDTO,
|
||||
sharedContext?: Context
|
||||
): Promise<AuthProviderDTO>
|
||||
|
||||
@InjectManager("baseRepository_")
|
||||
async updateAuthProvider(
|
||||
data:
|
||||
| AuthenticationTypes.UpdateAuthProviderDTO[]
|
||||
| AuthenticationTypes.UpdateAuthProviderDTO,
|
||||
| AuthTypes.UpdateAuthProviderDTO[]
|
||||
| AuthTypes.UpdateAuthProviderDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<
|
||||
AuthenticationTypes.AuthProviderDTO | AuthenticationTypes.AuthProviderDTO[]
|
||||
AuthTypes.AuthProviderDTO | AuthTypes.AuthProviderDTO[]
|
||||
> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
|
||||
const providers = await this.updateAuthProvider_(input, sharedContext)
|
||||
|
||||
const serializedProviders = await this.baseRepository_.serialize<
|
||||
AuthenticationTypes.AuthProviderDTO[]
|
||||
AuthTypes.AuthProviderDTO[]
|
||||
>(providers, {
|
||||
populate: true,
|
||||
})
|
||||
@@ -195,7 +195,7 @@ export default class AuthenticationModuleService<
|
||||
}
|
||||
|
||||
async updateAuthProvider_(
|
||||
data: AuthenticationTypes.UpdateAuthProviderDTO[],
|
||||
data: AuthTypes.UpdateAuthProviderDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<TAuthProvider[]> {
|
||||
return await this.authProviderService_.update(data, sharedContext)
|
||||
@@ -221,7 +221,7 @@ export default class AuthenticationModuleService<
|
||||
sharedContext
|
||||
)
|
||||
|
||||
return await this.baseRepository_.serialize<AuthenticationTypes.AuthUserDTO>(
|
||||
return await this.baseRepository_.serialize<AuthTypes.AuthUserDTO>(
|
||||
authUser,
|
||||
{
|
||||
exclude: ["password_hash"],
|
||||
@@ -242,7 +242,7 @@ export default class AuthenticationModuleService<
|
||||
)
|
||||
|
||||
return await this.baseRepository_.serialize<
|
||||
AuthenticationTypes.AuthUserDTO[]
|
||||
AuthTypes.AuthUserDTO[]
|
||||
>(authUsers, {
|
||||
populate: true,
|
||||
})
|
||||
@@ -261,7 +261,7 @@ export default class AuthenticationModuleService<
|
||||
)
|
||||
|
||||
return [
|
||||
await this.baseRepository_.serialize<AuthenticationTypes.AuthUserDTO[]>(
|
||||
await this.baseRepository_.serialize<AuthTypes.AuthUserDTO[]>(
|
||||
authUsers,
|
||||
{
|
||||
populate: true,
|
||||
@@ -285,14 +285,14 @@ export default class AuthenticationModuleService<
|
||||
data: CreateAuthUserDTO[] | CreateAuthUserDTO,
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<
|
||||
AuthenticationTypes.AuthUserDTO | AuthenticationTypes.AuthUserDTO[]
|
||||
AuthTypes.AuthUserDTO | AuthTypes.AuthUserDTO[]
|
||||
> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
|
||||
const authUsers = await this.createAuthUsers_(input, sharedContext)
|
||||
|
||||
const serializedUsers = await this.baseRepository_.serialize<
|
||||
AuthenticationTypes.AuthUserDTO[]
|
||||
AuthTypes.AuthUserDTO[]
|
||||
>(authUsers, {
|
||||
populate: true,
|
||||
})
|
||||
@@ -322,14 +322,14 @@ export default class AuthenticationModuleService<
|
||||
data: UpdateAuthUserDTO | UpdateAuthUserDTO[],
|
||||
@MedusaContext() sharedContext: Context = {}
|
||||
): Promise<
|
||||
AuthenticationTypes.AuthUserDTO | AuthenticationTypes.AuthUserDTO[]
|
||||
AuthTypes.AuthUserDTO | AuthTypes.AuthUserDTO[]
|
||||
> {
|
||||
const input = Array.isArray(data) ? data : [data]
|
||||
|
||||
const updatedUsers = await this.updateAuthUsers_(input, sharedContext)
|
||||
|
||||
const serializedUsers = await this.baseRepository_.serialize<
|
||||
AuthenticationTypes.AuthUserDTO[]
|
||||
AuthTypes.AuthUserDTO[]
|
||||
>(updatedUsers, {
|
||||
populate: true,
|
||||
})
|
||||
@@ -356,8 +356,8 @@ export default class AuthenticationModuleService<
|
||||
protected getRegisteredAuthenticationProvider(
|
||||
provider: string,
|
||||
{ scope }: AuthenticationInput
|
||||
): AbstractAuthenticationModuleProvider {
|
||||
let containerProvider: AbstractAuthenticationModuleProvider
|
||||
): AbstractAuthModuleProvider {
|
||||
let containerProvider: AbstractAuthModuleProvider
|
||||
try {
|
||||
containerProvider = this.__container__[`auth_provider_${provider}`]
|
||||
} catch (error) {
|
||||
@@ -1,4 +1,4 @@
|
||||
import { AuthenticationTypes, Context, DAL, FindConfig } from "@medusajs/types"
|
||||
import { AuthTypes, Context, DAL, FindConfig } from "@medusajs/types"
|
||||
import {
|
||||
InjectManager,
|
||||
MedusaContext,
|
||||
@@ -29,7 +29,7 @@ export default class AuthUserService<
|
||||
|
||||
@InjectManager("authUserRepository_")
|
||||
async retrieveByProviderAndEntityId<
|
||||
TEntityMethod = AuthenticationTypes.AuthUserDTO
|
||||
TEntityMethod = AuthTypes.AuthUserDTO
|
||||
>(
|
||||
entityId: string,
|
||||
provider: string,
|
||||
@@ -1,3 +1,3 @@
|
||||
export { default as AuthenticationModuleService } from "./authentication-module"
|
||||
export { default as AuthModuleService } from "./auth-module"
|
||||
export { default as AuthProviderService } from "./auth-provider"
|
||||
export { default as AuthUserService } from "./auth-user"
|
||||
@@ -1,3 +0,0 @@
|
||||
# Authentication Module
|
||||
|
||||
The Authentication Module is Medusa’s authentication engine engine. It provides functions to authenticate users through identity providers and store metadata about users that can be used for authorization purposes.
|
||||
@@ -1,6 +0,0 @@
|
||||
if (typeof process.env.DB_TEMP_NAME === "undefined") {
|
||||
const tempName = parseInt(process.env.JEST_WORKER_ID || "1")
|
||||
process.env.DB_TEMP_NAME = `medusa-authentication-integration-${tempName}`
|
||||
}
|
||||
|
||||
process.env.MEDUSA_AUTHENTICATION_DB_SCHEMA = "public"
|
||||
@@ -1,2 +0,0 @@
|
||||
export { default as UsernamePasswordProvider } from "./username-password"
|
||||
export { default as GoogleProvider } from "./google"
|
||||
@@ -15,7 +15,7 @@ export enum Modules {
|
||||
PRODUCT = "productService",
|
||||
PRICING = "pricingService",
|
||||
PROMOTION = "promotion",
|
||||
AUTHENTICATION = "authentication",
|
||||
AUTH = "auth",
|
||||
WORKFLOW_ENGINE = "workflows",
|
||||
SALES_CHANNEL = "salesChannel",
|
||||
CART = "cart",
|
||||
@@ -31,7 +31,7 @@ export enum ModuleRegistrationName {
|
||||
PRODUCT = "productModuleService",
|
||||
PRICING = "pricingModuleService",
|
||||
PROMOTION = "promotionModuleService",
|
||||
AUTHENTICATION = "authenticationModuleService",
|
||||
AUTH = "authModuleService",
|
||||
WORKFLOW_ENGINE = "workflowsModuleService",
|
||||
SALES_CHANNEL = "salesChannelModuleService",
|
||||
CART = "cartModuleService",
|
||||
@@ -48,7 +48,7 @@ export const MODULE_PACKAGE_NAMES = {
|
||||
[Modules.CACHE]: "@medusajs/cache-inmemory",
|
||||
[Modules.PRICING]: "@medusajs/pricing",
|
||||
[Modules.PROMOTION]: "@medusajs/promotion",
|
||||
[Modules.AUTHENTICATION]: "@medusajs/authentication",
|
||||
[Modules.AUTH]: "@medusajs/auth",
|
||||
[Modules.WORKFLOW_ENGINE]: "@medusajs/workflow-engine-inmemory",
|
||||
[Modules.SALES_CHANNEL]: "@medusajs/sales-channel",
|
||||
[Modules.CART]: "@medusajs/cart",
|
||||
@@ -157,11 +157,11 @@ export const ModulesDefinition: { [key: string | Modules]: ModuleDefinition } =
|
||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||
},
|
||||
},
|
||||
[Modules.AUTHENTICATION]: {
|
||||
key: Modules.AUTHENTICATION,
|
||||
registrationName: ModuleRegistrationName.AUTHENTICATION,
|
||||
[Modules.AUTH]: {
|
||||
key: Modules.AUTH,
|
||||
registrationName: ModuleRegistrationName.AUTH,
|
||||
defaultPackage: false,
|
||||
label: upperCaseFirst(ModuleRegistrationName.AUTHENTICATION),
|
||||
label: upperCaseFirst(ModuleRegistrationName.AUTH),
|
||||
isRequired: false,
|
||||
canOverride: true,
|
||||
isQueryable: true,
|
||||
|
||||
22
packages/types/src/auth/common/provider.ts
Normal file
22
packages/types/src/auth/common/provider.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
export type AuthenticationResponse = {
|
||||
success: boolean
|
||||
authUser?: any
|
||||
error?: string
|
||||
location?: string
|
||||
}
|
||||
|
||||
export type AuthModuleProviderConfig = {
|
||||
name: string
|
||||
scopes: Record<string, AuthProviderScope>
|
||||
}
|
||||
|
||||
export type AuthProviderScope = { domain?: string } & Record<string, string>
|
||||
|
||||
export type AuthenticationInput = {
|
||||
connection: { encrypted: boolean }
|
||||
url: string
|
||||
headers: Record<string, string>
|
||||
query: Record<string, string>
|
||||
body: Record<string, string>
|
||||
scope: string
|
||||
}
|
||||
@@ -15,7 +15,7 @@ import { Context } from "../shared-context"
|
||||
import { FindConfig } from "../common"
|
||||
import { IModuleService } from "../modules-sdk"
|
||||
|
||||
export interface IAuthenticationModuleService extends IModuleService {
|
||||
export interface IAuthModuleService extends IModuleService {
|
||||
authenticate(
|
||||
provider: string,
|
||||
providerData: AuthenticationInput
|
||||
@@ -1,4 +1,4 @@
|
||||
export * as AuthenticationTypes from "./authentication"
|
||||
export * as AuthTypes from "./auth"
|
||||
export * as CacheTypes from "./cache"
|
||||
export * as CartTypes from "./cart"
|
||||
export * as CommonTypes from "./common"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export * from "./address"
|
||||
export * from "./authentication"
|
||||
export * from "./auth"
|
||||
export * from "./bundles"
|
||||
export * from "./cache"
|
||||
export * from "./cart"
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { AuthenticationResponse, AuthProviderScope } from "@medusajs/types"
|
||||
import { AuthProviderScope, AuthenticationResponse } from "@medusajs/types"
|
||||
|
||||
import { MedusaError } from "../common"
|
||||
|
||||
export abstract class AbstractAuthenticationModuleProvider {
|
||||
export abstract class AbstractAuthModuleProvider {
|
||||
public static PROVIDER: string
|
||||
public static DISPLAY_NAME: string
|
||||
protected readonly scopes_: Record<string, AuthProviderScope>
|
||||
1
packages/utils/src/auth/index.ts
Normal file
1
packages/utils/src/auth/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./abstract-auth-provider"
|
||||
@@ -1 +0,0 @@
|
||||
export * from "./abstract-authentication-provider"
|
||||
@@ -1,4 +1,4 @@
|
||||
export * from "./authentication"
|
||||
export * from "./auth"
|
||||
export * from "./bundles"
|
||||
export * from "./common"
|
||||
export * from "./dal"
|
||||
|
||||
@@ -7889,9 +7889,9 @@ __metadata:
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
"@medusajs/authentication@workspace:packages/authentication":
|
||||
"@medusajs/auth@workspace:packages/auth":
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@medusajs/authentication@workspace:packages/authentication"
|
||||
resolution: "@medusajs/auth@workspace:packages/auth"
|
||||
dependencies:
|
||||
"@medusajs/modules-sdk": ^1.12.7
|
||||
"@medusajs/types": ^1.11.11
|
||||
@@ -7915,7 +7915,7 @@ __metadata:
|
||||
tsc-alias: ^1.8.6
|
||||
typescript: ^5.1.6
|
||||
bin:
|
||||
medusa-authentication-seed: dist/scripts/bin/run-seed.js
|
||||
medusa-auth-seed: dist/scripts/bin/run-seed.js
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
|
||||
Reference in New Issue
Block a user