|
|
|
@ -58,14 +58,14 @@ export class UserService { |
|
|
|
this.#statements = createPreparedStatement(db); |
|
|
|
} |
|
|
|
|
|
|
|
#createTotp(user: Pick<UserType, 'username' | 'totpKey'>) { |
|
|
|
#createTotp(user: { username: string; totpKey: string }) { |
|
|
|
return new TOTP({ |
|
|
|
issuer: 'wg-easy', |
|
|
|
label: user.username, |
|
|
|
algorithm: 'SHA1', |
|
|
|
digits: 6, |
|
|
|
period: 30, |
|
|
|
secret: user.totpKey!, |
|
|
|
secret: user.totpKey, |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
@ -167,11 +167,12 @@ export class UserService { |
|
|
|
if (!code) { |
|
|
|
return { success: false, error: 'TOTP_REQUIRED' }; |
|
|
|
} else { |
|
|
|
if (!txUser.totpKey) { |
|
|
|
const totpKey = txUser.totpKey; |
|
|
|
if (!totpKey) { |
|
|
|
return { success: false, error: 'UNEXPECTED_ERROR' }; |
|
|
|
} |
|
|
|
|
|
|
|
const totp = this.#createTotp(txUser); |
|
|
|
const totp = this.#createTotp({ username: txUser.username, totpKey }); |
|
|
|
if (totp.validate({ token: code, window: 1 }) === null) { |
|
|
|
return { success: false, error: 'INVALID_TOTP_CODE' }; |
|
|
|
} |
|
|
|
@ -196,11 +197,12 @@ export class UserService { |
|
|
|
throw new Error('User not found'); |
|
|
|
} |
|
|
|
|
|
|
|
if (!txUser.totpKey) { |
|
|
|
const totpKey = txUser.totpKey; |
|
|
|
if (!totpKey) { |
|
|
|
throw new Error('TOTP key is not set'); |
|
|
|
} |
|
|
|
|
|
|
|
const totp = this.#createTotp(txUser); |
|
|
|
const totp = this.#createTotp({ username: txUser.username, totpKey }); |
|
|
|
if (totp.validate({ token: code, window: 1 }) === null) { |
|
|
|
throw new Error('Invalid TOTP code'); |
|
|
|
} |
|
|
|
|