2017-05-21 10:27:12 +00:00
|
|
|
|
2017-09-24 21:19:03 +00:00
|
|
|
import Assert = require("assert");
|
2017-10-06 22:09:42 +00:00
|
|
|
import VerifyGet = require("../../../src/lib/routes/verify/get");
|
2017-10-17 21:24:02 +00:00
|
|
|
import AuthenticationSessionHandler = require("../../../src/lib/AuthenticationSession");
|
|
|
|
import { AuthenticationSession } from "../../../types/AuthenticationSession";
|
2017-10-07 16:37:08 +00:00
|
|
|
import { AuthenticationMethodCalculator } from "../../../src/lib/AuthenticationMethodCalculator";
|
|
|
|
import { AuthenticationMethodsConfiguration } from "../../../src/lib/configuration/Configuration";
|
2017-09-24 21:19:03 +00:00
|
|
|
import Sinon = require("sinon");
|
2017-05-21 10:27:12 +00:00
|
|
|
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");
|
2017-10-16 22:35:34 +00:00
|
|
|
import { ServerVariables } from "../../../src/lib/ServerVariables";
|
|
|
|
import { ServerVariablesMockBuilder, ServerVariablesMock } from "../../mocks/ServerVariablesMockBuilder";
|
2017-05-21 10:27:12 +00:00
|
|
|
|
2017-10-16 22:35:34 +00:00
|
|
|
describe("test /verify endpoint", function () {
|
2017-05-21 10:27:12 +00:00
|
|
|
let req: ExpressMock.RequestMock;
|
|
|
|
let res: ExpressMock.ResponseMock;
|
2017-10-16 22:35:34 +00:00
|
|
|
let mocks: ServerVariablesMock;
|
|
|
|
let vars: ServerVariables;
|
2017-05-21 10:27:12 +00:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
req = ExpressMock.RequestMock();
|
|
|
|
res = ExpressMock.ResponseMock();
|
2017-09-24 21:19:03 +00:00
|
|
|
req.session = {};
|
|
|
|
req.query = {
|
|
|
|
redirect: "http://redirect.url"
|
|
|
|
};
|
2017-09-21 20:07:34 +00:00
|
|
|
req.app = {
|
2017-09-24 21:19:03 +00:00
|
|
|
get: Sinon.stub().returns({ logger: winston })
|
2017-09-21 20:07:34 +00:00
|
|
|
};
|
2017-10-17 21:24:02 +00:00
|
|
|
AuthenticationSessionHandler.reset(req as any);
|
2017-05-21 10:27:12 +00:00
|
|
|
req.headers = {};
|
|
|
|
req.headers.host = "secret.example.com";
|
2017-10-16 22:35:34 +00:00
|
|
|
const s = ServerVariablesMockBuilder.build();
|
|
|
|
mocks = s.mocks;
|
|
|
|
vars = s.variables;
|
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-10-16 22:35:34 +00:00
|
|
|
mocks.accessController.isAccessAllowedMock.returns(true);
|
2017-10-17 21:24:02 +00:00
|
|
|
AuthenticationSessionHandler.reset(req as any);
|
|
|
|
return AuthenticationSessionHandler.get(req as any, vars.logger)
|
|
|
|
.then(function (authSession) {
|
2017-09-21 20:07:34 +00:00
|
|
|
authSession.first_factor = true;
|
|
|
|
authSession.second_factor = true;
|
|
|
|
authSession.userid = "myuser";
|
2017-09-22 19:18:38 +00:00
|
|
|
authSession.groups = ["mygroup", "othergroup"];
|
2017-10-16 22:35:34 +00:00
|
|
|
return VerifyGet.default(vars)(req as express.Request, res as any);
|
2017-09-21 20:07:34 +00:00
|
|
|
})
|
2017-08-02 22:30:41 +00:00
|
|
|
.then(function () {
|
2017-09-24 21:19:03 +00:00
|
|
|
Sinon.assert.calledWithExactly(res.setHeader, "Remote-User", "myuser");
|
|
|
|
Sinon.assert.calledWithExactly(res.setHeader, "Remote-Groups", "mygroup,othergroup");
|
|
|
|
Assert.equal(204, res.status.getCall(0).args[0]);
|
2017-08-02 22:30:41 +00:00
|
|
|
});
|
2017-05-21 10:27:12 +00:00
|
|
|
});
|
|
|
|
|
2017-10-17 21:24:02 +00:00
|
|
|
function test_session(_authSession: AuthenticationSession, status_code: number) {
|
|
|
|
return AuthenticationSessionHandler.get(req as any, vars.logger)
|
2017-09-24 21:19:03 +00:00
|
|
|
.then(function (authSession) {
|
|
|
|
authSession = _authSession;
|
2017-10-16 22:35:34 +00:00
|
|
|
return VerifyGet.default(vars)(req as express.Request, res as any);
|
2017-09-24 21:19:03 +00:00
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
Assert.equal(status_code, res.status.getCall(0).args[0]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-17 21:24:02 +00:00
|
|
|
function test_non_authenticated_401(authSession: AuthenticationSession) {
|
2017-10-16 22:35:34 +00:00
|
|
|
return test_session(authSession, 401);
|
2017-09-24 21:19:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 21:24:02 +00:00
|
|
|
function test_unauthorized_403(authSession: AuthenticationSession) {
|
2017-10-16 22:35:34 +00:00
|
|
|
return test_session(authSession, 403);
|
2017-09-24 21:19:03 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 21:24:02 +00:00
|
|
|
function test_authorized(authSession: AuthenticationSession) {
|
2017-10-16 22:35:34 +00:00
|
|
|
return test_session(authSession, 204);
|
2017-09-24 21:19:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
describe("given user tries to access a 2-factor endpoint", function () {
|
2017-10-16 22:38:10 +00:00
|
|
|
before(function () {
|
2017-10-16 22:35:34 +00:00
|
|
|
mocks.accessController.isAccessAllowedMock.returns(true);
|
|
|
|
});
|
|
|
|
|
2017-09-24 21:19:03 +00:00
|
|
|
describe("given different cases of session", function () {
|
|
|
|
it("should not be authenticated when second factor is missing", function () {
|
|
|
|
return test_non_authenticated_401({
|
|
|
|
userid: "user",
|
|
|
|
first_factor: true,
|
|
|
|
second_factor: false,
|
|
|
|
email: undefined,
|
|
|
|
groups: [],
|
2017-10-16 22:38:10 +00:00
|
|
|
last_activity_datetime: new Date().getTime()
|
2017-05-21 10:27:12 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-09-24 21:19:03 +00:00
|
|
|
it("should not be authenticated when first factor is missing", function () {
|
|
|
|
return test_non_authenticated_401({
|
|
|
|
userid: "user",
|
|
|
|
first_factor: false,
|
|
|
|
second_factor: true,
|
|
|
|
email: undefined,
|
|
|
|
groups: [],
|
2017-10-16 22:38:10 +00:00
|
|
|
last_activity_datetime: new Date().getTime()
|
2017-09-24 21:19:03 +00:00
|
|
|
});
|
2017-05-25 13:09:29 +00:00
|
|
|
});
|
2017-05-21 10:27:12 +00:00
|
|
|
|
2017-09-24 21:19:03 +00:00
|
|
|
it("should not be authenticated when userid is missing", function () {
|
|
|
|
return test_non_authenticated_401({
|
|
|
|
userid: undefined,
|
|
|
|
first_factor: true,
|
|
|
|
second_factor: false,
|
|
|
|
email: undefined,
|
|
|
|
groups: [],
|
2017-10-16 22:38:10 +00:00
|
|
|
last_activity_datetime: new Date().getTime()
|
2017-09-24 21:19:03 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not be authenticated when first and second factor are missing", function () {
|
|
|
|
return test_non_authenticated_401({
|
|
|
|
userid: "user",
|
|
|
|
first_factor: false,
|
|
|
|
second_factor: false,
|
|
|
|
email: undefined,
|
|
|
|
groups: [],
|
2017-10-16 22:38:10 +00:00
|
|
|
last_activity_datetime: new Date().getTime()
|
2017-09-24 21:19:03 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not be authenticated when session has not be initiated", function () {
|
|
|
|
return test_non_authenticated_401(undefined);
|
2017-05-21 10:27:12 +00:00
|
|
|
});
|
|
|
|
|
2017-09-24 21:19:03 +00:00
|
|
|
it("should not be authenticated when domain is not allowed for user", function () {
|
2017-10-17 21:24:02 +00:00
|
|
|
return AuthenticationSessionHandler.get(req as any, vars.logger)
|
2017-10-16 22:35:34 +00:00
|
|
|
.then(function (authSession) {
|
2017-09-24 21:19:03 +00:00
|
|
|
authSession.first_factor = true;
|
|
|
|
authSession.second_factor = true;
|
|
|
|
authSession.userid = "myuser";
|
|
|
|
req.headers.host = "test.example.com";
|
2017-10-16 22:35:34 +00:00
|
|
|
mocks.accessController.isAccessAllowedMock.returns(false);
|
2017-09-24 21:19:03 +00:00
|
|
|
|
|
|
|
return test_unauthorized_403({
|
|
|
|
first_factor: true,
|
|
|
|
second_factor: true,
|
|
|
|
userid: "user",
|
|
|
|
groups: ["group1", "group2"],
|
2017-10-16 22:35:34 +00:00
|
|
|
email: undefined,
|
2017-10-16 22:38:10 +00:00
|
|
|
last_activity_datetime: new Date().getTime()
|
2017-09-24 21:19:03 +00:00
|
|
|
});
|
|
|
|
});
|
2017-08-02 22:30:41 +00:00
|
|
|
});
|
2017-05-21 10:27:12 +00:00
|
|
|
});
|
2017-09-24 21:19:03 +00:00
|
|
|
});
|
2017-05-21 10:27:12 +00:00
|
|
|
|
2017-09-24 21:19:03 +00:00
|
|
|
describe("given user tries to access a basic auth endpoint", function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
req.query = {
|
2017-10-07 16:37:08 +00:00
|
|
|
redirect: "http://redirect.url"
|
2017-09-24 21:19:03 +00:00
|
|
|
};
|
2017-10-07 16:37:08 +00:00
|
|
|
req.headers["host"] = "redirect.url";
|
2017-10-16 22:35:34 +00:00
|
|
|
mocks.config.authentication_methods.per_subdomain_methods = {
|
|
|
|
"redirect.url": "basic_auth"
|
|
|
|
};
|
2017-05-21 10:27:12 +00:00
|
|
|
});
|
|
|
|
|
2017-10-16 22:35:34 +00:00
|
|
|
it("should be authenticated when first factor is validated and second factor is not", function () {
|
|
|
|
mocks.accessController.isAccessAllowedMock.returns(true);
|
2017-10-17 21:24:02 +00:00
|
|
|
return AuthenticationSessionHandler.get(req as any, vars.logger)
|
2017-10-16 22:35:34 +00:00
|
|
|
.then(function (authSession) {
|
2017-09-21 20:07:34 +00:00
|
|
|
authSession.first_factor = true;
|
2017-10-07 22:46:57 +00:00
|
|
|
authSession.userid = "user1";
|
2017-10-16 22:35:34 +00:00
|
|
|
return VerifyGet.default(vars)(req as express.Request, res as any);
|
2017-09-24 21:19:03 +00:00
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
Assert(res.status.calledWith(204));
|
|
|
|
Assert(res.send.calledOnce);
|
|
|
|
});
|
|
|
|
});
|
2017-09-21 20:07:34 +00:00
|
|
|
|
2017-09-24 21:19:03 +00:00
|
|
|
it("should be rejected with 401 when first factor is not validated", function () {
|
2017-10-16 22:35:34 +00:00
|
|
|
mocks.accessController.isAccessAllowedMock.returns(true);
|
2017-10-17 21:24:02 +00:00
|
|
|
return AuthenticationSessionHandler.get(req as any, vars.logger)
|
2017-10-16 22:35:34 +00:00
|
|
|
.then(function (authSession) {
|
2017-09-24 21:19:03 +00:00
|
|
|
authSession.first_factor = false;
|
2017-10-16 22:35:34 +00:00
|
|
|
return VerifyGet.default(vars)(req as express.Request, res as any);
|
2017-09-24 21:19:03 +00:00
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
Assert(res.status.calledWith(401));
|
2017-09-21 20:07:34 +00:00
|
|
|
});
|
2017-05-21 21:32:09 +00:00
|
|
|
});
|
2017-05-21 10:27:12 +00:00
|
|
|
});
|
2017-10-16 22:38:10 +00:00
|
|
|
|
|
|
|
describe("inactivity period", function () {
|
|
|
|
it("should update last inactivity period on requests on /verify", function () {
|
|
|
|
mocks.config.session.inactivity = 200000;
|
|
|
|
mocks.accessController.isAccessAllowedMock.returns(true);
|
|
|
|
const currentTime = new Date().getTime() - 1000;
|
2017-10-17 21:24:02 +00:00
|
|
|
AuthenticationSessionHandler.reset(req as any);
|
|
|
|
return AuthenticationSessionHandler.get(req as any, vars.logger)
|
|
|
|
.then(function (authSession) {
|
2017-10-16 22:38:10 +00:00
|
|
|
authSession.first_factor = true;
|
|
|
|
authSession.second_factor = true;
|
|
|
|
authSession.userid = "myuser";
|
|
|
|
authSession.groups = ["mygroup", "othergroup"];
|
|
|
|
authSession.last_activity_datetime = currentTime;
|
|
|
|
return VerifyGet.default(vars)(req as express.Request, res as any);
|
|
|
|
})
|
|
|
|
.then(function () {
|
2017-10-17 21:24:02 +00:00
|
|
|
return AuthenticationSessionHandler.get(req as any, vars.logger);
|
2017-10-16 22:38:10 +00:00
|
|
|
})
|
|
|
|
.then(function (authSession) {
|
|
|
|
Assert(authSession.last_activity_datetime > currentTime);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should reset session when max inactivity period has been reached", function () {
|
|
|
|
mocks.config.session.inactivity = 1;
|
|
|
|
mocks.accessController.isAccessAllowedMock.returns(true);
|
|
|
|
const currentTime = new Date().getTime() - 1000;
|
2017-10-17 21:24:02 +00:00
|
|
|
AuthenticationSessionHandler.reset(req as any);
|
|
|
|
return AuthenticationSessionHandler.get(req as any, vars.logger)
|
|
|
|
.then(function (authSession) {
|
2017-10-16 22:38:10 +00:00
|
|
|
authSession.first_factor = true;
|
|
|
|
authSession.second_factor = true;
|
|
|
|
authSession.userid = "myuser";
|
|
|
|
authSession.groups = ["mygroup", "othergroup"];
|
|
|
|
authSession.last_activity_datetime = currentTime;
|
|
|
|
return VerifyGet.default(vars)(req as express.Request, res as any);
|
|
|
|
})
|
|
|
|
.then(function () {
|
2017-10-17 21:24:02 +00:00
|
|
|
return AuthenticationSessionHandler.get(req as any, vars.logger);
|
2017-10-16 22:38:10 +00:00
|
|
|
})
|
|
|
|
.then(function (authSession) {
|
|
|
|
Assert.equal(authSession.first_factor, false);
|
|
|
|
Assert.equal(authSession.second_factor, false);
|
|
|
|
Assert.equal(authSession.userid, undefined);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-05-21 10:27:12 +00:00
|
|
|
});
|
|
|
|
|