1.0.0 • Published 5 years ago

@shoskens/koa-jwt-authz v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

koa-jwt-authz

forked this since the original disappeared from npm registry

NPM version Build Status Dependency Status devDependency Status

XO code style

Validate a JWTs scope to authorize access to an endpoint.

Install

$ npm i -S @shoskens/koa-jwt-authz

koa >=2 is a peer dependency. Make sure it is installed in your project.

Usage

Use together with koa-jwt to both validate a JWT and make sure it has the correct permissions to call an endpoint.

const jwtAuthz = require('@tadashi/koa-jwt-authz')
const jwt = require('koa-jwt')
const Koa = require('koa')
const Router = require('koa-router')

const app = new Koa()
const router = new Router()

router.get('/', ctx => {
  ctx.body = {home: 'free'}
})

router.get('/me',
  jwt({secret: 'shared_secret'}),
  jwtAuthz(['read:users']),
  ctx => {
    ctx.body = ctx.state.user
  }
)

app.use(router.middleware())

const port = process.env.PORT || 3000
app.listen(port)

The JWT must have a scope claim and it must either be a string of space-separated permissions or an array of strings. For example:

# String: "write:users read:users"

# Array: ["write:users", "read:users"]

API

jwtAuthz(expectedScopes [, options])

parametertyperequireddefaultdescription
expectedScopesArrayyes-List of permissions
optionsObjectnosee belowOptions

options

parametertyperequireddefaultdescription
checkAllScopesBooleannofalseWhen true, all the expected scopes will be checked against the user's scopes
customScopeKeyStringnoscopeThe property name to check for the scope

Author

License

MIT © Thiago Lagden

1.0.0

5 years ago