Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 10 additions & 3 deletions lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const cookieOptsKey = Symbol('cookieOpts')
const originalHash = Symbol('originalHash')
const hash = Symbol('hash')
const sessionIdKey = Symbol('sessionId')
const encryptedSessionIdKey = Symbol('encryptedSessionId')

module.exports = class Session {
constructor (request, idGenerator, cookieOpts, cookieSigner, prevSession, sessionId = idGenerator(request)) {
Expand All @@ -24,6 +25,11 @@ module.exports = class Session {
this[cookieSignerKey] = cookieSigner
this[requestKey] = request
this[sessionIdKey] = sessionId
this[encryptedSessionIdKey] = (
prevSession &&
prevSession[sessionIdKey] === sessionId &&
prevSession[encryptedSessionIdKey]
) || cookieSigner.sign(this.sessionId)
Comment thread
Uzlopak marked this conversation as resolved.
this.cookie = new Cookie((prevSession && prevSession.cookie) || cookieOpts)

if (prevSession) {
Expand All @@ -34,9 +40,6 @@ module.exports = class Session {
}

this.touch()
if (!this.encryptedSessionId) {
this.encryptedSessionId = cookieSigner.sign(this.sessionId)
}
this[originalHash] = this[hash]()
}

Expand Down Expand Up @@ -146,6 +149,10 @@ module.exports = class Session {
return this[sessionIdKey]
}

get encryptedSessionId () {
return this[encryptedSessionIdKey]
}

[hash] () {
const sess = this
const str = stringify(sess, function (key, val) {
Expand Down
12 changes: 12 additions & 0 deletions test/TestStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

const { MemoryStore } = require('../lib/store')

class TestStore extends MemoryStore {
set (sessionId, session, callback) {
this.store.set(sessionId, JSON.parse(JSON.stringify(session)))
callback()
}
}

module.exports = TestStore
22 changes: 14 additions & 8 deletions test/base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const test = require('tap').test
const Signer = require('@fastify/cookie').Signer
const fastifyPlugin = require('fastify-plugin')
const { DEFAULT_OPTIONS, DEFAULT_COOKIE, DEFAULT_SESSION_ID, DEFAULT_SECRET, DEFAULT_ENCRYPTED_SESSION_ID, buildFastify } = require('./util')
const Store = require('../lib/fastifySession').Store
const TestStore = require('./TestStore')

test('should not set session cookie on post without params', async (t) => {
t.plan(3)
Expand All @@ -22,8 +22,8 @@ test('should not set session cookie on post without params', async (t) => {
})

test('should save the session properly', async (t) => {
t.plan(6)
const store = new Store()
t.plan(11)
const store = new TestStore()
const fastify = await buildFastify((request, reply) => {
request.session.test = true

Expand All @@ -35,11 +35,17 @@ test('should save the session properly', async (t) => {
const session = [...storeMap.entries()][0][1]
const keys = Object.keys(session)

// Only storing three keys: cookie, encryptedSessionId and test
t.equal(keys.length, 3)
// Only storing two keys: cookie and test
t.equal(keys.length, 2)
t.ok(keys.includes('cookie'))
t.ok(keys.includes('test'))
t.not(keys.includes('sessionId'))
t.not(keys.includes('encryptedSessionId'))

t.ok(session.cookie)
t.ok(session.encryptedSessionId)
Comment thread
Uzlopak marked this conversation as resolved.
t.equal(session.test, true)
t.equal(session.sessionId, undefined)
t.equal(session.encryptedSessionId, undefined)
})
reply.send()
}, { ...DEFAULT_OPTIONS, store })
Expand Down Expand Up @@ -87,7 +93,7 @@ test('should support multiple secrets', async (t) => {
const sessionIdSignedWithNewSecret = sign(sessionId, newSecret)

const storeMap = new Map()
const store = new Store(storeMap)
const store = new TestStore(storeMap)

storeMap.set(sessionId, {
test: 0,
Expand Down Expand Up @@ -129,7 +135,7 @@ test('should support multiple secrets', async (t) => {
cookie: `sessionId=${sessionIdSignedWithNewSecret}; Path=/; HttpOnly; Secure`
}
})
t.not(storeMap.get(sessionId).sessionId)
t.equal(storeMap.get(sessionId).sessionId, undefined)
t.equal(storeMap.get(sessionId).test, 2)
t.equal(response2.statusCode, 200)
t.equal(response2.headers['set-cookie'].includes(sessionId), true)
Expand Down
3 changes: 2 additions & 1 deletion test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const Fastify = require('fastify')
const fastifyCookie = require('@fastify/cookie')
const fastifySession = require('../lib/fastifySession')
const TestStore = require('./TestStore')

const DEFAULT_SECRET = 'cNaoPYAwF60HZJzkcNaoPYAwF60HZJzk'
const DEFAULT_OPTIONS = { secret: DEFAULT_SECRET }
Expand All @@ -17,7 +18,7 @@ async function buildFastify (handler, sessionOptions, plugin) {
if (plugin) {
await fastify.register(plugin)
}
await fastify.register(fastifySession, sessionOptions)
await fastify.register(fastifySession, { store: new TestStore(), ...sessionOptions })

fastify.get('/', handler)
await fastify.listen({ port: 0 })
Expand Down