Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cookieMaxAge and secureCookies options #1612

Merged
merged 8 commits into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/smooth-pumpkins-deliver/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"releases": [
{ "name": "@keystone-alpha/keystone", "type": "minor" },
{ "name": "@keystone-alpha/session", "type": "minor" }
],
"dependents": []
}
11 changes: 11 additions & 0 deletions .changeset/smooth-pumpkins-deliver/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Adds a `cookieMaxAge` and `secureCookies` option to the keystone constructor.

These will default to 1 day and `true` in production. Or `null` and `false` in other environments.

### Usage
```javascript
const keystone = new Keystone({
cookieMaxAge: 1000 * 60 * 60 * 24 * 7, // 1 week
secureCookies: true,
});
```
13 changes: 11 additions & 2 deletions packages/keystone/lib/Keystone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ module.exports = class Keystone {
onConnect,
cookieSecret = 'qwerty',
sessionStore,
secureCookies = process.env.NODE_ENV === 'production', // Default to true in production
cookieMaxAge = process.env.NODE_ENV === 'production' ? 1000 * 60 * 60 * 24 : null,
MadeByMike marked this conversation as resolved.
Show resolved Hide resolved
}) {
this.name = name;
this.adapterConnectOptions = adapterConnectOptions;
Expand All @@ -57,8 +59,9 @@ module.exports = class Keystone {
this._extendedMutations = [];
this._graphQLQuery = {};
this._cookieSecret = cookieSecret;
this._secureCookies = secureCookies;
this._cookieMaxAge = cookieMaxAge;
this._sessionStore = sessionStore;
this.registeredTypes = new Set();
this.eventHandlers = { onConnect };

if (adapters) {
Expand Down Expand Up @@ -544,7 +547,13 @@ module.exports = class Keystone {
// Used by other middlewares such as authentication strategies. Important
// to be first so the methods added to `req` are available further down
// the request pipeline.
commonSessionMiddleware(this, this._cookieSecret, this._sessionStore),
commonSessionMiddleware({
MadeByMike marked this conversation as resolved.
Show resolved Hide resolved
keystone: this,
cookieSecret: this._cookieSecret,
sessionStore: this.sessionStore,
secureCookies: this._secureCookies,
cookieMaxAge: this._cookieMaxAge,
}),
...(await Promise.all(
[
// Inject any field middlewares (eg; WYSIWIG's static assets)
Expand Down
10 changes: 9 additions & 1 deletion packages/session/lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ const cookieSignature = require('cookie-signature');
const expressSession = require('express-session');
const cookie = require('cookie');

const commonSessionMiddleware = (keystone, cookieSecret, sessionStore) => {
const commonSessionMiddleware = ({
keystone,
cookieSecret,
sessionStore,
secureCookies,
cookieMaxAge,
}) => {
const COOKIE_NAME = 'keystone.sid';

// We have at least one auth strategy
Expand Down Expand Up @@ -50,7 +56,9 @@ const commonSessionMiddleware = (keystone, cookieSecret, sessionStore) => {
resave: false,
saveUninitialized: false,
name: COOKIE_NAME,
cookie: { secure: secureCookies },
store: sessionStore,
maxAge: cookieMaxAge,
});

return [injectAuthCookieMiddleware, sessionMiddleware, populateAuthedItemMiddleware(keystone)];
Expand Down