Browse Source

remove type assertion

pull/2553/head
Bernd Storath 4 weeks ago
parent
commit
cb59d63a82
  1. 14
      src/server/database/repositories/user/service.ts

14
src/server/database/repositories/user/service.ts

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

Loading…
Cancel
Save