2017-05-25 13:09:29 +00:00
|
|
|
|
|
|
|
import sinon = require("sinon");
|
|
|
|
import BluebirdPromise = require("bluebird");
|
2017-07-19 19:06:12 +00:00
|
|
|
import Assert = require("assert");
|
2017-07-16 12:55:01 +00:00
|
|
|
import U2FSignPost = require("../../../../../../../src/server/lib/routes/secondfactor/u2f/sign/post");
|
|
|
|
import AuthenticationSession = require("../../../../../../../src/server/lib/AuthenticationSession");
|
2017-07-19 19:06:12 +00:00
|
|
|
import { ServerVariablesHandler } from "../../../../../../../src/server/lib/ServerVariablesHandler";
|
2017-05-25 13:09:29 +00:00
|
|
|
import winston = require("winston");
|
|
|
|
|
|
|
|
import ExpressMock = require("../../../../mocks/express");
|
|
|
|
import ServerVariablesMock = require("../../../../mocks/ServerVariablesMock");
|
|
|
|
import U2FMock = require("../../../../mocks/u2f");
|
|
|
|
import U2f = require("u2f");
|
|
|
|
|
|
|
|
describe("test u2f routes: sign", function () {
|
2017-07-19 19:06:12 +00:00
|
|
|
let req: ExpressMock.RequestMock;
|
|
|
|
let res: ExpressMock.ResponseMock;
|
|
|
|
let authSession: AuthenticationSession.AuthenticationSession;
|
|
|
|
let mocks: ServerVariablesMock.ServerVariablesMock;
|
2017-05-25 13:09:29 +00:00
|
|
|
|
2017-07-19 19:06:12 +00:00
|
|
|
beforeEach(function () {
|
|
|
|
req = ExpressMock.RequestMock();
|
|
|
|
req.app = {};
|
2017-05-25 13:09:29 +00:00
|
|
|
|
2017-07-19 19:06:12 +00:00
|
|
|
mocks = ServerVariablesMock.mock(req.app);
|
|
|
|
mocks.logger = winston;
|
2017-05-25 13:09:29 +00:00
|
|
|
|
2017-07-19 19:06:12 +00:00
|
|
|
req.session = {};
|
|
|
|
AuthenticationSession.reset(req as any);
|
|
|
|
authSession = AuthenticationSession.get(req as any);
|
|
|
|
authSession.userid = "user";
|
|
|
|
authSession.first_factor = true;
|
|
|
|
authSession.second_factor = false;
|
|
|
|
authSession.identity_check = {
|
|
|
|
challenge: "u2f-register",
|
|
|
|
userid: "user"
|
|
|
|
};
|
|
|
|
req.headers = {};
|
|
|
|
req.headers.host = "localhost";
|
2017-05-25 13:09:29 +00:00
|
|
|
|
2017-07-19 19:06:12 +00:00
|
|
|
const options = {
|
|
|
|
inMemoryOnly: true
|
|
|
|
};
|
2017-05-25 13:09:29 +00:00
|
|
|
|
2017-07-19 19:06:12 +00:00
|
|
|
res = ExpressMock.ResponseMock();
|
|
|
|
res.send = sinon.spy();
|
|
|
|
res.json = sinon.spy();
|
|
|
|
res.status = sinon.spy();
|
|
|
|
});
|
2017-05-25 13:09:29 +00:00
|
|
|
|
2017-07-19 19:06:12 +00:00
|
|
|
it("should return status code 204", function () {
|
|
|
|
const expectedStatus = {
|
|
|
|
keyHandle: "keyHandle",
|
|
|
|
publicKey: "pbk",
|
|
|
|
certificate: "cert"
|
|
|
|
};
|
|
|
|
const u2f_mock = U2FMock.U2FMock();
|
|
|
|
u2f_mock.checkSignature.returns(expectedStatus);
|
2017-05-25 13:09:29 +00:00
|
|
|
|
2017-07-19 19:06:12 +00:00
|
|
|
mocks.userDataStore.retrieveU2FRegistrationStub.returns(BluebirdPromise.resolve({
|
|
|
|
registration: {
|
|
|
|
publicKey: "PUBKEY"
|
|
|
|
}
|
|
|
|
}));
|
2017-05-25 13:09:29 +00:00
|
|
|
|
2017-07-19 19:06:12 +00:00
|
|
|
authSession.sign_request = {
|
|
|
|
appId: "app",
|
|
|
|
challenge: "challenge",
|
|
|
|
keyHandle: "key",
|
|
|
|
version: "U2F_V2"
|
|
|
|
};
|
|
|
|
mocks.u2f = u2f_mock;
|
|
|
|
return U2FSignPost.default(req as any, res as any)
|
|
|
|
.then(function () {
|
|
|
|
Assert(authSession.second_factor);
|
|
|
|
});
|
|
|
|
});
|
2017-05-25 13:09:29 +00:00
|
|
|
|
2017-07-19 19:06:12 +00:00
|
|
|
it("should return unauthorized error on registration request internal error", function () {
|
|
|
|
mocks.userDataStore.retrieveU2FRegistrationStub.returns(BluebirdPromise.resolve({
|
|
|
|
registration: {
|
|
|
|
publicKey: "PUBKEY"
|
|
|
|
}
|
|
|
|
}));
|
2017-05-25 13:09:29 +00:00
|
|
|
|
2017-07-19 19:06:12 +00:00
|
|
|
const u2f_mock = U2FMock.U2FMock();
|
|
|
|
u2f_mock.checkSignature.returns({ errorCode: 500 });
|
2017-05-25 13:09:29 +00:00
|
|
|
|
2017-07-19 19:06:12 +00:00
|
|
|
authSession.sign_request = {
|
|
|
|
appId: "app",
|
|
|
|
challenge: "challenge",
|
|
|
|
keyHandle: "key",
|
|
|
|
version: "U2F_V2"
|
|
|
|
};
|
|
|
|
mocks.u2f = u2f_mock;
|
|
|
|
return U2FSignPost.default(req as any, res as any)
|
|
|
|
.then(function () {
|
|
|
|
Assert.equal(500, res.status.getCall(0).args[0]);
|
|
|
|
});
|
|
|
|
});
|
2017-05-25 13:09:29 +00:00
|
|
|
});
|
|
|
|
|