mirror of
https://github.com/0rangebananaspy/authelia.git
synced 2024-09-14 22:47:21 +07:00
3a88ca95b8
A window of 1 means the token is checked against current time slot T as well as at time slot T-1 and T+1. A time slot is 30 seconds by default in Authelia.
27 lines
664 B
TypeScript
27 lines
664 B
TypeScript
import Speakeasy = require("speakeasy");
|
|
import BluebirdPromise = require("bluebird");
|
|
|
|
const TOTP_ENCODING = "base32";
|
|
const WINDOW: number = 1;
|
|
|
|
export class TOTPValidator {
|
|
private speakeasy: typeof Speakeasy;
|
|
|
|
constructor(speakeasy: typeof Speakeasy) {
|
|
this.speakeasy = speakeasy;
|
|
}
|
|
|
|
validate(token: string, secret: string): BluebirdPromise<void> {
|
|
const isValid = this.speakeasy.totp.verify({
|
|
secret: secret,
|
|
encoding: TOTP_ENCODING,
|
|
token: token,
|
|
window: WINDOW
|
|
} as any);
|
|
|
|
if (isValid)
|
|
return BluebirdPromise.resolve();
|
|
else
|
|
return BluebirdPromise.reject(new Error("Wrong TOTP token."));
|
|
}
|
|
} |