2017-05-25 20:09:29 +07:00
|
|
|
|
|
|
|
import sinon = require("sinon");
|
|
|
|
import BluebirdPromise = require("bluebird");
|
|
|
|
import assert = require("assert");
|
2017-10-07 05:09:42 +07:00
|
|
|
import U2FSignRequestGet = require("../../../../../src/lib/routes/secondfactor/u2f/sign_request/get");
|
|
|
|
import AuthenticationSession = require("../../../../../src/lib/AuthenticationSession");
|
|
|
|
import { ServerVariablesHandler } from "../../../../../src/lib/ServerVariablesHandler";
|
2017-05-25 20:09:29 +07:00
|
|
|
import winston = require("winston");
|
|
|
|
|
|
|
|
import ExpressMock = require("../../../../mocks/express");
|
2017-07-20 02:06:12 +07:00
|
|
|
import { UserDataStoreStub } from "../../../../mocks/storage/UserDataStoreStub";
|
2017-05-25 20:09:29 +07:00
|
|
|
import ServerVariablesMock = require("../../../../mocks/ServerVariablesMock");
|
|
|
|
import U2FMock = require("../../../../mocks/u2f");
|
|
|
|
import U2f = require("u2f");
|
|
|
|
|
2017-10-07 05:09:42 +07:00
|
|
|
import { SignMessage } from "../../../../../../shared/SignMessage";
|
2017-05-25 20:09:29 +07:00
|
|
|
|
|
|
|
describe("test u2f routes: sign_request", function () {
|
|
|
|
let req: ExpressMock.RequestMock;
|
|
|
|
let res: ExpressMock.ResponseMock;
|
2017-07-20 02:06:12 +07:00
|
|
|
let mocks: ServerVariablesMock.ServerVariablesMock;
|
2017-05-25 20:09:29 +07:00
|
|
|
let authSession: AuthenticationSession.AuthenticationSession;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
req = ExpressMock.RequestMock();
|
|
|
|
req.app = {};
|
|
|
|
|
|
|
|
mocks = ServerVariablesMock.mock(req.app);
|
|
|
|
mocks.logger = winston;
|
|
|
|
|
|
|
|
req.session = {};
|
|
|
|
|
|
|
|
AuthenticationSession.reset(req as any);
|
|
|
|
|
|
|
|
req.headers = {};
|
|
|
|
req.headers.host = "localhost";
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
inMemoryOnly: true
|
|
|
|
};
|
|
|
|
|
|
|
|
res = ExpressMock.ResponseMock();
|
|
|
|
res.send = sinon.spy();
|
|
|
|
res.json = sinon.spy();
|
|
|
|
res.status = sinon.spy();
|
2017-09-22 03:07:34 +07:00
|
|
|
|
|
|
|
return AuthenticationSession.get(req as any)
|
|
|
|
.then(function (_authSession: AuthenticationSession.AuthenticationSession) {
|
|
|
|
authSession = _authSession;
|
|
|
|
authSession.userid = "user";
|
|
|
|
authSession.first_factor = true;
|
|
|
|
authSession.second_factor = false;
|
|
|
|
authSession.identity_check = {
|
|
|
|
challenge: "u2f-register",
|
|
|
|
userid: "user"
|
|
|
|
};
|
|
|
|
});
|
2017-05-25 20:09:29 +07:00
|
|
|
});
|
|
|
|
|
2017-07-20 02:06:12 +07: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
|
|
|
|
};
|
|
|
|
const u2f_mock = U2FMock.U2FMock();
|
|
|
|
u2f_mock.request.returns(expectedRequest);
|
|
|
|
|
|
|
|
mocks.userDataStore.retrieveU2FRegistrationStub.returns(BluebirdPromise.resolve({
|
|
|
|
registration: {
|
|
|
|
publicKey: "PUBKEY",
|
|
|
|
keyHandle: "KeyHandle"
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
mocks.u2f = u2f_mock;
|
|
|
|
return U2FSignRequestGet.default(req as any, res as any)
|
|
|
|
.then(function () {
|
|
|
|
assert.deepEqual(expectedRequest, authSession.sign_request);
|
|
|
|
assert.deepEqual(expectedRequest, res.json.getCall(0).args[0].request);
|
|
|
|
});
|
|
|
|
});
|
2017-05-25 20:09:29 +07:00
|
|
|
});
|
|
|
|
|