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