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

Update the repo 🚀 ! #167

Merged
merged 15 commits into from
Apr 12, 2020
Merged
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
21 changes: 19 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
# OS #
###################
.DS_Store
node_modules/*
.idea
Thumbs.db
tmp/
temp/


# Node.js #
###################
node_modules
package-lock.json
npm-debug.log
yarn-debug.log
yarn-error.log


# NYC #
###################
coverage
*.lcov
.nyc_output
.idea
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
15 changes: 8 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
language: node_js
node_js:
- "7.6"
- "stable"
sudo: false
- 8
- 10
- 12
- 'lts/*'
- 'node'
script:
- npm run test-cov
after_script:
- npm i coveralls
- cat ./coverage/lcov.info | node ./node_modules/coveralls/bin/coveralls
cache:
directories:
- node_modules
- cat ./coverage/lcov.info | node ./node_modules/coveralls/bin/coveralls
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) [year] [fullname]
Copyright (c) 2020 Koa contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
38 changes: 25 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# koa-jwt
# [**koa-jwt**](https://github.com/koajs/jwt)

> Koa middleware for validating JSON Web Tokens.

[![node version][node-image]][node-url]
[![npm download][download-image]][download-url]
Expand All @@ -20,6 +22,7 @@
[license-image]: https://img.shields.io/npm/l/koa-jwt.svg?maxAge=2592000&style=flat-square
[license-url]: https://github.com/koajs/jwt/blob/master/LICENSE


## Table of Contents

- [koa-jwt](#koa-jwt)
Expand All @@ -39,6 +42,7 @@
- [Contributors](#contributors)
- [License](#license)


## Introduction

This module lets you authenticate HTTP requests using JSON Web Tokens
Expand All @@ -51,19 +55,22 @@ for a good introduction.
* `koa-jwt` version 3+ on the [master](https://github.com/koajs/jwt) branch uses `async` / `await` and hence requires node >= 7.6.
* If you are using `koa` version 1, you need to install `koa-jwt@1` from npm. This is the code on the [koa-v1](https://github.com/koajs/jwt/tree/koa-v1) branch.


## Install

```bash
npm install koa-jwt
```


## Usage

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


### Retrieving the token

The token is normally provided in a HTTP header (`Authorization`), but it
Expand All @@ -84,9 +91,9 @@ should match the following interface:

The resolution order for the token is the following. The first non-empty token resolved will be the one that is verified.

* `opts.getToken` function
* check the cookies (if `opts.cookie` is set)
* check the Authorization header for a bearer token
* `opts.getToken` function.
* check the cookies (if `opts.cookie` is set).
* check the Authorization header for a bearer token.

### Passing the secret

Expand All @@ -105,13 +112,14 @@ match the following interface:
/**
* Your custom isRevoked resolver
*
* @param {object} ctx The ctx object passed to the middleware
* @param {object} decodedToken Content of the token
* @param {object} token token The token
* @return {Promise} If the token is not revoked, the promise must resolve with false, otherwise (the promise resolve with true or error) the token is revoked
* @param {object} ctx The ctx object passed to the middleware
* @param {object} decodedToken Content of the token
* @param {object} token token The token
* @return {Promise} If the token is not revoked, the promise must resolve with false, otherwise (the promise resolve with true or error) the token is revoked
*/
```


## Example

```js
Expand Down Expand Up @@ -157,7 +165,7 @@ app.listen(3000);
Alternatively you can conditionally run the `jwt` middleware under certain conditions:

```js
var koa = require('koa');
var Koa = require('koa');
var jwt = require('koa-jwt');

var app = new Koa();
Expand Down Expand Up @@ -221,6 +229,7 @@ This allows for rolling shared secrets, for example:
app.use(jwt({ secret: ['old-shared-secret', 'new-shared-secret'] }));
```


## Token Verification Exceptions

If the JWT has an expiration (`exp`), it will be checked.
Expand Down Expand Up @@ -260,8 +269,7 @@ If the `secret` option is a function, this function is called for each JWT recei
order to determine which secret is used to verify the JWT.

The signature of this function should be `(header, payload) => [Promise(secret)]`, where
`header` is the token header and `payload` is the token payload. For instance to support JWKS token header should contain
`alg` and `kid`: algorithm and key id fields respectively.
`header` is the token header and `payload` is the token payload. For instance to support JWKS token header should contain `alg` and `kid`: algorithm and key id fields respectively.

This option can be used to support JWKS (JSON Web Key Set) providers by using
[node-jwks-rsa](https://github.com/auth0/node-jwks-rsa). For example:
Expand All @@ -281,9 +289,10 @@ app.use(jwt({
}));
```


## Related Modules

* [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken) — JSON Web Token signing and verification
* [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken) — JSON Web Token signing and verification.

Note that koa-jwt no longer exports the `sign`, `verify` and `decode` functions from `jsonwebtoken` in the koa-v2 branch.

Expand All @@ -294,6 +303,7 @@ npm install
npm test
```


## Authors/Maintainers

* [Stian Grytøyr](https://github.com/stiang) (initial author)
Expand All @@ -320,7 +330,9 @@ The initial code was largely based on [express-jwt](https://github.com/auth0/exp
* [scttcper](https://github.com/scttcper)
* [jhnns](https://github.com/jhnns)
* [dunnock](https://github.com/dunnock)
* [3imed-jaberi](https://github.com/3imed-jaberi)


## License

[The MIT License](http://opensource.org/licenses/MIT)
[MIT](/LICENSE)
6 changes: 2 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,15 @@ module.exports = (opts = {}) => {

try {
if (typeof secret === 'function') {
secret = await getSecret(secret, token)
secret = await getSecret(secret, token);
}

if (!secret) {
throw new Error('Secret not provided');
}

let secrets = Array.isArray(secret) ? secret : [secret];
const decodedTokens = secrets.map(async (s) => {
return await verify(token, s, opts)
});
const decodedTokens = secrets.map(async s => await verify(token, s, opts));

const decodedToken = await pAny(decodedTokens)
.catch(function (err) {
Expand Down
117 changes: 93 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "koa-jwt",
"version": "3.5.1",
"description": "Koa JWT authentication middleware.",
"version": "3.6.0",
"description": "Koa middleware for validating JSON Web Tokens",
"main": "./lib",
"types": "types/index.d.ts",
"keywords": [
"auth",
Expand All @@ -15,23 +16,87 @@
"middleware",
"token",
"oauth",
"permissions",
"koa"
],
"repository": {
"type": "git",
"url": "git://github.com/koajs/jwt.git"
},
"bugs": {
"url": "https://github.com/koajs/jwt/issues"
},
"homepage": "https://github.com/koajs/jwt",
"author": {
"name": "Stian Grytøyr",
"email": "stian@grytoyr.net",
"url": "http://stian.grytoyr.net/"
},
"license": "MIT",
"main": "./lib",
"authors": [
{
"name": "Stian Grytøyr",
"email": "stian@grytoyr.net",
"url": "http://stian.grytoyr.net/"
},
{
"name": "Scott Donnelly",
"url": "http://scott.donnel.ly/"
}
],
"contributors": [
{
"name": "Jesús Rodríguez",
"email": "Foxandxss@gmail.com",
"url": "http://angular-tips.com/"
},
{
"name": "Teoman Soygul",
"url": "http://soygul.com"
},
{
"name": "Charlike Mike Reagent",
"email": "hello@tunnckocore.com",
"url": "https://tunnckoCore.com"
},
{
"name": "Getulio Romão Campos Junior",
"email": "gromao@gmail.com",
"url": "https://github.com/getuliojr"
},
{
"name": "Cesar Andreu",
"email": "cesarandreu@gmail.com",
"url": "https://cesarandreu.com"
},
{
"name": "Michael Westphal",
"url": "https://github.com/michaelwestphal"
},
{
"name": "Jackong",
"url": "https://github.com/Jackong"
},
{
"name": "Daniel Kennedy",
"url": "https://github.com/danwkennedy"
},
{
"name": "Nicolás Fantone",
"email": "contact@nfantone.dev",
"url": "https://github.com/nfantone"
},
{
"name": "Scott Cooper",
"email": "scttcper@gmail.com",
"url": "https://github.com/scttcper"
},
{
"name": "Johannes Ewald",
"email": "mail@johannesewald.de",
"url": "https://github.com/jhnns"
},
{
"name": "Maxim Vorobjov",
"url": "https://github.com/dunnock"
},
{
"name": "Imed Jaberi",
"email": "imed_jebari@hotmail.fr",
"url": "https://github.com/3imed-jaberi"
}
],
"nyc": {
"reporter": [
"lcov",
Expand All @@ -40,22 +105,26 @@
"report-dir": "./coverage"
},
"dependencies": {
"jsonwebtoken": "8.5.1",
"koa-unless": "1.0.7",
"p-any": "1.1.0"
"jsonwebtoken": "^8.5.1",
"koa-unless": "^1.0.7",
"p-any": "^2.1.0"
},
"devDependencies": {
"chai": "latest",
"koa": "^2.0.1",
"mocha": "3.2.0",
"nyc": "10.1.2",
"supertest": "3.0.0"
"koa": "^2.11.0",
"mocha": "^7.0.1",
"nyc": "^15.0.0",
"supertest": "^4.0.2"
},
"engines": {
"node": ">= 7.6.0"
"node": ">= 8"
},
"scripts": {
"test": "nyc npm run test-only",
"test-only": "mocha --reporter spec test/test.js"
}
}
"test": "mocha --reporter spec test/test.js --exit",
"test-cov": "nyc npm run test"
},
"bugs": {
"url": "https://github.com/koajs/jwt/issues"
},
"homepage": "https://github.com/koajs/jwt"
}
2 changes: 1 addition & 1 deletion test/test-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ console.log('You can test the server by issuing curl commands like the following
console.log('');
console.log(' curl http://localhost:3000/public/foo # should succeed (return "unprotected")');
console.log(' curl http://localhost:3000/api/foo # should fail (return "401 Unauthorized ...")');
console.log(' curl -H "Authorization: Bearer ' + TOKEN + '" http://localhost:3000/api/foo # should succeed (return "protected")');
console.log(` curl -H "Authorization: Bearer ${TOKEN}" http://localhost:3000/api/foo # should succeed (return "protected")`);
console.log('');

const app = new Koa();
Expand Down
Loading