2017-05-21 00:16:57 +07:00
|
|
|
|
|
|
|
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();
|
2017-05-21 17:14:59 +07:00
|
|
|
return BluebirdPromise.reject(new Error("Wrong challenge"));
|
2017-05-21 00:16:57 +07:00
|
|
|
}
|
|
|
|
}
|