2017-05-21 17:27:12 +07:00
|
|
|
|
|
|
|
import assert = require("assert");
|
2017-05-25 20:09:29 +07:00
|
|
|
import VerifyGet = require("../../../../src/server/lib/routes/verify/get");
|
|
|
|
import AuthenticationSession = require("../../../../src/server/lib/AuthenticationSession");
|
|
|
|
|
2017-05-21 17:27:12 +07:00
|
|
|
import sinon = require("sinon");
|
|
|
|
import winston = require("winston");
|
2017-05-25 20:09:29 +07:00
|
|
|
import BluebirdPromise = require("bluebird");
|
2017-05-21 17:27:12 +07:00
|
|
|
|
|
|
|
import express = require("express");
|
|
|
|
|
2017-05-25 20:09:29 +07:00
|
|
|
import ExpressMock = require("../../mocks/express");
|
|
|
|
import AccessControllerMock = require("../../mocks/AccessController");
|
|
|
|
import ServerVariablesMock = require("../../mocks/ServerVariablesMock");
|
2017-05-21 17:27:12 +07:00
|
|
|
|
|
|
|
describe("test authentication token verification", function () {
|
|
|
|
let req: ExpressMock.RequestMock;
|
|
|
|
let res: ExpressMock.ResponseMock;
|
|
|
|
let accessController: AccessControllerMock.AccessControllerMock;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
accessController = AccessControllerMock.AccessControllerMock();
|
|
|
|
accessController.isDomainAllowedForUser.returns(true);
|
|
|
|
|
|
|
|
req = ExpressMock.RequestMock();
|
|
|
|
res = ExpressMock.ResponseMock();
|
|
|
|
req.headers = {};
|
|
|
|
req.headers.host = "secret.example.com";
|
2017-05-25 20:09:29 +07:00
|
|
|
const mocks = ServerVariablesMock.mock(req.app);
|
|
|
|
mocks.config = {};
|
|
|
|
mocks.logger = winston;
|
|
|
|
mocks.accessController = accessController;
|
2017-05-21 17:27:12 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should be already authenticated", function (done) {
|
|
|
|
req.session = {};
|
2017-05-25 20:09:29 +07:00
|
|
|
AuthenticationSession.reset(req as any);
|
|
|
|
const authSession = AuthenticationSession.get(req as any);
|
|
|
|
authSession.first_factor = true;
|
|
|
|
authSession.second_factor = true;
|
|
|
|
authSession.userid = "myuser";
|
2017-05-21 17:27:12 +07:00
|
|
|
|
|
|
|
res.send = sinon.spy(function () {
|
|
|
|
assert.equal(204, res.status.getCall(0).args[0]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2017-05-25 20:09:29 +07:00
|
|
|
VerifyGet.default(req as express.Request, res as any);
|
2017-05-21 17:27:12 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("given different cases of session", function () {
|
2017-05-25 20:09:29 +07:00
|
|
|
function test_session(auth_session: AuthenticationSession.AuthenticationSession, status_code: number) {
|
|
|
|
return new BluebirdPromise(function (resolve, reject) {
|
2017-05-21 17:27:12 +07:00
|
|
|
req.session = {};
|
|
|
|
req.session.auth_session = auth_session;
|
|
|
|
|
|
|
|
res.send = sinon.spy(function () {
|
|
|
|
assert.equal(status_code, res.status.getCall(0).args[0]);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
|
2017-05-25 20:09:29 +07:00
|
|
|
VerifyGet.default(req as express.Request, res as any);
|
2017-05-21 17:27:12 +07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-25 20:09:29 +07:00
|
|
|
function test_unauthorized(auth_session: AuthenticationSession.AuthenticationSession) {
|
2017-05-21 17:27:12 +07:00
|
|
|
return test_session(auth_session, 401);
|
|
|
|
}
|
|
|
|
|
2017-05-25 20:09:29 +07:00
|
|
|
function test_authorized(auth_session: AuthenticationSession.AuthenticationSession) {
|
2017-05-21 17:27:12 +07:00
|
|
|
return test_session(auth_session, 204);
|
|
|
|
}
|
|
|
|
|
|
|
|
it("should not be authenticated when second factor is missing", function () {
|
|
|
|
return test_unauthorized({
|
|
|
|
userid: "user",
|
|
|
|
first_factor: true,
|
2017-05-25 20:09:29 +07:00
|
|
|
second_factor: false,
|
|
|
|
email: undefined,
|
|
|
|
groups: [],
|
2017-05-21 17:27:12 +07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not be authenticated when first factor is missing", function () {
|
2017-05-25 20:09:29 +07:00
|
|
|
return test_unauthorized({
|
|
|
|
userid: "user",
|
|
|
|
first_factor: false,
|
|
|
|
second_factor: true,
|
|
|
|
email: undefined,
|
|
|
|
groups: [],
|
|
|
|
});
|
2017-05-21 17:27:12 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should not be authenticated when userid is missing", function () {
|
|
|
|
return test_unauthorized({
|
2017-05-25 20:09:29 +07:00
|
|
|
userid: undefined,
|
2017-05-21 17:27:12 +07:00
|
|
|
first_factor: true,
|
2017-05-25 20:09:29 +07:00
|
|
|
second_factor: false,
|
|
|
|
email: undefined,
|
|
|
|
groups: [],
|
2017-05-21 17:27:12 +07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not be authenticated when first and second factor are missing", function () {
|
2017-05-25 20:09:29 +07:00
|
|
|
return test_unauthorized({
|
|
|
|
userid: "user",
|
|
|
|
first_factor: false,
|
|
|
|
second_factor: false,
|
|
|
|
email: undefined,
|
|
|
|
groups: [],
|
|
|
|
});
|
2017-05-21 17:27:12 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should not be authenticated when session has not be initiated", function () {
|
|
|
|
return test_unauthorized(undefined);
|
|
|
|
});
|
|
|
|
|
2017-06-02 03:08:20 +07:00
|
|
|
it("should not be authenticated when domain is not allowed for user", function () {
|
2017-05-22 04:32:09 +07:00
|
|
|
req.headers.host = "test.example.com";
|
|
|
|
|
|
|
|
accessController.isDomainAllowedForUser.returns(false);
|
|
|
|
accessController.isDomainAllowedForUser.withArgs("test.example.com", "user", ["group1", "group2"]).returns(true);
|
|
|
|
|
2017-05-25 20:09:29 +07:00
|
|
|
return test_unauthorized({
|
2017-05-22 04:32:09 +07:00
|
|
|
first_factor: true,
|
|
|
|
second_factor: true,
|
|
|
|
userid: "user",
|
2017-05-25 20:09:29 +07:00
|
|
|
groups: ["group1", "group2"],
|
|
|
|
email: undefined
|
2017-05-22 04:32:09 +07:00
|
|
|
});
|
|
|
|
});
|
2017-05-21 17:27:12 +07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|