Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/perliedman/utoken
Access token validation middleware for Zeit's Micro
https://github.com/perliedman/utoken
Last synced: about 1 month ago
JSON representation
Access token validation middleware for Zeit's Micro
- Host: GitHub
- URL: https://github.com/perliedman/utoken
- Owner: perliedman
- License: isc
- Created: 2018-02-09T13:51:34.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-02-09T15:25:57.000Z (almost 7 years ago)
- Last Synced: 2024-10-04T02:16:31.229Z (about 1 month ago)
- Language: JavaScript
- Size: 34.2 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# µ-token (utoken)
A access token validation middleware for [micro](https://github.com/zeit/micro).
Example:
```js
const accessTokenMiddleWare = require('utoken')
const app = require('my-micro-app')micro(accessTokenMiddleWare(app))
```This will create a middleware that will read access tokens from the request's `access_token` query parameter.
By default, all tokens will be rejected, so we will need to specify a function that tests for valid access tokens:```js
micro(accessTokenMiddleWare(app, {
tokenValidatorFn: token => token == '12345'
}))
```This will allow the single access token `12345`. For real world use, you might want to look up the token in a database
or something like that.## Options
There are a couple of options that can be passed to customize the middleware's functionality:
### tokenAccessFn
This function specifies how the access token is extracted from a request; it is a function that will
be passed the HTTP request and should return the token (or `undefined` if not present).By default, this function will extract the query parameter `access_token` from the request's URL.
### tokenValidatorFn
This function validates an access token and returns `true` if it is a valid token and `false` otherwise.
The default is a function that _always_ returns `false` (rejects all tokens).
### failFn
Called when access token validation fails: returns a HTTP 401 by default with the error message
`"Missing or invalid access token"`.