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-22 15:42:05 +00:00
|
|
|
import { AuthenticationSessionHandler } from "../../../src/lib/AuthenticationSessionHandler";
|
2017-10-17 21:24:02 +00:00
|
|
|
import { AuthenticationSession } from "../../../types/AuthenticationSession";
|
2017-10-07 16:37:08 +00:00
|
|
|
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-11-01 13:24:18 +00:00
|
|
|
describe("test /api/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-10-22 15:42:05 +00:00
|
|
|
let authSession: AuthenticationSession;
|
2017-05-21 10:27:12 +00:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
req = ExpressMock.RequestMock();
|
|
|
|
res = ExpressMock.ResponseMock();
|
2017-11-01 13:24:18 +00:00
|
|
|
req.originalUrl = "/api/xxxx";
|
2017-09-24 21:19:03 +00:00
|
|
|
req.query = {
|
2017-12-04 21:39:55 +00:00
|
|
|
redirect: "undefined"
|
2017-09-24 21:19:03 +00:00
|
|
|
};
|
2017-10-17 21:24:02 +00:00
|
|
|
AuthenticationSessionHandler.reset(req as any);
|
2018-04-24 22:41:41 +00:00
|
|
|
req.headers["x-original-url"] = "https://secret.example.com/";
|
2017-12-04 21:39:55 +00:00
|
|
|
const s = ServerVariablesMockBuilder.build(false);
|
2017-10-16 22:35:34 +00:00
|
|
|
mocks = s.mocks;
|
|
|
|
vars = s.variables;
|
2017-10-22 15:42:05 +00:00
|
|
|
authSession = AuthenticationSessionHandler.get(req as any, vars.logger);
|
2017-05-21 10:27:12 +00:00
|
|
|
});
|
|
|
|
|
2017-11-01 18:23:45 +00:00
|
|
|
describe("with session cookie", function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
vars.config.authentication_methods.default_method = "two_factor";
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should be already authenticated", function () {
|
|
|
|
mocks.accessController.isAccessAllowedMock.returns(true);
|
|
|
|
authSession.first_factor = true;
|
|
|
|
authSession.second_factor = true;
|
|
|
|
authSession.userid = "myuser";
|
|
|
|
authSession.groups = ["mygroup", "othergroup"];
|
|
|
|
return VerifyGet.default(vars)(req as express.Request, res as any)
|
|
|
|
.then(function () {
|
|
|
|
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]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function test_session(_authSession: AuthenticationSession, status_code: number) {
|
|
|
|
return VerifyGet.default(vars)(req as express.Request, res as any)
|
|
|
|
.then(function () {
|
|
|
|
Assert.equal(status_code, res.status.getCall(0).args[0]);
|
|
|
|
});
|
|
|
|
}
|
2017-05-21 10:27:12 +00:00
|
|
|
|
2017-11-01 18:23:45 +00:00
|
|
|
function test_non_authenticated_401(authSession: AuthenticationSession) {
|
|
|
|
return test_session(authSession, 401);
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_unauthorized_403(authSession: AuthenticationSession) {
|
|
|
|
return test_session(authSession, 403);
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_authorized(authSession: AuthenticationSession) {
|
|
|
|
return test_session(authSession, 204);
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("given user tries to access a 2-factor endpoint", function () {
|
|
|
|
before(function () {
|
|
|
|
mocks.accessController.isAccessAllowedMock.returns(true);
|
2017-09-24 21:19:03 +00:00
|
|
|
});
|
|
|
|
|
2017-11-01 18:23:45 +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: [],
|
|
|
|
last_activity_datetime: new Date().getTime()
|
|
|
|
});
|
|
|
|
});
|
2017-09-24 21:19:03 +00:00
|
|
|
|
2017-11-01 18:23:45 +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: [],
|
|
|
|
last_activity_datetime: new Date().getTime()
|
|
|
|
});
|
|
|
|
});
|
2017-09-24 21:19:03 +00:00
|
|
|
|
2017-11-01 18:23:45 +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: [],
|
|
|
|
last_activity_datetime: new Date().getTime()
|
|
|
|
});
|
|
|
|
});
|
2017-09-24 21:19:03 +00:00
|
|
|
|
2017-11-01 18:23:45 +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: [],
|
|
|
|
last_activity_datetime: new Date().getTime()
|
|
|
|
});
|
|
|
|
});
|
2017-10-16 22:35:34 +00:00
|
|
|
|
2017-11-01 18:23:45 +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-11-01 18:23:45 +00:00
|
|
|
it("should not be authenticated when domain is not allowed for user", function () {
|
|
|
|
authSession.first_factor = true;
|
|
|
|
authSession.second_factor = true;
|
|
|
|
authSession.userid = "myuser";
|
2018-04-24 22:41:41 +00:00
|
|
|
req.headers["x-original-url"] = "https://test.example.com/";
|
2017-11-01 18:23:45 +00:00
|
|
|
mocks.accessController.isAccessAllowedMock.returns(false);
|
|
|
|
|
|
|
|
return test_unauthorized_403({
|
|
|
|
first_factor: true,
|
|
|
|
second_factor: true,
|
|
|
|
userid: "user",
|
|
|
|
groups: ["group1", "group2"],
|
|
|
|
email: undefined,
|
|
|
|
last_activity_datetime: new Date().getTime()
|
|
|
|
});
|
2017-09-24 21:19:03 +00:00
|
|
|
});
|
2017-05-25 13:09:29 +00:00
|
|
|
});
|
2017-11-01 18:23:45 +00:00
|
|
|
});
|
2017-05-21 10:27:12 +00:00
|
|
|
|
2017-11-01 18:23:45 +00:00
|
|
|
describe("given user tries to access a single factor endpoint", function () {
|
|
|
|
beforeEach(function () {
|
2018-04-24 22:41:41 +00:00
|
|
|
req.headers["x-original-url"] = "https://redirect.url/";
|
2017-11-01 18:23:45 +00:00
|
|
|
mocks.config.authentication_methods.per_subdomain_methods = {
|
|
|
|
"redirect.url": "single_factor"
|
|
|
|
};
|
2017-09-24 21:19:03 +00:00
|
|
|
});
|
|
|
|
|
2017-11-01 18:23:45 +00:00
|
|
|
it("should be authenticated when first factor is validated and second factor is not", function () {
|
|
|
|
mocks.accessController.isAccessAllowedMock.returns(true);
|
|
|
|
authSession.first_factor = true;
|
|
|
|
authSession.userid = "user1";
|
|
|
|
return VerifyGet.default(vars)(req as express.Request, res as any)
|
|
|
|
.then(function () {
|
|
|
|
Assert(res.status.calledWith(204));
|
|
|
|
Assert(res.send.calledOnce);
|
|
|
|
});
|
2017-09-24 21:19:03 +00:00
|
|
|
});
|
|
|
|
|
2017-11-01 18:23:45 +00:00
|
|
|
it("should be rejected with 401 when first factor is not validated", function () {
|
|
|
|
mocks.accessController.isAccessAllowedMock.returns(true);
|
|
|
|
authSession.first_factor = false;
|
|
|
|
return VerifyGet.default(vars)(req as express.Request, res as any)
|
|
|
|
.then(function () {
|
|
|
|
Assert(res.status.calledWith(401));
|
|
|
|
});
|
2017-05-21 10:27:12 +00:00
|
|
|
});
|
2017-11-01 18:23:45 +00:00
|
|
|
});
|
2017-05-21 10:27:12 +00:00
|
|
|
|
2017-11-01 18:23:45 +00:00
|
|
|
describe("inactivity period", function () {
|
|
|
|
it("should update last inactivity period on requests on /api/verify", function () {
|
|
|
|
mocks.config.session.inactivity = 200000;
|
|
|
|
mocks.accessController.isAccessAllowedMock.returns(true);
|
|
|
|
const currentTime = new Date().getTime() - 1000;
|
|
|
|
AuthenticationSessionHandler.reset(req as any);
|
2017-10-22 15:42:05 +00:00
|
|
|
authSession.first_factor = true;
|
|
|
|
authSession.second_factor = true;
|
|
|
|
authSession.userid = "myuser";
|
2017-11-01 18:23:45 +00:00
|
|
|
authSession.groups = ["mygroup", "othergroup"];
|
|
|
|
authSession.last_activity_datetime = currentTime;
|
|
|
|
return VerifyGet.default(vars)(req as express.Request, res as any)
|
|
|
|
.then(function () {
|
|
|
|
return AuthenticationSessionHandler.get(req as any, vars.logger);
|
|
|
|
})
|
|
|
|
.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;
|
|
|
|
AuthenticationSessionHandler.reset(req as any);
|
|
|
|
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 () {
|
|
|
|
return AuthenticationSessionHandler.get(req as any, vars.logger);
|
|
|
|
})
|
|
|
|
.then(function (authSession) {
|
|
|
|
Assert.equal(authSession.first_factor, false);
|
|
|
|
Assert.equal(authSession.second_factor, false);
|
|
|
|
Assert.equal(authSession.userid, undefined);
|
|
|
|
});
|
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-12-04 21:39:55 +00:00
|
|
|
describe("response type 401 | 302", function() {
|
|
|
|
it("should return error code 401", function() {
|
|
|
|
mocks.accessController.isAccessAllowedMock.returns(true);
|
|
|
|
mocks.config.authentication_methods.default_method = "single_factor";
|
|
|
|
mocks.ldapAuthenticator.authenticateStub.rejects(new Error(
|
|
|
|
"Invalid credentials"));
|
|
|
|
req.headers["proxy-authorization"] = "Basic am9objpwYXNzd29yZA==";
|
|
|
|
|
|
|
|
return VerifyGet.default(vars)(req as express.Request, res as any)
|
|
|
|
.then(function () {
|
|
|
|
Assert(res.status.calledWithExactly(401));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should redirect to provided redirection url", function() {
|
|
|
|
const REDIRECT_URL = "http://redirection_url.com";
|
|
|
|
mocks.accessController.isAccessAllowedMock.returns(true);
|
|
|
|
mocks.config.authentication_methods.default_method = "single_factor";
|
|
|
|
mocks.ldapAuthenticator.authenticateStub.rejects(new Error(
|
|
|
|
"Invalid credentials"));
|
|
|
|
req.headers["proxy-authorization"] = "Basic am9objpwYXNzd29yZA==";
|
2018-04-24 22:41:41 +00:00
|
|
|
req.query["rd"] = REDIRECT_URL;
|
2017-12-04 21:39:55 +00:00
|
|
|
|
|
|
|
return VerifyGet.default(vars)(req as express.Request, res as any)
|
|
|
|
.then(function () {
|
|
|
|
Assert(res.redirect.calledWithExactly(REDIRECT_URL));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-11-01 18:23:45 +00:00
|
|
|
describe("with basic auth", function () {
|
|
|
|
it("should authenticate correctly", function () {
|
|
|
|
mocks.accessController.isAccessAllowedMock.returns(true);
|
|
|
|
mocks.config.authentication_methods.default_method = "single_factor";
|
|
|
|
mocks.ldapAuthenticator.authenticateStub.returns({
|
|
|
|
groups: ["mygroup", "othergroup"],
|
|
|
|
});
|
|
|
|
req.headers["proxy-authorization"] = "Basic am9objpwYXNzd29yZA==";
|
|
|
|
|
|
|
|
return VerifyGet.default(vars)(req as express.Request, res as any)
|
|
|
|
.then(function () {
|
|
|
|
Sinon.assert.calledWithExactly(res.setHeader, "Remote-User", "john");
|
|
|
|
Sinon.assert.calledWithExactly(res.setHeader, "Remote-Groups", "mygroup,othergroup");
|
|
|
|
Assert.equal(204, res.status.getCall(0).args[0]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should fail when endpoint is protected by two factors", function () {
|
|
|
|
mocks.accessController.isAccessAllowedMock.returns(true);
|
|
|
|
mocks.config.authentication_methods.default_method = "single_factor";
|
2017-10-16 22:35:34 +00:00
|
|
|
mocks.config.authentication_methods.per_subdomain_methods = {
|
2017-11-01 18:23:45 +00:00
|
|
|
"secret.example.com": "two_factor"
|
2017-10-16 22:35:34 +00:00
|
|
|
};
|
2017-11-01 18:23:45 +00:00
|
|
|
mocks.ldapAuthenticator.authenticateStub.resolves({
|
|
|
|
groups: ["mygroup", "othergroup"],
|
|
|
|
});
|
|
|
|
req.headers["proxy-authorization"] = "Basic am9objpwYXNzd29yZA==";
|
|
|
|
|
|
|
|
return VerifyGet.default(vars)(req as express.Request, res as any)
|
|
|
|
.then(function () {
|
|
|
|
Assert(res.status.calledWithExactly(401));
|
|
|
|
});
|
2017-05-21 10:27:12 +00:00
|
|
|
});
|
|
|
|
|
2017-11-01 18:23:45 +00:00
|
|
|
it("should fail when base64 token is not valid", function () {
|
2017-10-16 22:35:34 +00:00
|
|
|
mocks.accessController.isAccessAllowedMock.returns(true);
|
2017-11-01 18:23:45 +00:00
|
|
|
mocks.config.authentication_methods.default_method = "single_factor";
|
|
|
|
mocks.ldapAuthenticator.authenticateStub.resolves({
|
|
|
|
groups: ["mygroup", "othergroup"],
|
|
|
|
});
|
|
|
|
req.headers["proxy-authorization"] = "Basic i_m*not_a_base64*token";
|
|
|
|
|
2017-10-22 15:42:05 +00:00
|
|
|
return VerifyGet.default(vars)(req as express.Request, res as any)
|
2017-09-24 21:19:03 +00:00
|
|
|
.then(function () {
|
2017-11-01 18:23:45 +00:00
|
|
|
Assert(res.status.calledWithExactly(401));
|
2017-09-24 21:19:03 +00:00
|
|
|
});
|
|
|
|
});
|
2017-09-21 20:07:34 +00:00
|
|
|
|
2017-11-01 18:23:45 +00:00
|
|
|
it("should fail when base64 token has not format user:psswd", function () {
|
2017-10-16 22:35:34 +00:00
|
|
|
mocks.accessController.isAccessAllowedMock.returns(true);
|
2017-11-01 18:23:45 +00:00
|
|
|
mocks.config.authentication_methods.default_method = "single_factor";
|
|
|
|
mocks.ldapAuthenticator.authenticateStub.resolves({
|
|
|
|
groups: ["mygroup", "othergroup"],
|
|
|
|
});
|
|
|
|
req.headers["proxy-authorization"] = "Basic am9objpwYXNzOmJhZA==";
|
|
|
|
|
2017-10-22 15:42:05 +00:00
|
|
|
return VerifyGet.default(vars)(req as express.Request, res as any)
|
2017-09-24 21:19:03 +00:00
|
|
|
.then(function () {
|
2017-11-01 18:23:45 +00:00
|
|
|
Assert(res.status.calledWithExactly(401));
|
2017-09-21 20:07:34 +00:00
|
|
|
});
|
2017-05-21 21:32:09 +00:00
|
|
|
});
|
2017-10-16 22:38:10 +00:00
|
|
|
|
2017-11-01 18:23:45 +00:00
|
|
|
it("should fail when bad user password is provided", function () {
|
2017-10-16 22:38:10 +00:00
|
|
|
mocks.accessController.isAccessAllowedMock.returns(true);
|
2017-11-01 18:23:45 +00:00
|
|
|
mocks.config.authentication_methods.default_method = "single_factor";
|
|
|
|
mocks.ldapAuthenticator.authenticateStub.rejects(new Error(
|
|
|
|
"Invalid credentials"));
|
|
|
|
req.headers["proxy-authorization"] = "Basic am9objpwYXNzd29yZA==";
|
|
|
|
|
2017-10-22 15:42:05 +00:00
|
|
|
return VerifyGet.default(vars)(req as express.Request, res as any)
|
2017-10-16 22:38:10 +00:00
|
|
|
.then(function () {
|
2017-11-01 18:23:45 +00:00
|
|
|
Assert(res.status.calledWithExactly(401));
|
2017-10-16 22:38:10 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-11-01 18:23:45 +00:00
|
|
|
it("should fail when resource is restricted", function () {
|
|
|
|
mocks.accessController.isAccessAllowedMock.returns(false);
|
|
|
|
mocks.config.authentication_methods.default_method = "single_factor";
|
|
|
|
mocks.ldapAuthenticator.authenticateStub.resolves({
|
|
|
|
groups: ["mygroup", "othergroup"],
|
|
|
|
});
|
|
|
|
req.headers["proxy-authorization"] = "Basic am9objpwYXNzd29yZA==";
|
|
|
|
|
2017-10-22 15:42:05 +00:00
|
|
|
return VerifyGet.default(vars)(req as express.Request, res as any)
|
2017-10-16 22:38:10 +00:00
|
|
|
.then(function () {
|
2017-11-01 18:23:45 +00:00
|
|
|
Assert(res.status.calledWithExactly(401));
|
2017-10-16 22:38:10 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-05-21 10:27:12 +00:00
|
|
|
});
|
|
|
|
|