diff --git a/.env.sample b/.env.sample index badbb5f2..517b164f 100644 --- a/.env.sample +++ b/.env.sample @@ -19,7 +19,6 @@ FORCE_HTTPS=true DEPLOYMENT=self ENABLE_UPDATES=true -SUBDOMAINS_ENABLED=false WEBSOCKETS_ENABLED=true DEBUG=cache,presenters,events diff --git a/app.json b/app.json index feeea109..66ce6a04 100644 --- a/app.json +++ b/app.json @@ -44,11 +44,6 @@ "value": "true", "required": true }, - "SUBDOMAINS_ENABLED": { - "value": "false", - "required": true, - "description": "Allows each team to have a different subdomain. Not recommend when self hosting" - }, "WEBSOCKETS_ENABLED": { "value": "true", "required": true, diff --git a/app/stores/AuthStore.js b/app/stores/AuthStore.js index 55516c28..6586b290 100644 --- a/app/stores/AuthStore.js +++ b/app/stores/AuthStore.js @@ -3,7 +3,7 @@ import { observable, action, computed, autorun, runInAction } from 'mobx'; import invariant from 'invariant'; import { getCookie, setCookie, removeCookie } from 'tiny-cookie'; import { client } from 'utils/ApiClient'; -import { stripSubdomain } from 'shared/utils/domains'; +import { getCookieDomain } from 'shared/utils/domains'; import RootStore from 'stores/RootStore'; import User from 'models/User'; import Team from 'models/Team'; @@ -174,7 +174,7 @@ export default class AuthStore { delete sessions[team.id]; setCookie('sessions', JSON.stringify(sessions), { - domain: stripSubdomain(window.location.hostname), + domain: getCookieDomain(window.location.hostname), }); this.team = null; } diff --git a/server/auth/index.js b/server/auth/index.js index ad5e5fe3..e1fc6028 100644 --- a/server/auth/index.js +++ b/server/auth/index.js @@ -6,7 +6,7 @@ import validation from '../middlewares/validation'; import auth from '../middlewares/authentication'; import addMonths from 'date-fns/add_months'; import { Team } from '../models'; -import { stripSubdomain } from '../../shared/utils/domains'; +import { getCookieDomain } from '../../shared/utils/domains'; import slack from './slack'; import google from './google'; @@ -25,7 +25,7 @@ router.get('/redirect', auth(), async ctx => { // transfer access token cookie from root to subdomain ctx.cookies.set('accessToken', undefined, { httpOnly: true, - domain: stripSubdomain(ctx.request.hostname), + domain: getCookieDomain(ctx.request.hostname), }); ctx.cookies.set('accessToken', user.getJwtToken(), { diff --git a/server/auth/slack.js b/server/auth/slack.js index 9f46857f..b2f00203 100644 --- a/server/auth/slack.js +++ b/server/auth/slack.js @@ -3,7 +3,7 @@ import Sequelize from 'sequelize'; import Router from 'koa-router'; import auth from '../middlewares/authentication'; import addHours from 'date-fns/add_hours'; -import { stripSubdomain } from '../../shared/utils/domains'; +import { getCookieDomain } from '../../shared/utils/domains'; import { slackAuth } from '../../shared/utils/routeHelpers'; import { Authentication, @@ -27,7 +27,7 @@ router.get('slack', async ctx => { ctx.cookies.set('state', state, { httpOnly: false, expires: addHours(new Date(), 1), - domain: stripSubdomain(ctx.request.hostname), + domain: getCookieDomain(ctx.request.hostname), }); ctx.redirect(slackAuth(state)); }); diff --git a/server/middlewares/authentication.js b/server/middlewares/authentication.js index 027849ed..cbc7cca6 100644 --- a/server/middlewares/authentication.js +++ b/server/middlewares/authentication.js @@ -6,7 +6,7 @@ import { getUserForJWT } from '../utils/jwt'; import { AuthenticationError, UserSuspendedError } from '../errors'; import addMonths from 'date-fns/add_months'; import addMinutes from 'date-fns/add_minutes'; -import { stripSubdomain } from '../../shared/utils/domains'; +import { getCookieDomain } from '../../shared/utils/domains'; export default function auth(options?: { required?: boolean } = {}) { return async function authMiddleware(ctx: Context, next: () => Promise<*>) { @@ -89,7 +89,7 @@ export default function auth(options?: { required?: boolean } = {}) { // update the database when the user last signed in user.updateSignedIn(ctx.request.ip); - const domain = stripSubdomain(ctx.request.hostname); + const domain = getCookieDomain(ctx.request.hostname); const expires = addMonths(new Date(), 3); // set a cookie for which service we last signed in with. This is diff --git a/shared/utils/domains.js b/shared/utils/domains.js index 7e47ed59..7e039400 100644 --- a/shared/utils/domains.js +++ b/shared/utils/domains.js @@ -42,6 +42,14 @@ export function parseDomain(url: string): ?Domain { return null; } +export function getCookieDomain(domain: string) { + // TODO: All the process.env parsing needs centralizing + return process.env.SUBDOMAINS_ENABLED === 'true' || + process.env.SUBDOMAINS_ENABLED === true + ? stripSubdomain(domain) + : domain; +} + export function stripSubdomain(hostname: string) { const parsed = parseDomain(hostname); if (!parsed) return hostname;