Skip to content

Commit

Permalink
Merge pull request #108 from manonthemat/master
Browse files Browse the repository at this point in the history
linting
  • Loading branch information
nelsonic committed Oct 10, 2015
2 parents efbdc43 + 1802a55 commit d2eff25
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 150 deletions.
4 changes: 2 additions & 2 deletions example/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ server.register(hapiAuthJWT, function (err) {
}
// see: http://hapijs.com/api#serverauthschemename-scheme
server.auth.strategy('jwt', 'jwt',
{ key: secret, validateFunc: validate,
{ key: secret, validateFunc: validate,
verifyOptions: { ignoreExpiration: true }
});

server.auth.default('jwt');

server.route([
Expand Down
2 changes: 1 addition & 1 deletion example/simple_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ server.register(require('../lib'), function (err) {
{ key: 'NeverShareYourSecret', // Never Share your secret key
validateFunc: validate // validate function defined above
});

server.auth.default('jwt');

server.route([
Expand Down
3 changes: 1 addition & 2 deletions lib/extract.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var Cookie = require('cookie'); // highly popular decoupled cookie parser
var Boom = require('boom'); // error handling https://github.com/hapijs/boom

/**
* Extract the JWT from URL, Auth Header or Cookie
Expand Down Expand Up @@ -28,7 +27,7 @@ module.exports = function (request, options) {
}

// strip pointless "Bearer " label & any whitespace > http://git.io/xP4F
return auth ? auth.replace(/Bearer/gi,'').replace(/ /g,'') : null;
return auth ? auth.replace(/Bearer/gi, '').replace(/ /g, '') : null;
};

module.exports.isValid = function basicChecks (token) {
Expand Down
46 changes: 23 additions & 23 deletions test/cookies-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var test = require('tape');
var JWT = require('jsonwebtoken');
var secret = 'NeverShareYourSecret';
var server = require('./server.js');
var cookie_options = '; Max-Age=31536000;' //' Expires=Mon, 18 Jul 2016 05:29:45 GMT; Secure; HttpOnly';
var cookie_options = '; Max-Age=31536000;'; //' Expires=Mon, 18 Jul 2016 05:29:45 GMT; Secure; HttpOnly';

// var cookie_options = {
// ttl: 365 * 30 * 7 * 24 * 60 * 60 * 1000, // in the distant future ...
Expand All @@ -14,11 +14,11 @@ var cookie_options = '; Max-Age=31536000;' //' Expires=Mon, 18 Jul 2016 05:29:45
// }

test("Attempt to access restricted content using inVALID Cookie Token", function(t) {
var token = JWT.sign({ id:123,"name":"Charlie" }, 'badsecret');
var token = JWT.sign({ id: 123, "name": "Charlie" }, 'badsecret');
var options = {
method: "POST",
url: "/privado",
headers: { cookie : "token=" + token}
headers: { cookie: "token=" + token }
};
console.log(options);
server.inject(options, function(response) {
Expand All @@ -28,11 +28,11 @@ test("Attempt to access restricted content using inVALID Cookie Token", function
});

test("Attempt to access restricted content with VALID Token but malformed Cookie", function(t) {
var token = JWT.sign({ id:123,"name":"Charlie" }, secret);
var token = JWT.sign({ id: 123, "name": "Charlie" }, secret);
var options = {
method: "POST",
url: "/privado",
headers: { cookie : token }
headers: { cookie: token }
};
// server.inject lets us similate an http request
server.inject(options, function(response) {
Expand All @@ -42,11 +42,11 @@ test("Attempt to access restricted content with VALID Token but malformed Cookie
});

test("Access restricted content with VALID Token Cookie", function(t) {
var token = JWT.sign({ id:123,"name":"Charlie" }, secret);
var token = JWT.sign({ id: 123, "name": "Charlie" }, secret);
var options = {
method: "POST",
url: "/privado",
headers: { cookie : "token=" + token }
headers: { cookie: "token=" + token }
};
// server.inject lets us similate an http request
server.inject(options, function(response) {
Expand All @@ -56,11 +56,11 @@ test("Access restricted content with VALID Token Cookie", function(t) {
});

test("Access restricted content with VALID Token Cookie (With Options!)", function(t) {
var token = JWT.sign({ id:123,"name":"Charlie" }, secret);
var token = JWT.sign({ id: 123, "name": "Charlie" }, secret);
var options = {
method: "POST",
url: "/privado",
headers: { cookie : "token=" + token + cookie_options }
headers: { cookie: "token=" + token + cookie_options }
};
// console.log(' - - - - - - - - - - - - - - - OPTIONS:')
// console.log(options);
Expand All @@ -78,13 +78,13 @@ test("Access restricted content with VALID Token Cookie (With Options!)", functi
// supply valid Token Auth Header but invalid Cookie
// should succeed because Auth Header is first
test("Authorization Header should take precedence over any cookie", function(t) {
var token = JWT.sign({ id:123,"name":"Charlie" }, secret);
var token = JWT.sign({ id: 123, "name": "Charlie" }, secret);
var options = {
method: "POST",
url: "/privado",
headers: {
authorization: "Bearer "+token,
cookie : "token=malformed.token" + cookie_options
authorization: "Bearer " + token,
cookie: "token=malformed.token" + cookie_options
}
};
server.inject(options, function(response) {
Expand All @@ -98,14 +98,14 @@ test("Authorization Header should take precedence over any cookie", function(t)
// valid google analytics cookie but invalid auth header token
// see: https://github.com/dwyl/hapi-auth-jwt2/issues/65#issuecomment-124791842
test("Valid Google Analytics cookie should be ignored", function(t) {
var GA = "gwcm=%7B%22expires%22%3Anull%2C%22clabel%22%3A%22SbNVCILRtFcQwcrE6gM%22%2C%22backoff%22%3A1437241242%7D; _ga=GA1.2.1363734468.1432273334"
var token = JWT.sign({ id:123,"name":"Charlie" }, secret);
var GA = "gwcm=%7B%22expires%22%3Anull%2C%22clabel%22%3A%22SbNVCILRtFcQwcrE6gM%22%2C%22backoff%22%3A1437241242%7D; _ga=GA1.2.1363734468.1432273334";
var token = JWT.sign({ id: 123, "name": "Charlie" }, secret);
var options = {
method: "POST",
url: "/privado",
headers: {
authorization: "Bearer "+token,
cookie : GA
authorization: "Bearer " + token,
cookie: GA
}
};
server.inject(options, function(response) {
Expand All @@ -115,14 +115,14 @@ test("Valid Google Analytics cookie should be ignored", function(t) {
});

test("Valid Google Analytics cookie should be ignored (BAD Header Token)", function(t) {
var GA = "gwcm=%7B%22expires%22%3Anull%2C%22clabel%22%3A%22SbNVCILRtFcQwcrE6gM%22%2C%22backoff%22%3A1437241242%7D; _ga=GA1.2.1363734468.1432273334"
var token = JWT.sign({ id:123,"name":"Charlie" }, 'invalid');
var GA = "gwcm=%7B%22expires%22%3Anull%2C%22clabel%22%3A%22SbNVCILRtFcQwcrE6gM%22%2C%22backoff%22%3A1437241242%7D; _ga=GA1.2.1363734468.1432273334";
var token = JWT.sign({ id: 123, "name": "Charlie" }, 'invalid');
var options = {
method: "POST",
url: "/privado",
headers: {
authorization: "Bearer "+token,
cookie : GA
authorization: "Bearer " + token,
cookie: GA
}
};
server.inject(options, function(response) {
Expand All @@ -133,13 +133,13 @@ test("Valid Google Analytics cookie should be ignored (BAD Header Token)", funct

// Supply a VALID Token in Cookie A-N-D valid GA in Cookie!!
test("Valid Google Analytics cookie should be ignored (BAD Header Token)", function(t) {
var GA = "gwcm=%7B%22expires%22%3Anull%2C%22clabel%22%3A%22SbNVCILRtFcQwcrE6gM%22%2C%22backoff%22%3A1437241242%7D; _ga=GA1.2.1363734468.1432273334"
var token = JWT.sign({ id:123,"name":"Charlie" }, secret);
var GA = "gwcm=%7B%22expires%22%3Anull%2C%22clabel%22%3A%22SbNVCILRtFcQwcrE6gM%22%2C%22backoff%22%3A1437241242%7D; _ga=GA1.2.1363734468.1432273334";
var token = JWT.sign({ id: 123, "name": "Charlie" }, secret);
var options = {
method: "POST",
url: "/privado",
headers: {
cookie : "token=" + token + '; ' + GA
cookie: "token=" + token + '; ' + GA
}
};
server.inject(options, function(response) {
Expand Down
11 changes: 5 additions & 6 deletions test/custom-parameters-server.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
var Hapi = require('hapi');
var JWT = require('jsonwebtoken');
var secret = 'NeverShareYourSecret';

// for debug options see: http://hapijs.com/tutorials/logging
var server = new Hapi.Server({ debug: false });
server.connection();

var db = {
"123" : { allowed: true, "name":"Charlie" },
"321" : { allowed: false, "name":"Old Gregg"}
"123": { allowed: true, "name": "Charlie" },
"321": { allowed: false, "name": "Old Gregg" }
};

// defining our own validate function lets us do something
Expand All @@ -25,7 +24,7 @@ var privado = function(req, reply) {
return reply('worked');
};

server.register(require('../'), function (err) {
server.register(require('../'), function () {

server.auth.strategy('jwt', 'jwt', {
key: secret,
Expand All @@ -37,11 +36,11 @@ server.register(require('../'), function (err) {
});

server.route([
{ method: 'GET', path: '/', handler: home, config:{ auth: false } },
{ method: 'GET', path: '/', handler: home, config: { auth: false } },
{ method: 'POST', path: '/privado', handler: privado, config: { auth: 'jwt' } },
{ method: 'POST', path: '/required', handler: privado, config: { auth: { mode: 'required', strategy: 'jwt' } } },
{ method: 'POST', path: '/optional', handler: privado, config: { auth: { mode: 'optional', strategy: 'jwt' } } },
{ method: 'POST', path: '/try', handler: privado, config: { auth: { mode: 'try', strategy: 'jwt' } } },
{ method: 'POST', path: '/try', handler: privado, config: { auth: { mode: 'try', strategy: 'jwt' } } }
]);

});
Expand Down
50 changes: 25 additions & 25 deletions test/custom-parameters-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ var cookie_options = '; Max-Age=31536000;'; //' Expires=Mon, 18 Jul 2016 05:29:4
// Those tests are the same as cookie-test and url-token-test but with custom parameters in cookie or URL

test("Attempt to access restricted content using inVALID Cookie Token - custom parameters", function(t) {
var token = JWT.sign({ id:123,"name":"Charlie" }, 'badsecret');
var token = JWT.sign({ id: 123, "name": "Charlie" }, 'badsecret');
var options = {
method: "POST",
url: "/privado",
headers: { cookie : "customCookieKey=" + token}
headers: { cookie: "customCookieKey=" + token }
};
console.log(options);
server.inject(options, function(response) {
Expand All @@ -21,11 +21,11 @@ test("Attempt to access restricted content using inVALID Cookie Token - custom p
});

test("Attempt to access restricted content with VALID Token but malformed Cookie - custom parameters", function(t) {
var token = JWT.sign({ id:123,"name":"Charlie" }, secret);
var token = JWT.sign({ id: 123, "name": "Charlie" }, secret);
var options = {
method: "POST",
url: "/privado",
headers: { cookie : token }
headers: { cookie: token }
};
// server.inject lets us similate an http request
server.inject(options, function(response) {
Expand All @@ -35,11 +35,11 @@ test("Attempt to access restricted content with VALID Token but malformed Cookie
});

test("Access restricted content with VALID Token Cookie - custom parameters", function(t) {
var token = JWT.sign({ id:123,"name":"Charlie" }, secret);
var token = JWT.sign({ id: 123, "name": "Charlie" }, secret);
var options = {
method: "POST",
url: "/privado",
headers: { cookie : "customCookieKey=" + token }
headers: { cookie: "customCookieKey=" + token }
};
// server.inject lets us similate an http request
server.inject(options, function(response) {
Expand All @@ -49,11 +49,11 @@ test("Access restricted content with VALID Token Cookie - custom parameters", fu
});

test("Access restricted content with VALID Token Cookie (With Options!) - custom parameters", function(t) {
var token = JWT.sign({ id:123,"name":"Charlie" }, secret);
var token = JWT.sign({ id: 123, "name": "Charlie" }, secret);
var options = {
method: "POST",
url: "/privado",
headers: { cookie : "customCookieKey=" + token + cookie_options }
headers: { cookie: "customCookieKey=" + token + cookie_options }
};
// console.log(' - - - - - - - - - - - - - - - OPTIONS:')
// console.log(options);
Expand All @@ -71,13 +71,13 @@ test("Access restricted content with VALID Token Cookie (With Options!) - custom
// supply valid Token Auth Header but invalid Cookie
// should succeed because Auth Header is first
test("Authorization Header should take precedence over any cookie - custom parameters", function(t) {
var token = JWT.sign({ id:123,"name":"Charlie" }, secret);
var token = JWT.sign({ id: 123, "name": "Charlie" }, secret);
var options = {
method: "POST",
url: "/privado",
headers: {
authorization: "MyAuthScheme " + token,
cookie : "customCookieKey=malformed.token" + cookie_options
cookie: "customCookieKey=malformed.token" + cookie_options
}
};
server.inject(options, function(response) {
Expand All @@ -92,13 +92,13 @@ test("Authorization Header should take precedence over any cookie - custom param
// see: https://github.com/dwyl/hapi-auth-jwt2/issues/65#issuecomment-124791842
test("Valid Google Analytics cookie should be ignored - custom parameters", function(t) {
var GA = "gwcm=%7B%22expires%22%3Anull%2C%22clabel%22%3A%22SbNVCILRtFcQwcrE6gM%22%2C%22backoff%22%3A1437241242%7D; _ga=GA1.2.1363734468.1432273334";
var token = JWT.sign({ id:123,"name":"Charlie" }, secret);
var token = JWT.sign({ id: 123, "name": "Charlie" }, secret);
var options = {
method: "POST",
url: "/privado",
headers: {
authorization: "MyAuthScheme "+token,
cookie : GA
authorization: "MyAuthScheme " + token,
cookie: GA
}
};
server.inject(options, function(response) {
Expand All @@ -109,13 +109,13 @@ test("Valid Google Analytics cookie should be ignored - custom parameters", func

test("Valid Google Analytics cookie should be ignored (BAD Header Token) - custom parameters", function(t) {
var GA = "gwcm=%7B%22expires%22%3Anull%2C%22clabel%22%3A%22SbNVCILRtFcQwcrE6gM%22%2C%22backoff%22%3A1437241242%7D; _ga=GA1.2.1363734468.1432273334";
var token = JWT.sign({ id:123,"name":"Charlie" }, 'invalid');
var token = JWT.sign({ id: 123, "name": "Charlie" }, 'invalid');
var options = {
method: "POST",
url: "/privado",
headers: {
authorization: "MyAuthScheme "+token,
cookie : GA
authorization: "MyAuthScheme " + token,
cookie: GA
}
};
server.inject(options, function(response) {
Expand All @@ -127,12 +127,12 @@ test("Valid Google Analytics cookie should be ignored (BAD Header Token) - custo
// Supply a VALID Token in Cookie A-N-D valid GA in Cookie!!
test("Valid Google Analytics cookie should be ignored (BAD Header Token) - custom parameters", function(t) {
var GA = "gwcm=%7B%22expires%22%3Anull%2C%22clabel%22%3A%22SbNVCILRtFcQwcrE6gM%22%2C%22backoff%22%3A1437241242%7D; _ga=GA1.2.1363734468.1432273334";
var token = JWT.sign({ id:123,"name":"Charlie" }, secret);
var token = JWT.sign({ id: 123, "name": "Charlie" }, secret);
var options = {
method: "POST",
url: "/privado",
headers: {
cookie : "customCookieKey=" + token + '; ' + GA
cookie: "customCookieKey=" + token + '; ' + GA
}
};
server.inject(options, function(response) {
Expand All @@ -157,7 +157,7 @@ test("Attempt to access restricted content (with an INVALID URL Token) - custom

test("Try using an incorrect secret to sign the JWT - custom parameters", function(t) {
// use the token as the 'authorization' header in requests
var token = JWT.sign({ id:123,"name":"Charlie" }, 'incorrectSecret');
var token = JWT.sign({ id: 123, "name": "Charlie" }, 'incorrectSecret');
token = "?customUrlKey=" + token;
// console.log(" - - - - - - token - - - - -")
// console.log(token);
Expand All @@ -175,7 +175,7 @@ test("Try using an incorrect secret to sign the JWT - custom parameters", functi
test("URL Token is well formed but is allowed=false so should be denied - custom parameters", function(t) {
// use the token as the 'authorization' header in requests
// var token = jwt.sign({ "id": 1 ,"name":"Old Greg" }, 'incorrectSecret');
var token = JWT.sign({ id:321,"name":"Old Gregg" }, secret);
var token = JWT.sign({ id: 321, "name": "Old Gregg" }, secret);
token = "?customUrlKey=" + token;
var options = {
method: "POST",
Expand All @@ -190,7 +190,7 @@ test("URL Token is well formed but is allowed=false so should be denied - custom

test("Access restricted content (with VALID Token) - custom parameters", function(t) {
// use the token as the 'authorization' header in requests
var token = JWT.sign({ id:123,"name":"Charlie" }, secret);
var token = JWT.sign({ id: 123, "name": "Charlie" }, secret);
token = "?customUrlKey=" + token;
var options = {
method: "POST",
Expand All @@ -204,11 +204,11 @@ test("Access restricted content (with VALID Token) - custom parameters", functio
});

test("Attempt to access restricted content using inVALID header tokenType - custom parameters", function(t) {
var token = JWT.sign({ id:123,"name":"Charlie" }, 'badsecret');
var token = JWT.sign({ id: 123, "name": "Charlie" }, 'badsecret');
var options = {
method: "POST",
url: "/privado",
headers: { Authorization : "InvalidAuthScheme " + token }
headers: { Authorization: "InvalidAuthScheme " + token }
};
server.inject(options, function(response) {
t.equal(response.statusCode, 401, "Invalid token should error!");
Expand All @@ -217,11 +217,11 @@ test("Attempt to access restricted content using inVALID header tokenType - cust
});

test("Access restricted content (with VALID Token and header tokenType) - custom parameters", function(t) {
var token = JWT.sign({ id:123,"name":"Charlie" }, secret);
var token = JWT.sign({ id: 123, "name": "Charlie" }, secret);
var options = {
method: "POST",
url: "/privado",
headers: { Authorization : "MyAuthScheme " + token }
headers: { Authorization: "MyAuthScheme " + token }
};

server.inject(options, function(response) {
Expand Down
Loading

0 comments on commit d2eff25

Please sign in to comment.