couchdb-auth-proxy
A Node.js HTTP reverse proxy library for quick and dirty CouchDB proxy authentication.
Install
Install from NPM
npm install couchdb-auth-proxy -S
And import into your project
import couchdbProxy from "couchdb-auth-proxy";
const couchdbProxy = require("couchdb-auth-proxy");
Usage
Note: 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. See the CouchDB Docs for more info.
This library generates an HTTP server request function from two arguments: a user context method and some options. This method will work with Express/Connect apps as well as the plain Node.js HTTP server.
Here is an example proxy that authenticates every request as a super admin:
const server = http.createServer(couchdbProxy(function(req) {
return {
name: null,
roles: [ "_admin" ]
};
}));
In CouchDB, users are represented with a user context object. These are 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.
Your proxy can complete asynchronous tasks, great for authenticating against other databases or services. You can return a promise, or provide a third argument for a callback.
const server = http.createServer(couchdbProxy(function(req, res, next) {
const token = req.get("Authorization");
db.authenticateToken(token, (err, user) => {
if (err) return next(err);
next(null, {
name: user.name,
roles: []
});
});
}));
API
couchdbProxy( userCtxFn [, options ] ) → Middleware