Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gerhut/koa-http-auth
HTTP authorization middleware of koa.
https://github.com/gerhut/koa-http-auth
Last synced: 2 months ago
JSON representation
HTTP authorization middleware of koa.
- Host: GitHub
- URL: https://github.com/gerhut/koa-http-auth
- Owner: Gerhut
- License: mit
- Archived: true
- Created: 2016-03-06T11:35:26.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2019-12-25T08:22:02.000Z (about 5 years ago)
- Last Synced: 2024-08-04T00:04:26.112Z (6 months ago)
- Language: JavaScript
- Size: 11.7 KB
- Stars: 6
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- awesome-koa - koa-http-auth - 简单的 HTTP 身份验证,包括基本身份验证和摘要(Digest)身份验证。 ![](https://img.shields.io/github/stars/gerhut/koa-http-auth.svg?style=social&label=Star) ![](https://img.shields.io/npm/dm/koa-http-auth.svg?style=flat-square) (仓库 / 中间件)
README
# koa-http-auth
[![Build Status](https://travis-ci.org/Gerhut/koa-http-auth.svg?branch=master)](https://travis-ci.org/Gerhut/koa-http-auth)
[![codecov.io](https://codecov.io/github/Gerhut/koa-http-auth/coverage.svg?branch=master)](https://codecov.io/github/Gerhut/koa-http-auth?branch=master)
[![Dependency Status](https://david-dm.org/gerhut/koa-http-auth.svg)](https://david-dm.org/gerhut/koa-http-auth)
[![devDependency Status](https://david-dm.org/gerhut/koa-http-auth/dev-status.svg)](https://david-dm.org/gerhut/koa-http-auth#info=devDependencies)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)Simple [HTTP Authentication](https://tools.ietf.org/html/rfc2617) middleware
of koa## Install
$ npm install koa-http-auth
## Usage
### Basic Authentication
```javascript
const koa = require('koa')
const BasicAuth = require('koa-http-auth').Basicconst app = koa()
app.use(BasicAuth('Simple Application'))app.use(function * (next) {
if (this.request.auth == null) { // No authorization provided
this.body = 'Please log in.'
return // Middleware will auto give 401 response
}if (this.request.auth.user !== 'user' ||
this.request.auth.password('password')) {
this.body = 'Invalid user.'
delete this.request.auth // Delete request.auth ...
return // ... will make middleware give 401 response too.
}if (this.url === '/logout') {
this.body = 'You are successfully logged out.'
delete this.request.auth // Delete request.auth unconditionally ...
return // ... will make user logged out.
}this.body = 'Welcome back!'
yield next
})
```### Digest Authentication
```javascript
const koa = require('koa')
const DigestAuth = require('koa-http-auth').Digestconst app = koa()
app.use(DigestAuth('Simple Application'))app.use(function * (next) {
if (this.request.auth == null) { // No authorization provided
this.body = 'Please log in.'
return // Middleware will auto give 401 response
}if (this.request.auth.user !== 'user' ||
this.request.auth.password('password')) {
this.body = 'Invalid user.'
delete this.request.auth // Delete request.auth ...
return // ... will make middleware give 401 response too.
}if (this.url === '/logout') {
this.body = 'You are successfully logged out.'
delete this.request.auth // Delete request.auth unconditionally ...
return // ... will make user logged out.
}this.body = 'Welcome back!'
yield next
})
```