2017-05-21 00:16:57 +07:00
|
|
|
|
2017-10-07 05:09:42 +07:00
|
|
|
import { TOTPValidator } from "../src/lib/TOTPValidator";
|
2017-10-14 20:23:00 +07:00
|
|
|
import Sinon = require("sinon");
|
|
|
|
import Speakeasy = require("speakeasy");
|
2017-05-21 00:16:57 +07:00
|
|
|
|
|
|
|
describe("test TOTP validation", function() {
|
|
|
|
let totpValidator: TOTPValidator;
|
2017-10-14 20:23:00 +07:00
|
|
|
let totpValidateStub: Sinon.SinonStub;
|
2017-05-21 00:16:57 +07:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2017-10-14 20:23:00 +07:00
|
|
|
totpValidateStub = Sinon.stub(Speakeasy.totp, "verify");
|
|
|
|
totpValidator = new TOTPValidator(Speakeasy);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
totpValidateStub.restore();
|
2017-05-21 00:16:57 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should validate the TOTP token", function() {
|
|
|
|
const totp_secret = "NBD2ZV64R9UV1O7K";
|
|
|
|
const token = "token";
|
2017-10-14 20:23:00 +07:00
|
|
|
totpValidateStub.withArgs({
|
|
|
|
secret: totp_secret,
|
|
|
|
token: token,
|
|
|
|
encoding: "base32",
|
|
|
|
window: 1
|
|
|
|
}).returns(true);
|
2017-05-21 00:16:57 +07:00
|
|
|
return totpValidator.validate(token, totp_secret);
|
|
|
|
});
|
|
|
|
|
2017-05-21 17:14:59 +07:00
|
|
|
it("should not validate a wrong TOTP token", function(done) {
|
2017-05-21 00:16:57 +07:00
|
|
|
const totp_secret = "NBD2ZV64R9UV1O7K";
|
|
|
|
const token = "wrong token";
|
2017-10-14 20:23:00 +07:00
|
|
|
totpValidateStub.returns(false);
|
2017-05-21 17:14:59 +07:00
|
|
|
totpValidator.validate(token, totp_secret)
|
2017-05-21 00:16:57 +07:00
|
|
|
.catch(function() {
|
2017-05-21 17:14:59 +07:00
|
|
|
done();
|
2017-05-21 00:16:57 +07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|