From 0dd9a5f8156406eae0db3de07a0b2cf79cabb4df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Michaud?= Date: Sun, 19 Aug 2018 13:07:00 +0200 Subject: [PATCH] Make session cookie name customizable. (#256) This option is optional and set to authelia_session by default. --- config.template.yml | 3 +++ .../configuration/SessionConfigurationBuilder.spec.ts | 4 ++++ .../src/lib/configuration/SessionConfigurationBuilder.ts | 1 + .../configuration/schema/SessionConfiguration.spec.ts | 1 + .../src/lib/configuration/schema/SessionConfiguration.ts | 5 +++++ test/features/forward-headers.feature | 2 +- test/features/step_definitions/forward-headers.ts | 9 +++++---- 7 files changed, 20 insertions(+), 5 deletions(-) diff --git a/config.template.yml b/config.template.yml index 6457bbf8f..70889e4af 100644 --- a/config.template.yml +++ b/config.template.yml @@ -173,6 +173,9 @@ access_control: # # The session cookies identify the user once logged in. session: + # The name of the session cookie. (default: authelia_session). + name: authelia_session + # The secret to encrypt the session cookie. secret: unsecure_session_secret diff --git a/server/src/lib/configuration/SessionConfigurationBuilder.spec.ts b/server/src/lib/configuration/SessionConfigurationBuilder.spec.ts index 0d5cc11fe..a03507d64 100644 --- a/server/src/lib/configuration/SessionConfigurationBuilder.spec.ts +++ b/server/src/lib/configuration/SessionConfigurationBuilder.spec.ts @@ -39,6 +39,7 @@ describe("configuration/SessionConfigurationBuilder", function () { }, port: 8080, session: { + name: "authelia_session", domain: "example.com", expiration: 3600, secret: "secret" @@ -73,6 +74,7 @@ describe("configuration/SessionConfigurationBuilder", function () { const options = SessionConfigurationBuilder.build(configuration, deps); const expectedOptions = { + name: "authelia_session", secret: "secret", resave: false, saveUninitialized: true, @@ -118,6 +120,7 @@ describe("configuration/SessionConfigurationBuilder", function () { }, port: 8080, session: { + name: "authelia_session", domain: "example.com", expiration: 3600, secret: "secret", @@ -165,6 +168,7 @@ describe("configuration/SessionConfigurationBuilder", function () { secret: "secret", resave: false, saveUninitialized: true, + name: "authelia_session", cookie: { secure: true, httpOnly: true, diff --git a/server/src/lib/configuration/SessionConfigurationBuilder.ts b/server/src/lib/configuration/SessionConfigurationBuilder.ts index 427cec728..716a1f74a 100644 --- a/server/src/lib/configuration/SessionConfigurationBuilder.ts +++ b/server/src/lib/configuration/SessionConfigurationBuilder.ts @@ -7,6 +7,7 @@ export class SessionConfigurationBuilder { static build(configuration: Configuration, deps: GlobalDependencies): ExpressSession.SessionOptions { const sessionOptions: ExpressSession.SessionOptions = { + name: configuration.session.name, secret: configuration.session.secret, resave: false, saveUninitialized: true, diff --git a/server/src/lib/configuration/schema/SessionConfiguration.spec.ts b/server/src/lib/configuration/schema/SessionConfiguration.spec.ts index b63e7eede..e54010837 100644 --- a/server/src/lib/configuration/schema/SessionConfiguration.spec.ts +++ b/server/src/lib/configuration/schema/SessionConfiguration.spec.ts @@ -9,6 +9,7 @@ describe("configuration/schema/SessionConfiguration", function() { }; const newConfiguration = complete(configuration); + Assert.equal(newConfiguration.name, 'authelia_session'); Assert.equal(newConfiguration.expiration, 3600000); Assert.equal(newConfiguration.inactivity, undefined); }); diff --git a/server/src/lib/configuration/schema/SessionConfiguration.ts b/server/src/lib/configuration/schema/SessionConfiguration.ts index e628cb0d1..4b5d555b6 100644 --- a/server/src/lib/configuration/schema/SessionConfiguration.ts +++ b/server/src/lib/configuration/schema/SessionConfiguration.ts @@ -4,6 +4,7 @@ export interface SessionRedisOptions { } export interface SessionConfiguration { + name?: string; domain: string; secret: string; expiration?: number; @@ -14,6 +15,10 @@ export interface SessionConfiguration { export function complete(configuration: SessionConfiguration): SessionConfiguration { const newConfiguration: SessionConfiguration = (configuration) ? JSON.parse(JSON.stringify(configuration)) : {}; + if (!newConfiguration.name) { + newConfiguration.name = "authelia_session"; + } + if (!newConfiguration.expiration) { newConfiguration.expiration = 3600000; // 1 hour } diff --git a/test/features/forward-headers.feature b/test/features/forward-headers.feature index b5bbc0b5c..3e45c1356 100644 --- a/test/features/forward-headers.feature +++ b/test/features/forward-headers.feature @@ -1,4 +1,4 @@ -Feature: User and groups headers are correctly forwarded to backend +Feature: Headers are correctly forwarded to backend @need-authenticated-user-john Scenario: Custom-Forwarded-User and Custom-Forwarded-Groups are correctly forwarded to protected backend When I visit "https://public.example.com:8080/headers" diff --git a/test/features/step_definitions/forward-headers.ts b/test/features/step_definitions/forward-headers.ts index cbefd1a79..a459a4bfa 100644 --- a/test/features/step_definitions/forward-headers.ts +++ b/test/features/step_definitions/forward-headers.ts @@ -2,7 +2,8 @@ import {Then} from "cucumber"; import seleniumWebdriver = require("selenium-webdriver"); import CustomWorld = require("../support/world"); import Util = require("util"); -import BluebirdPromise = require("bluebird"); +import Bluebird = require("bluebird"); +import Request = require("request-promise"); Then("I see header {string} set to {string}", { timeout: 5000 }, @@ -11,8 +12,8 @@ Then("I see header {string} set to {string}", .then(function (txt: string) { const expectedLine = Util.format("\"%s\": \"%s\"", expectedHeaderName, expectedValue); if (txt.indexOf(expectedLine) > 0) - return BluebirdPromise.resolve(); + return Bluebird.resolve(); else - return BluebirdPromise.reject(new Error(Util.format("No such header or with unexpected value."))); + return Bluebird.reject(new Error(Util.format("No such header or with unexpected value."))); }); - }) + }) \ No newline at end of file