2017-05-25 13:09:29 +00:00
|
|
|
|
|
|
|
import sinon = require("sinon");
|
|
|
|
import BluebirdPromise = require("bluebird");
|
|
|
|
import assert = require("assert");
|
2017-10-06 22:09:42 +00:00
|
|
|
import U2FSignRequestGet = require("../../../../../src/lib/routes/secondfactor/u2f/sign_request/get");
|
2017-05-25 13:09:29 +00:00
|
|
|
import ExpressMock = require("../../../../mocks/express");
|
2017-07-19 19:06:12 +00:00
|
|
|
import { UserDataStoreStub } from "../../../../mocks/storage/UserDataStoreStub";
|
2017-05-25 13:09:29 +00:00
|
|
|
import U2FMock = require("../../../../mocks/u2f");
|
|
|
|
import U2f = require("u2f");
|
2017-10-17 21:24:02 +00:00
|
|
|
import { ServerVariablesMock, ServerVariablesMockBuilder } from "../../../../mocks/ServerVariablesMockBuilder";
|
|
|
|
import { ServerVariables } from "../../../../../src/lib/ServerVariables";
|
2017-05-25 13:09:29 +00:00
|
|
|
|
2017-10-06 22:09:42 +00:00
|
|
|
import { SignMessage } from "../../../../../../shared/SignMessage";
|
2017-05-25 13:09:29 +00:00
|
|
|
|
|
|
|
describe("test u2f routes: sign_request", function () {
|
|
|
|
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
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
req = ExpressMock.RequestMock();
|
2017-11-01 13:24:18 +00:00
|
|
|
req.originalUrl = "/api/xxxx";
|
2017-05-25 13:09:29 +00:00
|
|
|
req.app = {};
|
2017-10-17 21:24:02 +00:00
|
|
|
req.session = {
|
|
|
|
auth: {
|
|
|
|
userid: "user",
|
|
|
|
first_factor: true,
|
|
|
|
second_factor: false,
|
|
|
|
identity_check: {
|
|
|
|
challenge: "u2f-register",
|
|
|
|
userid: "user"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2017-05-25 13:09:29 +00:00
|
|
|
req.headers = {};
|
|
|
|
req.headers.host = "localhost";
|
|
|
|
|
2017-10-17 21:24:02 +00:00
|
|
|
const s = ServerVariablesMockBuilder.build();
|
|
|
|
mocks = s.mocks;
|
|
|
|
vars = s.variables;
|
|
|
|
|
2017-05-25 13:09:29 +00:00
|
|
|
const options = {
|
|
|
|
inMemoryOnly: true
|
|
|
|
};
|
|
|
|
|
|
|
|
res = ExpressMock.ResponseMock();
|
|
|
|
res.send = sinon.spy();
|
|
|
|
res.json = sinon.spy();
|
|
|
|
res.status = sinon.spy();
|
|
|
|
});
|
|
|
|
|
2017-07-19 19:06:12 +00:00
|
|
|
it("should send back the sign request and save it in the session", function () {
|
|
|
|
const expectedRequest: U2f.RegistrationResult = {
|
|
|
|
keyHandle: "keyHandle",
|
|
|
|
publicKey: "publicKey",
|
|
|
|
certificate: "Certificate",
|
|
|
|
successful: true
|
|
|
|
};
|
2017-10-17 21:24:02 +00:00
|
|
|
mocks.u2f.requestStub.returns(expectedRequest);
|
2017-07-19 19:06:12 +00:00
|
|
|
mocks.userDataStore.retrieveU2FRegistrationStub.returns(BluebirdPromise.resolve({
|
|
|
|
registration: {
|
|
|
|
publicKey: "PUBKEY",
|
|
|
|
keyHandle: "KeyHandle"
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2017-10-17 21:24:02 +00:00
|
|
|
return U2FSignRequestGet.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.deepEqual(expectedRequest, req.session.auth.sign_request);
|
2017-07-19 19:06:12 +00:00
|
|
|
assert.deepEqual(expectedRequest, res.json.getCall(0).args[0].request);
|
|
|
|
});
|
|
|
|
});
|
2017-05-25 13:09:29 +00:00
|
|
|
});
|
|
|
|
|