Skip to content
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

added parameter to irgnore url token #146

Merged
merged 12 commits into from
Apr 30, 2016
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,10 @@ signature `function(decoded, callback)` where:
- `responseFunc` - (***optional***) optional function called to decorate the response with authentication headers before the response headers or payload is written where:
- `request` - the request object.
- `reply(err, response)`- is called if an error occurred
- `urlKey` - (***optional***) if you prefer to pass your token via url, simply add a `token` url parameter to your request or use a custom parameter by setting `urlKey`
- `cookieKey` - (***optional***) if you prefer to pass your token via a cookie, simply set the cookie `token=your.jsonwebtoken.here` or use a custom key by setting `cookieKey`
- `tokenType` - (**optinal**) allow custom token type, e.g. Authorization: \<tokenType> 12345678, default is none.
- `allowUrlToken` - (***optional*** *default: true*) if don't want to allow url tokens you can disable this here by setting this to *false*
- `urlKey` - (***optional*** *default: 'token'*) if you prefer to pass your token via url, simply add a `token` url parameter to your request or use a custom parameter by setting `urlKey`
- `cookieKey` - (***optional*** *default: 'token'*) if you prefer to pass your token via a cookie, simply set the cookie `token=your.jsonwebtoken.here` or use a custom key by setting `cookieKey`
- `tokenType` - (**optinal** *default: none*) allow custom token type, e.g. Authorization: \<tokenType> 12345678, default is none.

### Understanding the Request Flow

Expand Down
3 changes: 2 additions & 1 deletion lib/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ module.exports = function (request, options) {
// The key holding token value in url or cookie defaults to token
var urlKey = typeof options.urlKey === 'string' ? options.urlKey : 'token';
var cookieKey = typeof options.cookieKey === 'string' ? options.cookieKey : 'token';
var allowUrlToken = typeof options.allowUrlToken === 'boolean' ? options.allowUrlToken : true;
var auth;

if(request.query[urlKey]) { // tokens via url: https://github.com/dwyl/hapi-auth-jwt2/issues/19
if(allowUrlToken && request.query[urlKey]) { // tokens via url: https://github.com/dwyl/hapi-auth-jwt2/issues/19

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about when allowUrlToken has a falsy value and request.query[urlKey] a truthly one ? If this case is not possible, please add a comment to state that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was the whole idea to be able to turn the url key off. And I wanted it to default to true, so that it's not a breaking change. Another way would probably be to set the urlKey explicit to false.

auth = request.query[urlKey];
} // JWT tokens in cookie: https://github.com/dwyl/hapi-auth-jwt2/issues/55
else if (request.headers.authorization) {
Expand Down
8 changes: 8 additions & 0 deletions test/basic_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,18 @@ server.register(require('../'), function () {
verifyOptions: { algorithms: [ 'HS256' ] } // only allow HS256 algorithm
});

server.auth.strategy('jwt-nourl', 'jwt', {
key: secret,
validateFunc: validate,
verifyOptions: { algorithms: [ 'HS256' ] }, // only allow HS256 algorithm
allowUrlToken: false
});

server.route([
{ method: 'GET', path: '/', handler: home, config: { auth: false } },
{ method: 'GET', path: '/token', handler: sendToken, config: { auth: 'jwt' } },
{ method: 'POST', path: '/privado', handler: privado, config: { auth: 'jwt' } },
{ method: 'POST', path: '/privadonourl', handler: privado, config: { auth: 'jwt-nourl' } },
{ 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' } } }
Expand Down
15 changes: 15 additions & 0 deletions test/url_token_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,18 @@ test("Access restricted content (with VALID Token)", function(t) {
t.end();
});
});

test("Using route with allowUrlToken=false should be denied", function(t) {
// use the token as the 'authorization' header in requests
var token = JWT.sign({ id: 123, "name": "Charlie" }, secret);
token = "?token=" + token;
var options = {
method: "POST",
url: "/privadonourl" + token
};
// server.inject lets us simulate an http request
server.inject(options, function(response) {
t.equal(response.statusCode, 401, "No urlKey configured so URL-Tokens should be denied");
t.end();
});
});