Permalink
Browse files

Added support for cookie authentication

Can now add `cookie` to nano configuration (updated read me
accordingly). Also fixed a typo on line 815.
  • Loading branch information...
1 parent 5b94fee commit d99aa582976c222c08430e0a0c0855a0d532ed14 Jonathan Mahoney committed May 15, 2012
Showing with 32 additions and 1 deletion.
  1. +26 −0 README.md
  2. +6 −1 nano.js
View
26 README.md
@@ -123,6 +123,32 @@ a very important configuration parameter if you have a high traffic website and
you can increase the size using `request_options` if this is problematic, and refer to the [request] documentation and examples for further clarification
+### using cookie authentication
+
+nano supports making requests using couchdb [cookie authentication](http://guide.couchdb.org/editions/1/en/security.html#cookies). Once authenticated, all nano operations can be performed as usual, but with the use of cookie authentication - simply by adding the cookie value to your configuration options:
+
+``` js
+var db = require('nano')({
+ "url" : "http://localhost:5984/foo"
+ , "cookie" : "couchdb_cookie_value_here"
+});
+```
+
+because couchdb cookies have a sliding expiry, it is important that you check for any updated 'set-cookie' values on response. For example:
+
+``` js
+db.insert(doc, null,
+ function (err, body, headers) {
+ if (headers && headers['set-cookie']) {
+ res.cookie('cookie_name', JSON.stringify(headers['set-cookie']));
+ }
+ if (!err) {
+ res.send('ok');
+ }
+ }
+ );
+```
+
## database functions
### nano.db.create(name, [callback])
View
7 nano.js
@@ -162,6 +162,11 @@ module.exports = exports = nano = function database_module(cfg) {
delete req.headers.accept; // undo headers set
}
+ if(cfg.cookie) {
+ req.headers["X-CouchDB-WWW-Authenticate"] = "Cookie";
+ req.headers.cookie = cfg.cookie;
+ }
+
// these need to be encoded
if(!isEmpty(params)) {
try {
@@ -807,7 +812,7 @@ module.exports = exports = nano = function database_module(cfg) {
// please send pull requests if you want to use a option
// in request that is not exposed
if(cfg.request_defaults) {
- request = require('request').defaults(request_defaults);
+ request = require('request').defaults(cfg.request_defaults);
}
// assuming a cfg.log inside cfg

0 comments on commit d99aa58

Please sign in to comment.