|
1
|
import { |
|
2
|
Context, |
|
3
|
ModulesSdkTypes, |
|
4
|
RecoveryCodeAuthMfaProvider |
|
5
|
} from "@medusajs/framework/types" |
|
6
|
import { AuthMfaRecoveryCode } from "@models" |
|
7
|
import { |
|
8
|
generateRecoveryCode, |
|
9
|
hashRecoveryCode, |
|
10
|
verifyRecoveryCodeHash, |
|
11
|
} from "../../utils/mfa" |
|
12
|
|
|
13
|
type InjectedDependencies = { |
|
14
|
authMfaRecoveryCodeService: ModulesSdkTypes.IMedusaInternalService< |
|
15
|
typeof AuthMfaRecoveryCode |
|
16
|
> |
|
17
|
} |
|
18
|
|
|
19
|
export class RecoveryCodeMfaProvider implements RecoveryCodeAuthMfaProvider { |
|
20
|
static identifier = "recovery_code" |
|
21
|
|
|
22
|
readonly method = RecoveryCodeMfaProvider.identifier as "recovery_code" |
|
23
|
|
|
24
|
protected authMfaRecoveryCodeService_: ModulesSdkTypes.IMedusaInternalService< |
|
25
|
typeof AuthMfaRecoveryCode |
|
26
|
> |
|
27
|
|
|
28
|
constructor({ authMfaRecoveryCodeService }: InjectedDependencies) { |
|
29
|
this.authMfaRecoveryCodeService_ = authMfaRecoveryCodeService |
|
30
|
} |
|
31
|
|
|
32
|
async canVerifyForAuthIdentity( |
|
33
|
data: { auth_identity_id: string }, |
|
34
|
sharedContext: Context = {} |
|
35
|
): Promise<boolean> { |
|
36
|
const [code] = await this.authMfaRecoveryCodeService_.list( |
|
37
|
{ |
|
38
|
auth_identity_id: data.auth_identity_id, |
|
39
|
}, |
|
40
|
{ select: ["id"] }, |
|
41
|
sharedContext |
|
42
|
) |
|
43
|
|
|
44
|
return !!code |
|
45
|
} |
|
46
|
|
|
47
|
async generateCodes( |
|
48
|
data: { auth_identity_id: string; count: number }, |
|
49
|
sharedContext: Context = {} |
|
50
|
): Promise<string[]> { |
|
51
|
const existingCodes = await this.authMfaRecoveryCodeService_.list( |
|
52
|
{ auth_identity_id: data.auth_identity_id }, |
|
53
|
{ select: ["id"] }, |
|
54
|
sharedContext |
|
55
|
) |
|
56
|
|
|
57
|
if (existingCodes.length) { |
|
58
|
await this.authMfaRecoveryCodeService_.delete( |
|
59
|
existingCodes.map((code) => code.id), |
|
60
|
sharedContext |
|
61
|
) |
|
62
|
} |
|
63
|
|
|
64
|
const codes = Array.from({ length: data.count }, () => |
|
65
|
generateRecoveryCode() |
|
66
|
) |
|
67
|
|
|
68
|
await this.authMfaRecoveryCodeService_.create( |
|
69
|
await Promise.all( |
|
70
|
codes.map(async (code) => ({ |
|
71
|
auth_identity_id: data.auth_identity_id, |
|
72
|
code_hash: await hashRecoveryCode(code), |
|
73
|
})) |
|
74
|
), |
|
75
|
sharedContext |
|
76
|
) |
|
77
|
|
|
78
|
return codes |
|
79
|
} |
|
80
|
|
|
81
|
async verify( |
|
82
|
data: { auth_identity_id: string; code: string }, |
|
83
|
sharedContext: Context = {} |
|
84
|
): Promise<boolean> { |
|
85
|
const recoveryCodes = await this.authMfaRecoveryCodeService_.list( |
|
86
|
{ |
|
87
|
auth_identity_id: data.auth_identity_id, |
|
88
|
}, |
|
89
|
{ select: ["id", "code_hash"] }, |
|
90
|
sharedContext |
|
91
|
) |
|
92
|
|
|
93
|
let recoveryCode: (typeof recoveryCodes)[number] | undefined |
|
94
|
|
|
95
|
for (const candidate of recoveryCodes) { |
|
96
|
if (await verifyRecoveryCodeHash(candidate.code_hash, data.code)) { |
|
97
|
recoveryCode = candidate |
|
98
|
break |
|
99
|
} |
|
100
|
} |
|
101
|
|
|
102
|
if (!recoveryCode) { |
|
103
|
return false |
|
104
|
} |
|
105
|
|
|
106
|
await this.authMfaRecoveryCodeService_.delete(recoveryCode.id, sharedContext) |
|
107
|
|
|
108
|
return true |
|
109
|
} |
|
110
|
} |