Skip to content

Commit

Permalink
feat: Adds an env variable for changing the cookie domain. (#1356)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyHubert authored and ryasmi committed Sep 6, 2019
1 parent f13011c commit 77c383a
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ FS_REPO=local
########
#RESTRICT_CREATE_ORGANISATION=true

#COOKIE_DOMAIN=

# Location of virus scanning binary (ClamAV - https://www.clamav.net/)
#CLAMDSCAN_BINARY=/usr/bin/clamdscan
#CLAMDSCAN_CONF=/etc/clamav/clamd.conf
Expand Down
3 changes: 3 additions & 0 deletions lib/tools/getWebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ function getWebpackConfig(args) {
? '"development"'
: '"production"',
'process.env.BROWSER': true,
'process.env.COOKIE_DOMAIN': process.env.COOKIE_DOMAIN
? `"${process.env.COOKIE_DOMAIN}"`
: false,
__CLIENT__: true,
__SERVER__: false,
__DEVELOPMENT__: isDebug,
Expand Down
4 changes: 2 additions & 2 deletions ui/src/redux/modules/auth/logout.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { actions as routerActions } from 'redux-router5';
import { Map } from 'immutable';
import Cookies from 'js-cookie';
import { each, pickBy } from 'lodash';
import { testCookieName } from 'ui/utils/auth';
import { testCookieName, getCookieOptions } from 'ui/utils/auth';

export const LOGOUT = 'learninglocker/auth/LOGOUT';

Expand All @@ -22,7 +22,7 @@ function* logoutSaga() {
try {
const cookies = Cookies.get();
const filteredCookies = pickBy(cookies, (value, cookieName) => testCookieName(cookieName));
each(filteredCookies, (value, name) => Cookies.remove(name));
each(filteredCookies, (value, name) => Cookies.remove(name, getCookieOptions()));
yield put(routerActions.navigateTo('login'));
} catch (err) {
console.error(err);
Expand Down
4 changes: 2 additions & 2 deletions ui/src/redux/modules/auth/orgLogout.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { takeEvery, put } from 'redux-saga/effects';
import { actions as routerActions } from 'redux-router5';
import Cookies from 'js-cookie';
import { each, pickBy } from 'lodash';
import { testOrgCookieName } from 'ui/utils/auth';
import { testOrgCookieName, getCookieOptions } from 'ui/utils/auth';

export const ORG_LOGOUT = 'learninglocker/auth/ORG_LOGOUT';

Expand All @@ -27,7 +27,7 @@ function* orgLogoutSaga({ organisationId }) {
const cookies = Cookies.get();
const testCookie = testOrgCookieName(organisationId);
const filteredCookies = pickBy(cookies, (value, cookieName) => testCookie(cookieName));
each(filteredCookies, (value, name) => Cookies.remove(name));
each(filteredCookies, (value, name) => Cookies.remove(name, getCookieOptions()));
yield put(routerActions.navigateTo('home'));
} catch (err) {
throw new Error('Failed to remove auth cookies from browser storage');
Expand Down
4 changes: 2 additions & 2 deletions ui/src/redux/modules/auth/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Cookies from 'js-cookie';
import jwtDecode from 'jwt-decode';
import { fromJS, Map } from 'immutable';
import { takeEvery } from 'redux-saga/effects';
import { getCookieName, getCookieNameStartsWith } from 'ui/utils/auth';
import { getCookieName, getCookieNameStartsWith, getCookieOptions } from 'ui/utils/auth';

/**
* takes tokens received from the auth process
Expand Down Expand Up @@ -117,7 +117,7 @@ function storeTokenSaga({ token, tokenType, tokenId }) {
try {
if (token) {
const cookieName = getCookieName({ tokenType, tokenId });
Cookies.set(cookieName, token);
Cookies.set(cookieName, token, getCookieOptions());
}
} catch (err) {
console.error(err);
Expand Down
11 changes: 11 additions & 0 deletions ui/src/utils/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ const getCookieNameStartsWith = ({ tokenType }, cookies) => {
return key;
};

const getCookieOptions = () => {
const cookieOptions = {};
if (process.env.COOKIE_DOMAIN) {
cookieOptions.domain = process.env.COOKIE_DOMAIN
.replace(/HOSTNAME/, location.hostname);
}

return cookieOptions;
};

/**
* Tests whether the given cookie name is any auth cookie
*/
Expand All @@ -35,6 +45,7 @@ const testOrgCookieName = organisationId => (cookieName) => {
export {
getCookieName,
getCookieNameStartsWith,
getCookieOptions,
testCookieName,
testOrgCookieName
};

0 comments on commit 77c383a

Please sign in to comment.