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-10-06 22:09:42 +00:00
|
|
|
import U2FSignPost = require("../../../../../src/lib/routes/secondfactor/u2f/sign/post");
|
2017-10-17 21:24:02 +00:00
|
|
|
import { ServerVariables } from "../../../../../src/lib/ServerVariables";
|
2017-05-25 13:09:29 +00:00
|
|
|
import winston = require("winston");
|
|
|
|
|
2017-10-17 21:24:02 +00:00
|
|
|
import { ServerVariablesMockBuilder, ServerVariablesMock } from "../../../../mocks/ServerVariablesMockBuilder";
|
2017-05-25 13:09:29 +00:00
|
|
|
import ExpressMock = require("../../../../mocks/express");
|
|
|
|
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;
|
2017-10-17 21:24:02 +00:00
|
|
|
let mocks: ServerVariablesMock;
|
|
|
|
let vars: ServerVariables;
|
2017-05-25 13:09:29 +00:00
|
|
|
|
2017-07-19 19:06:12 +00:00
|
|
|
beforeEach(function () {
|
|
|
|
req = ExpressMock.RequestMock();
|
|
|
|
req.app = {};
|
2017-11-01 13:24:18 +00:00
|
|
|
req.originalUrl = "/api/xxxx";
|
2017-05-25 13:09:29 +00:00
|
|
|
|
2017-10-17 21:24:02 +00:00
|
|
|
const s = ServerVariablesMockBuilder.build();
|
|
|
|
mocks = s.mocks;
|
|
|
|
vars = s.variables;
|
|
|
|
|
|
|
|
req.session = {
|
|
|
|
auth: {
|
|
|
|
userid: "user",
|
|
|
|
first_factor: true,
|
|
|
|
second_factor: false,
|
|
|
|
identity_check: {
|
|
|
|
challenge: "u2f-register",
|
|
|
|
userid: "user"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-07-19 19:06:12 +00:00
|
|
|
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"
|
|
|
|
};
|
2017-10-17 21:24:02 +00:00
|
|
|
mocks.u2f.checkSignatureStub.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-10-17 21:24:02 +00:00
|
|
|
req.session.auth.sign_request = {
|
2017-07-19 19:06:12 +00:00
|
|
|
appId: "app",
|
|
|
|
challenge: "challenge",
|
|
|
|
keyHandle: "key",
|
|
|
|
version: "U2F_V2"
|
|
|
|
};
|
2017-10-17 21:24:02 +00:00
|
|
|
return U2FSignPost.default(vars)(req as any, res as any)
|
2017-07-19 19:06:12 +00:00
|
|
|
.then(function () {
|
2017-10-17 21:24:02 +00:00
|
|
|
Assert(req.session.auth.second_factor);
|
2017-07-19 19:06:12 +00:00
|
|
|
});
|
|
|
|
});
|
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-10-17 21:24:02 +00:00
|
|
|
mocks.u2f.checkSignatureStub.returns({ errorCode: 500 });
|
2017-05-25 13:09:29 +00:00
|
|
|
|
2017-10-17 21:24:02 +00:00
|
|
|
req.session.auth.sign_request = {
|
2017-07-19 19:06:12 +00:00
|
|
|
appId: "app",
|
|
|
|
challenge: "challenge",
|
|
|
|
keyHandle: "key",
|
|
|
|
version: "U2F_V2"
|
|
|
|
};
|
2017-10-17 21:24:02 +00:00
|
|
|
return U2FSignPost.default(vars)(req as any, res as any)
|
2017-07-19 19:06:12 +00:00
|
|
|
.then(function () {
|
2017-10-10 21:03:30 +00:00
|
|
|
Assert.equal(res.status.getCall(0).args[0], 200);
|
|
|
|
Assert.deepEqual(res.send.getCall(0).args[0],
|
|
|
|
{ error: "Operation failed." });
|
2017-07-19 19:06:12 +00:00
|
|
|
});
|
|
|
|
});
|
2017-05-25 13:09:29 +00:00
|
|
|
});
|
|
|
|
|