@crombolo/server 0.17.0 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/two-factor.ts CHANGED
@@ -25,7 +25,11 @@ export const MEMBER_TWO_FACTOR_PROVIDER = 'UmbracoMemberAppAuthenticator'
25
25
 
26
26
  export type TwoFactorResult<T = undefined> =
27
27
  | ({ ok: true } & (T extends undefined ? object : { value: T }))
28
- | { ok: false; reason: 'unavailable' | 'invalidCode' | 'notEnabled'; message: string }
28
+ | {
29
+ ok: false
30
+ reason: 'unavailable' | 'invalidCode' | 'notEnabled' | 'alreadyEnabled'
31
+ message: string
32
+ }
29
33
 
30
34
  export class TwoFactorService {
31
35
  #repository: TwoFactorRepository
@@ -69,7 +73,12 @@ export class TwoFactorService {
69
73
  return { ok: true, value: { secret, qrCodeSetupImageUrl: qrCodeDataUrl(uri), otpauthUri: uri } }
70
74
  }
71
75
 
72
- /** Turns the provider on when `code` is right for `secret`; answers with the recovery codes, shown once. */
76
+ /**
77
+ * Turns the provider on when `code` is right for `secret`; answers with the
78
+ * recovery codes, shown once. Refused while it is already on: replacing the
79
+ * authenticator means turning it off first, which asks for the current one, so
80
+ * a stolen session cannot swap in its own.
81
+ */
73
82
  async enable(
74
83
  ownerKey: string,
75
84
  provider: string,
@@ -77,6 +86,12 @@ export class TwoFactorService {
77
86
  code: string,
78
87
  ): Promise<TwoFactorResult<{ recoveryCodes: string[] }>> {
79
88
  if (!this.#key) return this.#unavailable()
89
+ if (await this.#repository.get(ownerKey, provider))
90
+ return {
91
+ ok: false,
92
+ reason: 'alreadyEnabled',
93
+ message: 'Two-factor sign-in is already on. Turn it off first to use another app.',
94
+ }
80
95
  const step = verifyTotp(secret, code)
81
96
  if (step === undefined)
82
97
  return {