Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/elysiajs/elysia-bearer
A plugin for Elysia for retreiving Bearer token
https://github.com/elysiajs/elysia-bearer
Last synced: 11 days ago
JSON representation
A plugin for Elysia for retreiving Bearer token
- Host: GitHub
- URL: https://github.com/elysiajs/elysia-bearer
- Owner: elysiajs
- License: mit
- Created: 2022-12-04T13:29:18.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-11-16T00:00:16.000Z (28 days ago)
- Last Synced: 2024-11-17T16:01:27.672Z (26 days ago)
- Language: TypeScript
- Size: 157 KB
- Stars: 8
- Watchers: 1
- Forks: 4
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-elysia - bearer - Plugin for retreiving Bearer token. (Plugins)
README
# @elysiajs/bearer
Plugin for [elysia](https://github.com/elysiajs/elysia) for retrieving Bearer token.This plugin is for retrieving a Bearer token specified in [RFC6750](https://www.rfc-editor.org/rfc/rfc6750#section-2).
This plugin **DOES NOT** handle authentication validation for your server, rather the plugin leaves the decision for developers to apply logic for handle validation check themself.
## Installation
```bash
bun add @elysiajs/bearer
```## Example
```typescript
import { Elysia } from 'elysia'
import { bearer } from '@elysiajs/bearer'const app = new Elysia()
.use(bearer())
.get('/sign', ({ bearer }) => bearer, {
beforeHandle({ bearer, set }) {
if (!bearer) {
set.status = 400
set.headers[
'WWW-Authenticate'
] = `Bearer realm='sign', error="invalid_request"`return 'Unauthorized'
}
}
})
.listen(8080)
```## API
This plugin decorates `bearer` into `Context`.### bearer
Extracted bearer token according to RFC6750, is either `string` or `undefined`,If is undefined, means that there's no token provided.
## Config
Below is the configurable property for customizing the Bearer plugin.### Extract
Custom extractor for retrieving tokens when the API doesn't compliant with RFC6750.```typescript
/**
* If the API doesn't compliant with RFC6750
* The key for extracting the token is configurable
*/
extract: {
/**
* Determined which fields to be identified as Bearer token
*
* @default access_token
*/
body?: string
/**
* Determined which fields to be identified as Bearer token
*
* @default access_token
*/
query?: string
/**
* Determined which type of Authentication should be Bearer token
*
* @default Bearer
*/
header?: string
}
```