Skip to content
This repository has been archived by the owner on Jul 5, 2020. It is now read-only.

Commit

Permalink
feat(cookie): decouple cookie singing and encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Aug 1, 2017
1 parent 40e925b commit 2554511
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions src/Cookie/index.js
Expand Up @@ -313,6 +313,33 @@ Cookie.get = function (req, key, secret = null, decrypt = false, cookies = null)
return cookie ? Cookie._parseJSON(cookie) : null
}

/**
* Pack the cookie value by properly formatting,
* signing and encrypting it
*
* @method packValue
*
* @param {String} value
* @param {String} secret
* @param {Boolean} encrypt
*
* @return {String}
*/
Cookie.packValue = function (value, secret, encrypt) {
value = Cookie._stringifyJSON(value)
value = Cookie._signValue(value, secret)

/**
* Encrypt the cookie value only when secret is defined
* and encrypt is set to true
*/
if (secret && encrypt) {
value = Cookie._encrypt(value, secret)
}

return String(value)
}

/**
* Write cookie to the HTTP response object. It will append
* duplicate cookies to the `Set-Cookie` header, since
Expand Down Expand Up @@ -341,18 +368,7 @@ Cookie.get = function (req, key, secret = null, decrypt = false, cookies = null)
* ```
*/
Cookie.create = function (res, key, value, options = {}, secret = null, encrypt = false) {
value = Cookie._stringifyJSON(value)
value = Cookie._signValue(value, secret)

/**
* Encrypt the cookie value only when secret is defined
* and encrypt is set to true
*/
if (secret && encrypt) {
value = Cookie._encrypt(value, secret)
}

const cookie = parser.serialize(key, String(value), options)
const cookie = parser.serialize(key, Cookie.packValue(value, secret, encrypt), options)
Cookie._append(res, key, cookie)
}

Expand Down

0 comments on commit 2554511

Please sign in to comment.