-
-
Notifications
You must be signed in to change notification settings - Fork 153
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
1.0.0 #48
Open
jonathanong
wants to merge
2
commits into
master
Choose a base branch
from
rewrite
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
1.0.0 #48
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
node_modules | ||
.DS_Store* | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
module.exports = Cookie | ||
|
||
function Cookie(name, value, attrs) { | ||
value || (this.expires = new Date(0)) | ||
|
||
this.name = name | ||
this.value = value || "" | ||
|
||
for (var name in attrs) this[name] = attrs[name] | ||
} | ||
|
||
Cookie.prototype = { | ||
path: "/", | ||
expires: undefined, | ||
domain: undefined, | ||
httpOnly: true, | ||
secure: false, | ||
overwrite: true, | ||
|
||
toString: function() { | ||
return this.name + "=" + this.value | ||
}, | ||
|
||
toHeader: function() { | ||
var header = this.toString() | ||
|
||
if (this.maxage) this.expires = new Date(Date.now() + this.maxage); | ||
|
||
if (this.path ) header += "; path=" + this.path | ||
if (this.expires ) header += "; expires=" + this.expires.toUTCString() | ||
if (this.domain ) header += "; domain=" + this.domain | ||
if (this.secure ) header += "; secure" | ||
if (this.httpOnly ) header += "; httponly" | ||
|
||
return header | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,143 +1,148 @@ | ||
|
||
var base64 = require('base64-url') | ||
var Keygrip = require('keygrip') | ||
var http = require('http') | ||
var cache = {} | ||
|
||
function Cookies(request, response, keys) { | ||
if (!(this instanceof Cookies)) return new Cookies(request, response, keys) | ||
|
||
this.request = request | ||
this.response = response | ||
if (keys) { | ||
// array of key strings | ||
if (Array.isArray(keys)) | ||
this.keys = new Keygrip(keys) | ||
// any keygrip constructor to allow different versions | ||
else if (keys.constructor && keys.constructor.name === 'Keygrip') | ||
this.keys = keys | ||
} | ||
} | ||
|
||
Cookies.prototype = { | ||
get: function(name, opts) { | ||
var sigName = name + ".sig" | ||
, header, match, value, remote, data, index | ||
, signed = opts && opts.signed !== undefined ? opts.signed : !!this.keys | ||
var Cookie = require('./cookie') | ||
var utils = require('./utils') | ||
|
||
header = this.request.headers["cookie"] | ||
if (!header) return | ||
var getPattern = utils.getPattern | ||
var pushCookie = utils.pushCookie | ||
var merge = utils.merge | ||
|
||
match = header.match(getPattern(name)) | ||
if (!match) return | ||
module.exports = function (options, keys) { | ||
if (Array.isArray(options) | ||
|| (options && options.constructor && options.constructor.name === 'Keygrip')) { | ||
keys = options | ||
options = {} | ||
} | ||
|
||
value = match[1] | ||
if (!opts || !signed) return value | ||
options = options || {} | ||
keys = keys || options.keys | ||
if (Array.isArray(keys)) keys = new Keygrip(keys) | ||
|
||
remote = this.get(sigName) | ||
if (!remote) return | ||
function Cookies(req, res, next) { | ||
if (!(this instanceof Cookies)) return new Cookies(req, res, next) | ||
|
||
data = name + "=" + value | ||
if (!this.keys) throw new Error('.keys required for signed cookies'); | ||
index = this.keys.index(data, remote) | ||
this.req = req | ||
this.res = res | ||
|
||
if (index < 0) { | ||
this.set(sigName, null, {path: "/", signed: false }) | ||
} else { | ||
index && this.set(sigName, this.keys.sign(data), { signed: false }) | ||
return value | ||
// middleware support | ||
if (typeof next === 'function') { | ||
req.cookies = res.cookies = this | ||
next() | ||
} | ||
}, | ||
} | ||
|
||
Cookies.prototype.get = function (name) { | ||
var header = this.req.headers.cookie | ||
if (!header) return | ||
var match = header.match(getPattern(name)) | ||
if (!match) return | ||
return match[1] | ||
} | ||
|
||
set: function(name, value, opts) { | ||
var res = this.response | ||
, req = this.request | ||
, headers = res.getHeader("Set-Cookie") || [] | ||
, secure = req.connection.encrypted | ||
, cookie = new Cookie(name, value, opts) | ||
, signed = opts && opts.signed !== undefined ? opts.signed : !!this.keys | ||
Cookies.prototype.set = function (name, value, opts) { | ||
opts = this.extend(opts) | ||
|
||
var res = this.res | ||
var headers = res.getHeader('Set-Cookie') || [] | ||
// node.js header sillyness | ||
if (typeof headers == "string") headers = [headers] | ||
|
||
if (!secure && opts && opts.secure) throw new Error("Cannot send secure cookie over unencrypted socket") | ||
|
||
cookie.secure = secure | ||
if (opts && "secure" in opts) cookie.secure = opts.secure | ||
if (opts && "secureProxy" in opts) cookie.secure = opts.secureProxy | ||
var cookie = new Cookie(name, value, opts) | ||
headers = pushCookie(headers, cookie) | ||
|
||
if (opts && signed) { | ||
if (!this.keys) throw new Error('.keys required for signed cookies'); | ||
cookie.value = this.keys.sign(cookie.toString()) | ||
cookie.name += ".sig" | ||
headers = pushCookie(headers, cookie) | ||
} | ||
res.setHeader('Set-Cookie', headers) | ||
return this | ||
} | ||
|
||
Cookies.prototype.decode = function (name, buffer) { | ||
var value = this.get(name) | ||
if (!value) return | ||
|
||
var buf = new Buffer(base64.unescape(value), 'base64') | ||
if (buffer) return buf | ||
return buf.toString('utf8') | ||
} | ||
|
||
var setHeader = res.set ? http.OutgoingMessage.prototype.setHeader : res.setHeader | ||
setHeader.call(res, 'Set-Cookie', headers) | ||
Cookies.prototype.encode = function (name, value, opts) { | ||
opts = this.extend(opts) | ||
|
||
var digest = base64.escape(new Buffer(value, 'utf8').toString('base64')) | ||
this.set(name, digest, opts) | ||
return this | ||
} | ||
} | ||
|
||
function Cookie(name, value, attrs) { | ||
value || (this.expires = new Date(0)) | ||
Cookies.prototype.unsign = function (name, opts) { | ||
opts = this.extend(opts) | ||
assertKeys() | ||
|
||
this.name = name | ||
this.value = value || "" | ||
var value = opts.encoded | ||
? this.decode(name + '.b64') | ||
: this.get(name) | ||
if (!value) return | ||
|
||
for (var name in attrs) this[name] = attrs[name] | ||
} | ||
var remote = this.decode(name + '.sig', true) | ||
if (!remote) return // no signature | ||
|
||
Cookie.prototype = { | ||
path: "/", | ||
expires: undefined, | ||
domain: undefined, | ||
httpOnly: true, | ||
secure: false, | ||
overwrite: false, | ||
var data = name + '=' + value | ||
var index = keys.index(data, remote) | ||
|
||
toString: function() { | ||
return this.name + "=" + this.value | ||
}, | ||
// invalid signature, so we clear it | ||
if (index < 0) return this.clear(name + '.sig', opts) | ||
|
||
// update the signature to the latest key | ||
// to do: update the original cookie as well | ||
if (index > 0) this.encode(name + '.sig', data, opts) | ||
return value | ||
} | ||
|
||
Cookies.prototype.sign = function (name, value, opts) { | ||
opts = this.extend(opts) | ||
assertKeys() | ||
|
||
if (opts.encoded) this.encode(name + '.b64', value, opts) | ||
else this.set(name, value, opts) | ||
this.encode(name + '.sig', keys.sign(name + '=' + value), opts) | ||
return this | ||
} | ||
|
||
toHeader: function() { | ||
var header = this.toString() | ||
Cookies.prototype.decrypt = function (name, opts) { | ||
opts = this.extend(opts) | ||
assertKeys() | ||
|
||
if (this.maxage) this.expires = new Date(Date.now() + this.maxage); | ||
var value = this.decode(name + '.enc', true) | ||
if (!value) return | ||
|
||
if (this.path ) header += "; path=" + this.path | ||
if (this.expires ) header += "; expires=" + this.expires.toUTCString() | ||
if (this.domain ) header += "; domain=" + this.domain | ||
if (this.secure ) header += "; secure" | ||
if (this.httpOnly ) header += "; httponly" | ||
var msg = keys.decrypt(value) | ||
if (!msg) return // bad decryption | ||
|
||
return header | ||
// re-encrypt if not using the latest key | ||
if (msg[1] > 0) this.encrypt(name, msg[0], opts) | ||
return msg[0].toString('utf8') | ||
} | ||
} | ||
|
||
function getPattern(name) { | ||
if (cache[name]) return cache[name] | ||
Cookies.prototype.encrypt = function (name, value, opts) { | ||
opts = this.extend(opts) | ||
assertKeys() | ||
|
||
return cache[name] = new RegExp( | ||
"(?:^|;) *" + | ||
name.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&") + | ||
"=([^;]*)" | ||
) | ||
} | ||
this.encode(name + '.enc', keys.encrypt(value), opts) | ||
return this | ||
} | ||
|
||
function pushCookie(cookies, cookie) { | ||
if (cookie.overwrite) { | ||
cookies = cookies.filter(function(c) { return c.indexOf(cookie.name+'=') !== 0 }) | ||
// clear a cookie | ||
Cookies.prototype.clear = function (name, opts) { | ||
this.set(name, null, opts) | ||
} | ||
cookies.push(cookie.toHeader()) | ||
return cookies | ||
} | ||
|
||
Cookies.connect = Cookies.express = function(keys) { | ||
return function(req, res, next) { | ||
req.cookies = res.cookies = new Cookies(req, res, keys) | ||
next() | ||
// inherit the options from the main options | ||
Cookies.prototype.extend = function (opts) { | ||
return merge(Object.create(options), opts || {}) | ||
} | ||
} | ||
|
||
Cookies.Cookie = Cookie | ||
return Cookies | ||
|
||
module.exports = Cookies | ||
function assertKeys() { | ||
if (!keys) throw new Error('.keys required for signed cookies') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
var cache = {} | ||
exports.getPattern = function getPattern(name) { | ||
if (cache[name]) return cache[name] | ||
|
||
return cache[name] = new RegExp( | ||
"(?:^|;) *" + | ||
name.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&") + | ||
"=([^;]*)" | ||
) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
exports.pushCookie = function pushCookie(cookies, cookie) { | ||
if (cookie.overwrite) { | ||
cookies = cookies.filter(function(c) { | ||
return c.indexOf(cookie.name+'=') !== 0 | ||
}) | ||
} | ||
cookies.push(cookie.toHeader()) | ||
return cookies | ||
} | ||
|
||
exports.merge = function merge(dest, src) { | ||
for (var name in src) dest[name] = src[name] | ||
return dest | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
describe('Base64 Encoded Cookies', function () { | ||
describe('Getting/Setting', function () { | ||
var string = "something \n really \n crazy" | ||
var cookie | ||
|
||
before(function () { | ||
serve(function (req, res) { | ||
var cookies = Cookies(req, res) | ||
|
||
if (req.method === 'POST') { | ||
cookies.encode('unsigned', string) | ||
} else { | ||
res.end(cookies.decode('unsigned')) | ||
} | ||
|
||
res.end() | ||
}) | ||
}) | ||
|
||
it('should set an encoded cookie', function (done) { | ||
request(server) | ||
.post('/') | ||
.expect('Set-Cookie', /^unsigned=[\w-]+; path=\/; httponly$/) | ||
.expect(200, function (err, res) { | ||
if (err) return done(err) | ||
|
||
cookie = res.headers['set-cookie'][0] | ||
done() | ||
}) | ||
}) | ||
|
||
it('should get an encoded cookie', function (done) { | ||
request(server) | ||
.get('/') | ||
.set('Cookie', cookie) | ||
.expect(200) | ||
.expect(string, done) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this extend thing would be a little slow to use on each method, but perhaps that's not an issue.