Skip to content

Commit

Permalink
Added option for key to use for decoded data
Browse files Browse the repository at this point in the history
  • Loading branch information
stiang committed Jan 29, 2014
1 parent 881f3f7 commit 9abee41
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# koa-jwt

Koa middleware that validates JSON Web Tokens and sets `ctx.user`
if a valid token is provided.
(by default) if a valid token is provided.

This module lets you authenticate HTTP requests using JSON Web Tokens
in your [Koa](http://koajs.com/) (node.js) applications.
Expand All @@ -16,9 +16,9 @@ for a good introduction.
## Usage

The JWT authentication middleware authenticates callers using a JWT
token. If the token is valid, `ctx.user` will be set with the JSON
object decoded to be used by later middleware for authorization and
access control.
token. If the token is valid, `ctx.user` (by default) will be set
with the JSON object decoded to be used by later middleware for
authorization and access control.

## Example

Expand Down Expand Up @@ -64,6 +64,7 @@ app.use(function *(){
app.listen(3000);
```


Alternatively, you can add the `passthrough` option to always yield next,
even if no valid Authorization header was found:
```js
Expand All @@ -72,6 +73,12 @@ app.use(jwt({ secret: 'shared-secret', passthrough: true }));
This lets downstream middleware make decisions based on whether `ctx.user` is set.


If you prefer to use another ctx key for the decoded data, just pass in `key`, like so:
```js
app.use(jwt({ secret: 'shared-secret', key: 'jwtdata' }));
```
This makes the decoded data available as `ctx.jwtdata`.

You can specify audience and/or issuer as well:
```js
app.use(jwt({ secret: 'shared-secret',
Expand All @@ -80,6 +87,7 @@ app.use(jwt({ secret: 'shared-secret',
```
If the JWT has an expiration (`exp`), it will be checked.


This module also support tokens signed with public/private key pairs. Instead
of a secret, you can specify a Buffer with the public key:
```js
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ JWT.verify = thunkify(JWT.verify);

module.exports = function(opts) {
opts = opts || {};
opts.key = opts.key || 'user';

assert(opts.secret, '"secret" option is required');

Expand Down Expand Up @@ -40,7 +41,7 @@ module.exports = function(opts) {
}

if (user || opts.passthrough) {
this.user = user;
this[opts.key] = user;
yield next;
} else {
this.throw(401, msg);
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koa-jwt",
"version": "0.0.1",
"description": "JWT authentication middleware.",
"version": "0.0.2",
"description": "Koa JWT authentication middleware.",
"keywords": [
"auth",
"authn",
Expand All @@ -10,6 +10,8 @@
"authorization",
"http",
"jwt",
"json",
"middleware",
"token",
"oauth",
"koa"
Expand Down
24 changes: 24 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,28 @@ describe('success tests', function () {

});

it('should use provided key for decoded data', function(done) {
var validUserResponse = function(res) {
if (!(res.body.foo === 'bar')) return "Key param not used properly";
}

var secret = 'shhhhhh';
var token = jwt.sign({foo: 'bar'}, secret);

var app = koa();

app.use(koajwt({ secret: secret, key: 'jwtdata' }));
app.use(function* (next) {
this.body = this.jwtdata;
});

request(app.listen())
.get('/')
.set('Authorization', 'Bearer ' + token)
.expect(200)
.expect(validUserResponse)
.end(done);

});

});

0 comments on commit 9abee41

Please sign in to comment.