Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dd4e216

Browse files
committed
feat: initial implementation
0 parents  commit dd4e216

File tree

11 files changed

+311
-0
lines changed

11 files changed

+311
-0
lines changed

.autoreleaserc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"pre": {
3+
"verify": [
4+
"autorelease-github/verify",
5+
"autorelease-travis/verify"
6+
]
7+
},
8+
"post": {
9+
"publishChangelog": "autorelease-github/create-release"
10+
}
11+
}

.eslintrc.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"parser": "babel-eslint",
3+
"extends": "eslint:recommended",
4+
"rules": {
5+
"semi": 1,
6+
"no-console": 0
7+
},
8+
"parserOptions": {
9+
"ecmaVersion": 6,
10+
"sourceType": "module"
11+
},
12+
"env": {
13+
"node": true,
14+
"es6": true,
15+
"browser": true
16+
}
17+
}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules/
2+
/index.js
3+
/test.js
4+

.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.*
2+
Makefile
3+
rollup.config.js
4+

.travis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
sudo: false
2+
language: node_js
3+
cache:
4+
directories:
5+
- node_modules
6+
node_js:
7+
- '4'
8+
before_install:
9+
- npm i -g npm@latest
10+
after_success:
11+
- npm run autorelease
12+
branches:
13+
except:
14+
- "/^v\\d+\\.\\d+\\.\\d+$/"

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Tyler Johnson
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
BIN = ./node_modules/.bin
2+
SRC = $(wildcard src/* src/*/*)
3+
4+
build: index.js
5+
6+
index.js: src/index.js $(SRC)
7+
$(BIN)/rollup $< -c > $@
8+
9+
clean:
10+
rm -rf index.js
11+
12+
.PHONY: build clean

README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# couchdb-auth-proxy
2+
3+
[![npm](https://img.shields.io/npm/v/couchdb-auth-proxy.svg)](https://www.npmjs.com/package/couchdb-auth-proxy) [![David](https://img.shields.io/david/tyler-johnson/couchdb-auth-proxy.svg)](https://david-dm.org/tyler-johnson/couchdb-auth-proxy) [![Build Status](https://travis-ci.org/tyler-johnson/couchdb-auth-proxy.svg?branch=master)](https://travis-ci.org/tyler-johnson/couchdb-auth-proxy)
4+
5+
An HTTP reverse proxy server for easy CouchDB proxy authentication.
6+
7+
## Install
8+
9+
Install from NPM
10+
11+
```bash
12+
npm install couchdb-auth-proxy -S
13+
```
14+
15+
And import into your project
16+
17+
```js
18+
import couchdbProxy from "couchdb-auth-proxy";
19+
```
20+
21+
```js
22+
const couchdbProxy = require("couchdb-auth-proxy");
23+
```
24+
25+
## Usage
26+
27+
To begin, ensure proxy authentication is enabled on your CouchDB server. This is as simple as adding `{couch_httpd_auth, proxy_authentication_handler}` to the list of active authentication handlers in your configuration. Your `local.ini` file should have a line that looks something like this:
28+
29+
```ini
30+
[httpd]
31+
authentication_handlers = {couch_httpd_oauth, oauth_authentication_handler}, {couch_httpd_auth, cookie_authentication_handler}, {couch_httpd_auth, proxy_authentication_handler}, {couch_httpd_auth, default_authentication_handler}
32+
```
33+
34+
This library returns an Express/Connect middleware function. It accepts two arguments: a user context method and some options.
35+
36+
```js
37+
const app = express();
38+
39+
app.use(couchdbProxy(function(req) {
40+
// admin party!
41+
return {
42+
name: null,
43+
roles: [ "_admin" ]
44+
};
45+
}));
46+
```
47+
48+
In CouchDB, users are represented with a user context object. These are simply objects with `name` and `roles` fields. Usually this information comes from a document in the `_users` database, however we can also generate it from other means.
49+
50+
This library allows you to complete asynchronous authentication lookup, return a promise or pass `next` as the third argument. If you do use the `next()` callback, you absolutely must call it, or the request will never complete.
51+
52+
```js
53+
app.use(couchdbProxy(function(req, res, next) {
54+
const token = req.get("Authorization");
55+
56+
validateToken(token, function(err, user) {
57+
if (err) return next(err);
58+
59+
return {
60+
name: user.name,
61+
roles: []
62+
};
63+
});
64+
}));
65+
```
66+
67+
## API
68+
69+
#### `couchdbProxy( userCtxFn [, options ] ) → Middleware`
70+
71+
- `userCtxFn` (Function, *required*) - Method called on every request, with the request `req` and response `res` as arguments. This method should return a plain object with `name` and `roles` fields, representing the authenticated user. To run an async task, return a promise or pass a third argument `next` for a callback.
72+
- `options` (Object) - Options to configure the proxy.
73+
- `options.target` (String) - The URL of the CouchDB server to proxy to. This server must have [proxy authentication enabled](http://docs.couchdb.org/en/1.6.1/api/server/authn.html#proxy-authentication).
74+
- `options.secret` (String) - The [CouchDB secret](http://docs.couchdb.org/en/1.6.1/config/auth.html#couch_httpd_auth/secret) used to sign proxy tokens and cookies. This is very much an optional parameter and in general there is very little reason to use a secret. This is only absolutely required if `couch_httpd_auth/proxy_use_secret` is enabled on CouchDB.
75+
- `options.via` (String) - The name of the proxy to add to the `Via` header. This is so consumers of the HTTP API can tell that the request was directed through a proxy. This is optional and the `Via` header will be excluded when not provided.
76+
- `options.headerFields` (Object) - A map of custom header fields to use for the proxy. This should match what is declared in CouchDB `couch_httpd_auth` configuration, under `x_auth_roles`, `x_auth_token`, and `x_auth_username`. This is the default map:
77+
```json
78+
{
79+
"username": "X-Auth-CouchDB-UserName",
80+
"roles": "X-Auth-CouchDB-Roles",
81+
"token": "X-Auth-CouchDB-Token"
82+
}
83+
```

package.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "couchdb-auth-proxy",
3+
"version": "0.0.0-edge",
4+
"description": "An HTTP reverse proxy server for easy Couchdb proxy authentication",
5+
"author": "Tyler Johnson <[email protected]>",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/tyler-johnson/couchdb-auth-proxy.git"
9+
},
10+
"main": "index.js",
11+
"scripts": {
12+
"lint": "eslint src/ test/",
13+
"build": "make clean && make",
14+
"test": "make test",
15+
"prepublish": "npm run build",
16+
"autorelease": "autorelease pre && npm publish && autorelease post"
17+
},
18+
"dependencies": {
19+
"http-proxy": "^1.14.0",
20+
"transformer-proxy": "^0.3.3"
21+
},
22+
"devDependencies": {
23+
"autorelease": "^1.7.1",
24+
"autorelease-github": "^1.2.0",
25+
"autorelease-travis": "^1.2.1",
26+
"babel-eslint": "^6.1.2",
27+
"babel-plugin-external-helpers": "^6.8.0",
28+
"babel-plugin-transform-async-to-generator": "^6.8.0",
29+
"babel-plugin-transform-es2015-destructuring": "^6.9.0",
30+
"babel-plugin-transform-es2015-parameters": "^6.11.4",
31+
"babel-plugin-transform-object-rest-spread": "^6.8.0",
32+
"eslint": "^3.2.2",
33+
"rollup": "^0.34.3",
34+
"rollup-plugin-babel": "^2.6.1",
35+
"rollup-plugin-json": "^2.0.1"
36+
},
37+
"keywords": [],
38+
"license": "MIT"
39+
}

rollup.config.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import babel from "rollup-plugin-babel";
2+
import json from "rollup-plugin-json";
3+
4+
export default {
5+
onwarn: ()=>{},
6+
format: "cjs",
7+
plugins: [
8+
json(),
9+
babel({
10+
exclude: [ "node_modules/**" ],
11+
include: [ "src/**" ],
12+
plugins: [
13+
"external-helpers",
14+
"transform-es2015-destructuring",
15+
"transform-es2015-parameters",
16+
"transform-async-to-generator",
17+
"transform-object-rest-spread"
18+
]
19+
})
20+
]
21+
};

0 commit comments

Comments
 (0)