Skip to content
Open
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
11 changes: 11 additions & 0 deletions lib/extract_jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ extractors.fromAuthHeaderAsBearerToken = function () {
};


extractors.fromSignedCookie = function (cookie_name) {
return function (request) {
var token = null;
if (request.signedCookies && request.signedCookies[cookie_name]) {
token = request.signedCookies[cookie_name];
}
return token;
}
};


extractors.fromExtractors = function(extractors) {
if (!Array.isArray(extractors)) {
throw new TypeError('extractors.fromExtractors expects an array')
Expand Down
33 changes: 33 additions & 0 deletions test/extractors-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,39 @@ describe('Token extractor', function() {
});


});

describe('fromSignedCookie', function() {

var extractor = extract_jwt.fromSignedCookie('token');

it('should return the value from signed cookie', function() {
var req = new Request()
req.signedCookies['token'] = 'abcd123';

var token = extractor(req);

expect(token).to.equal('abcd123');
});

it('should return null if no signed cookie is present', function() {
var req = new Request()

var token = extractor(req);

expect(token).to.equal.null;
});

it('should return null if specified cookie name not present but other signed cookies are', function() {
var req = new Request()
req.signedCookies['othertoken'] = 'abcd123';

var token = extractor(req);

expect(token).to.equal.null;
});


});

describe('fromExtractors', function() {
Expand Down
1 change: 1 addition & 0 deletions test/mock_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ function Request() {
this.method = 'GET';
this.url = '/';
this.headers = {};
this.signedCookies = {};
}

module.exports = Request;