authelia/src/lib/TOTPValidator.ts

23 lines
572 B
TypeScript
Raw Normal View History

import { Speakeasy } from "../types/Dependencies";
import BluebirdPromise = require("bluebird");
const TOTP_ENCODING = "base32";
export default class TOTPValidator {
private speakeasy: Speakeasy;
constructor(speakeasy: Speakeasy) {
this.speakeasy = speakeasy;
}
validate(token: string, secret: string): BluebirdPromise<void> {
const real_token = this.speakeasy.totp({
secret: secret,
encoding: TOTP_ENCODING
});
if (token == real_token) return BluebirdPromise.resolve();
return BluebirdPromise.reject("Wrong challenge");
}
}