feat(authentication, types, utils): Add Authentication provider scopes (#6228)

* initial implementation

* add test for invalid scope

* get config from scope not db

* assign config from scope

* fix package.json

* optional providers

* make providers options

* update type
This commit is contained in:
Philip Korsholm
2024-01-29 17:42:42 +08:00
committed by GitHub
parent d1c18a3090
commit a41aad4bea
10 changed files with 150 additions and 49 deletions
@@ -1,8 +1,10 @@
import { AuthenticationResponse } from "@medusajs/types"
import { AuthenticationResponse, AuthProviderScope } from "@medusajs/types"
import { MedusaError } from "../common"
export abstract class AbstractAuthenticationModuleProvider {
public static PROVIDER: string
public static DISPLAY_NAME: string
protected readonly scopes_: Record<string, AuthProviderScope>
public get provider() {
return (this.constructor as Function & { PROVIDER: string }).PROVIDER
@@ -13,6 +15,19 @@ export abstract class AbstractAuthenticationModuleProvider {
.DISPLAY_NAME
}
protected constructor({ scopes }) {
this.scopes_ = scopes
}
public validateScope(scope) {
if (!this.scopes_[scope]) {
throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT,
`Scope "${scope}" is not valid for provider ${this.provider}`
)
}
}
abstract authenticate(
data: Record<string, unknown>
): Promise<AuthenticationResponse>