2017-05-21 17:27:12 +07:00
|
|
|
|
|
|
|
import assert = require("assert");
|
2017-07-16 19:55:01 +07:00
|
|
|
import VerifyGet = require("../../../../../src/server/lib/routes/verify/get");
|
|
|
|
import AuthenticationSession = require("../../../../../src/server/lib/AuthenticationSession");
|
2017-05-25 20:09:29 +07:00
|
|
|
|
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();
|
2017-09-22 03:07:34 +07:00
|
|
|
req.app = {
|
|
|
|
get: sinon.stub().returns({ logger: winston })
|
|
|
|
};
|
2017-08-03 05:30:41 +07:00
|
|
|
req.session = {};
|
|
|
|
AuthenticationSession.reset(req as any);
|
2017-05-21 17:27:12 +07:00
|
|
|
req.headers = {};
|
|
|
|
req.headers.host = "secret.example.com";
|
2017-05-25 20:09:29 +07:00
|
|
|
const mocks = ServerVariablesMock.mock(req.app);
|
2017-07-16 22:37:13 +07:00
|
|
|
mocks.config = {} as any;
|
2017-05-25 20:09:29 +07:00
|
|
|
mocks.logger = winston;
|
2017-07-16 22:37:13 +07:00
|
|
|
mocks.accessController = accessController as any;
|
2017-05-21 17:27:12 +07:00
|
|
|
});
|
|
|
|
|
2017-08-03 05:30:41 +07:00
|
|
|
it("should be already authenticated", function () {
|
2017-05-21 17:27:12 +07:00
|
|
|
req.session = {};
|
2017-05-25 20:09:29 +07:00
|
|
|
AuthenticationSession.reset(req as any);
|
2017-09-22 03:07:34 +07:00
|
|
|
return AuthenticationSession.get(req as any)
|
|
|
|
.then(function (authSession: AuthenticationSession.AuthenticationSession) {
|
|
|
|
authSession.first_factor = true;
|
|
|
|
authSession.second_factor = true;
|
|
|
|
authSession.userid = "myuser";
|
|
|
|
return VerifyGet.default(req as express.Request, res as any);
|
|
|
|
})
|
2017-08-03 05:30:41 +07:00
|
|
|
.then(function () {
|
|
|
|
assert.equal(204, res.status.getCall(0).args[0]);
|
|
|
|
});
|
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) {
|
2017-08-03 05:30:41 +07:00
|
|
|
return VerifyGet.default(req as express.Request, res as any)
|
|
|
|
.then(function () {
|
2017-05-21 17:27:12 +07:00
|
|
|
assert.equal(status_code, res.status.getCall(0).args[0]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-03 05:30:41 +07:00
|
|
|
function test_non_authenticated_401(auth_session: AuthenticationSession.AuthenticationSession) {
|
2017-05-21 17:27:12 +07:00
|
|
|
return test_session(auth_session, 401);
|
|
|
|
}
|
|
|
|
|
2017-08-03 05:30:41 +07:00
|
|
|
function test_unauthorized_403(auth_session: AuthenticationSession.AuthenticationSession) {
|
|
|
|
return test_session(auth_session, 403);
|
|
|
|
}
|
|
|
|
|
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 () {
|
2017-08-03 05:30:41 +07:00
|
|
|
return test_non_authenticated_401({
|
2017-05-21 17:27:12 +07:00
|
|
|
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-08-03 05:30:41 +07:00
|
|
|
return test_non_authenticated_401({
|
2017-05-25 20:09:29 +07:00
|
|
|
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 () {
|
2017-08-03 05:30:41 +07:00
|
|
|
return test_non_authenticated_401({
|
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-08-03 05:30:41 +07:00
|
|
|
return test_non_authenticated_401({
|
2017-05-25 20:09:29 +07:00
|
|
|
userid: "user",
|
|
|
|
first_factor: false,
|
|
|
|
second_factor: false,
|
|
|
|
email: undefined,
|
|
|
|
groups: [],
|
2017-08-03 05:30:41 +07:00
|
|
|
});
|
2017-05-21 17:27:12 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should not be authenticated when session has not be initiated", function () {
|
2017-08-03 05:30:41 +07:00
|
|
|
return test_non_authenticated_401(undefined);
|
2017-05-21 17:27:12 +07:00
|
|
|
});
|
|
|
|
|
2017-06-02 03:08:20 +07:00
|
|
|
it("should not be authenticated when domain is not allowed for user", function () {
|
2017-09-22 03:07:34 +07:00
|
|
|
return AuthenticationSession.get(req as any)
|
|
|
|
.then(function (authSession: AuthenticationSession.AuthenticationSession) {
|
|
|
|
authSession.first_factor = true;
|
|
|
|
authSession.second_factor = true;
|
|
|
|
authSession.userid = "myuser";
|
|
|
|
|
|
|
|
req.headers.host = "test.example.com";
|
|
|
|
|
|
|
|
accessController.isDomainAllowedForUser.returns(false);
|
|
|
|
accessController.isDomainAllowedForUser.withArgs("test.example.com", "user", ["group1", "group2"]).returns(true);
|
|
|
|
|
|
|
|
return test_unauthorized_403({
|
|
|
|
first_factor: true,
|
|
|
|
second_factor: true,
|
|
|
|
userid: "user",
|
|
|
|
groups: ["group1", "group2"],
|
|
|
|
email: undefined
|
|
|
|
});
|
|
|
|
});
|
2017-05-22 04:32:09 +07:00
|
|
|
});
|
2017-05-21 17:27:12 +07:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|