2017-05-25 20:09:29 +07:00
|
|
|
|
2017-10-07 05:09:42 +07:00
|
|
|
import U2FValidator = require("../../src/lib/secondfactor/U2FValidator");
|
|
|
|
import { INotifier } from "../../src/lib/INotifier";
|
2017-05-25 20:09:29 +07:00
|
|
|
import JQueryMock = require("../mocks/jquery");
|
|
|
|
import U2FApiMock = require("../mocks/u2f-api");
|
2017-10-07 05:09:42 +07:00
|
|
|
import { SignMessage } from "../../../shared/SignMessage";
|
2017-05-25 20:09:29 +07:00
|
|
|
import BluebirdPromise = require("bluebird");
|
|
|
|
import Assert = require("assert");
|
2017-08-05 02:20:31 +07:00
|
|
|
import { NotifierStub } from "../mocks/NotifierStub";
|
2017-05-25 20:09:29 +07:00
|
|
|
|
|
|
|
describe("test U2F validation", function () {
|
2017-08-05 02:20:31 +07:00
|
|
|
let notifier: INotifier;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
notifier = new NotifierStub();
|
|
|
|
});
|
|
|
|
|
2017-05-25 20:09:29 +07:00
|
|
|
it("should validate the U2F device", () => {
|
|
|
|
const signatureRequest: SignMessage = {
|
|
|
|
keyHandle: "keyhandle",
|
|
|
|
request: {
|
|
|
|
version: "U2F_V2",
|
|
|
|
appId: "https://example.com",
|
|
|
|
challenge: "challenge"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const u2fClient = U2FApiMock.U2FApiMock();
|
|
|
|
u2fClient.sign.returns(BluebirdPromise.resolve());
|
|
|
|
|
|
|
|
const getPromise = JQueryMock.JQueryDeferredMock();
|
|
|
|
getPromise.done.yields(signatureRequest);
|
|
|
|
getPromise.done.returns(getPromise);
|
|
|
|
|
|
|
|
const postPromise = JQueryMock.JQueryDeferredMock();
|
2017-10-18 04:24:02 +07:00
|
|
|
postPromise.done.yields({ redirect: "https://home.test.url" });
|
2017-05-25 20:09:29 +07:00
|
|
|
postPromise.done.returns(postPromise);
|
|
|
|
|
|
|
|
const jqueryMock = JQueryMock.JQueryMock();
|
2017-08-05 02:20:31 +07:00
|
|
|
jqueryMock.jquery.get.returns(getPromise);
|
|
|
|
jqueryMock.jquery.ajax.returns(postPromise);
|
2017-05-25 20:09:29 +07:00
|
|
|
|
2017-08-05 02:20:31 +07:00
|
|
|
return U2FValidator.validate(jqueryMock.jquery as any, notifier, u2fClient as any);
|
2017-05-25 20:09:29 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should fail during initial authentication request", () => {
|
|
|
|
const u2fClient = U2FApiMock.U2FApiMock();
|
|
|
|
|
|
|
|
const getPromise = JQueryMock.JQueryDeferredMock();
|
|
|
|
getPromise.done.returns(getPromise);
|
|
|
|
getPromise.fail.yields(undefined, "Error while issuing authentication request");
|
|
|
|
|
|
|
|
const jqueryMock = JQueryMock.JQueryMock();
|
2017-08-05 02:20:31 +07:00
|
|
|
jqueryMock.jquery.get.returns(getPromise);
|
2017-05-25 20:09:29 +07:00
|
|
|
|
2017-08-05 02:20:31 +07:00
|
|
|
return U2FValidator.validate(jqueryMock.jquery as any, notifier, u2fClient as any)
|
2017-05-25 20:09:29 +07:00
|
|
|
.catch(function(err: Error) {
|
|
|
|
Assert.equal("Error while issuing authentication request", err.message);
|
|
|
|
return BluebirdPromise.resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should fail during device signature", () => {
|
|
|
|
const signatureRequest: SignMessage = {
|
|
|
|
keyHandle: "keyhandle",
|
|
|
|
request: {
|
|
|
|
version: "U2F_V2",
|
|
|
|
appId: "https://example.com",
|
|
|
|
challenge: "challenge"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const u2fClient = U2FApiMock.U2FApiMock();
|
|
|
|
u2fClient.sign.returns(BluebirdPromise.reject(new Error("Device unable to sign")));
|
|
|
|
|
|
|
|
const getPromise = JQueryMock.JQueryDeferredMock();
|
|
|
|
getPromise.done.yields(signatureRequest);
|
|
|
|
getPromise.done.returns(getPromise);
|
|
|
|
|
|
|
|
const jqueryMock = JQueryMock.JQueryMock();
|
2017-08-05 02:20:31 +07:00
|
|
|
jqueryMock.jquery.get.returns(getPromise);
|
2017-05-25 20:09:29 +07:00
|
|
|
|
2017-08-05 02:20:31 +07:00
|
|
|
return U2FValidator.validate(jqueryMock.jquery as any, notifier, u2fClient as any)
|
2017-05-25 20:09:29 +07:00
|
|
|
.catch(function(err: Error) {
|
|
|
|
Assert.equal("Device unable to sign", err.message);
|
|
|
|
return BluebirdPromise.resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should fail at the end of the authentication request", () => {
|
|
|
|
const signatureRequest: SignMessage = {
|
|
|
|
keyHandle: "keyhandle",
|
|
|
|
request: {
|
|
|
|
version: "U2F_V2",
|
|
|
|
appId: "https://example.com",
|
|
|
|
challenge: "challenge"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const u2fClient = U2FApiMock.U2FApiMock();
|
|
|
|
u2fClient.sign.returns(BluebirdPromise.resolve());
|
|
|
|
|
|
|
|
const getPromise = JQueryMock.JQueryDeferredMock();
|
|
|
|
getPromise.done.yields(signatureRequest);
|
|
|
|
getPromise.done.returns(getPromise);
|
|
|
|
|
|
|
|
const postPromise = JQueryMock.JQueryDeferredMock();
|
|
|
|
postPromise.fail.yields(undefined, "Error while finishing authentication");
|
|
|
|
postPromise.done.returns(postPromise);
|
|
|
|
|
|
|
|
const jqueryMock = JQueryMock.JQueryMock();
|
2017-08-05 02:20:31 +07:00
|
|
|
jqueryMock.jquery.get.returns(getPromise);
|
|
|
|
jqueryMock.jquery.ajax.returns(postPromise);
|
2017-05-25 20:09:29 +07:00
|
|
|
|
2017-08-05 02:20:31 +07:00
|
|
|
return U2FValidator.validate(jqueryMock.jquery as any, notifier, u2fClient as any)
|
2017-05-25 20:09:29 +07:00
|
|
|
.catch(function(err: Error) {
|
|
|
|
Assert.equal("Error while finishing authentication", err.message);
|
|
|
|
return BluebirdPromise.resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|