https://github.com/syarig/koa-basic-auth-connect
Basic authentication middleware for koa.
https://github.com/syarig/koa-basic-auth-connect
Last synced: 4 months ago
JSON representation
Basic authentication middleware for koa.
- Host: GitHub
- URL: https://github.com/syarig/koa-basic-auth-connect
- Owner: syarig
- License: mit
- Created: 2022-04-14T14:04:52.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-06T06:08:38.000Z (over 2 years ago)
- Last Synced: 2025-02-27T18:11:19.936Z (5 months ago)
- Language: TypeScript
- Homepage:
- Size: 160 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# koa-basic-auth-connect
[](https://github.com/syarig/koa-basic-auth-connect/actions/workflows/test.yml)
[](https://codecov.io/gh/syarig/koa-basic-auth-connect)
[](https://badge.fury.io/js/koa-basic-auth-connect)
[]()

[](https://opensource.org/licenses/mit-license.php)## Installation
```shell
npm install koa-basic-auth-connect
```## Example
```js
const Koa = require('koa');
const basicAuth = require('koa-basic-auth-connect');const app = new Koa();
app.use(basicAuth({
users: {'SampleUser': 'password'}
}));
```The middleware checks for a match to the credentials of the received request. It parses the "Authorization" header
according to the Basic Authentication protocol and checks if the credentials are legitimate.If it is correct, a property is added to `ctx.state.auth`. This object contains an object with `user` and `password`
propertiesIf authentication fails, a 401 HTTP response is returned.
## Options
```ts
export type FunctionalOption=T | ((ctx: Context) => T);type Options={
users: Users;
realm?: FunctionalOption;
challenge?: boolean;
authorizer?: Authorizer;
continueIfUnauthorized?: FunctionalOption;
};
```| Option | Description | Default |
|-----------|--------------------------------------------------------------------|-----------|
| users | Records by User ID and Secret | |
| realm | Set realm on unauthorized response | |
| challenge | Add a challenge header on unauthorized response | false |
| authorizer | Set custom authorizer function | |
|continueIfUnauthorized | Continue middleware chain when unauthenticated | false |## Challenge
By default, the middleware does not add a `WWW-Authenticate` challenge header to the unauthorized response.
You can be enable that by `challenge` option. This will cause most browsers to display a popup for entering credentials
for unauthenticated responses. You may also add The realm can be used to identify the system to be authenticated and
stored by the client.```js
app.use(basicAuth({
users: {'ChallengeUser': 'psssword'},
challenge: true,
realm: 'Aiq+LNOl7X+LftH',
}))
```## Authorizer
The user and password are passed to the callback (async) function.
For example, you can implement your own authentication like this
```js
app.use(basicAuth({
authorizer: (user: string, password: string) => (password == 'anysecret')
}))
```