Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lagden/koa-jwt-authz
Validate a JWTs scope to authorize access to an endpoint
https://github.com/lagden/koa-jwt-authz
auth0 jwt koa2 middleware permissions scope
Last synced: 4 months ago
JSON representation
Validate a JWTs scope to authorize access to an endpoint
- Host: GitHub
- URL: https://github.com/lagden/koa-jwt-authz
- Owner: lagden
- License: mit
- Created: 2019-09-05T06:37:45.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-24T08:54:18.000Z (about 2 years ago)
- Last Synced: 2024-10-01T02:21:37.476Z (4 months ago)
- Topics: auth0, jwt, koa2, middleware, permissions, scope
- Language: JavaScript
- Size: 560 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# koa-jwt-authz
[![NPM version][npm-img]][npm]
[![Build Status][ci-img]][ci]
[![Coverage Status][coveralls-img]][coveralls][npm-img]: https://img.shields.io/npm/v/@tadashi/koa-jwt-authz.svg
[npm]: https://www.npmjs.com/package/@tadashi/koa-jwt-authz
[ci-img]: https://github.com/lagden/koa-jwt-authz/actions/workflows/nodejs.yml/badge.svg
[ci]: https://github.com/lagden/koa-jwt-authz/actions/workflows/nodejs.yml
[coveralls-img]: https://coveralls.io/repos/github/lagden/koa-jwt-authz/badge.svg?branch=master
[coveralls]: https://coveralls.io/github/lagden/koa-jwt-authz?branch=masterValidate a JWTs `scope` to authorize access to an endpoint.
## Install
```
$ npm i -S @tadashi/koa-jwt-authz
```> `koa >=2` is a peer dependency. Make sure it is installed in your project.
## Usage
Use together with [koa-jwt](https://github.com/koajs/jwt) to both validate a JWT and make sure it has the correct permissions to call an endpoint.
```js
import jwtAuthz from '@tadashi/koa-jwt-authz'
import jwt from 'koa-jwt'
import Koa from 'koa'
import Router from '@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())
app.listen(process.env.PORT ?? 3000)
```---
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\])
parameter | type | required | default | description
----------- | -------------------- | ----------- | ------------------- | ------------
expectedScopes | Array | yes | - | List of permissions
options | Object | no | [see below](#options) | Options#### options
parameter | type | required | default | description
----------- | -------------------- | ----------- | ------------------- | ------------
checkAllScopes | Boolean | no | false | When true, all the expected scopes will be checked against the user's scopes
customScopeKey | String | no | scope | The property name to check for the scope## Author
[](https://github.com/lagden)
## License
MIT © [Thiago Lagden](https://github.com/lagden)