Authentication with node.js, nano and CouchDB
Asked Answered
F

2

6

Is there a way to change the config parameters in nano after initialization? I'd like to init nano with:

nano = require('nano')('http://127.0.0.1:5984')

and later change user and password, after a user submits the login form. I always get an error:

nano.cfg.user = params.user.name
TypeError: Cannot set property 'user' of undefined

Or should I fork nano and write an auth function to adjust the values?

Footmark answered 5/10, 2011 at 12:27 Comment(0)
F
2

The authentication can be send as part of the http header:

if(cfg.user && cfg.pass) {
  req.headers['Authorization'] = "Basic " + new Buffer(cfg.user+":"+cfg.pass).toString('base64');
}

The username and password can be set with a 'auth'-function:

function auth_db(user, password, callback) {
  cfg.user = user;
  cfg.pass = password;
  return relax({db: "_session", method: "GET"}, callback);
}
Footmark answered 6/10, 2011 at 12:9 Comment(0)
C
5

I can't test it right now, but, looking at the sources, you can note two things:

Then I think you need to set the url configuration option to a new value with authentication parameters:

nano.config.url = 'http://' + params.user.name + ':' + params.user.password + '@localhost:5984';

Or you can keep a configuration object as in couch.example.js and do something like:

cfg.user = params.user.name;
cfg.pass = params.user.password;
nano.config.url = cfg.url;

UPDATE: here's a complete example:

var cfg = {
  host: "localhost",
  port: "5984",
  ssl: false
};

cfg.credentials = function credentials() {
  if (cfg.user && cfg.pass) {
    return cfg.user + ":" + cfg.pass + "@";
  }
  else { return ""; }
};

cfg.url = function () {
  return "http" + (cfg.ssl ? "s" : "") + "://" + cfg.credentials() + cfg.host +
    ":" + cfg.port;
};

var nano = require('nano')(cfg.url()),
  db = nano.use('DB_WITH_AUTH'),
  docId = 'DOCUMENT_ID';

function setUserPass(user, pass) {
  cfg.user = user;
  cfg.pass = pass;
  nano.config.url = cfg.url();
}

db.get(docId, function (e, r, h) {
  if (e) {
    if (e['status-code'] === 401) {
      console.log("Trying again with authentication...");
      setUserPass('USENAME', 'PASSWORD');
      db.get(docId, function (e, r, h) {
        if (e) {
          console.log("Sorry, it did not work:");
          return console.error(e);
        }
        console.log("It worked:");
        console.log(r);
        console.log(h);
      });
      return;
    }
    console.log("Hmmm, something went wrong:");
    return console.error(e);
  }
  console.log("No auth required:");
  console.log(r);
  console.log(h);
});
Cooncan answered 6/10, 2011 at 9:32 Comment(3)
Unfortunately there is also the path generated from the url. I tried several possibilities and ended up putting the authentication the header.Footmark
@Footmark I'm not sure to understand what you mean, so I've added a working example to explain what I mean. I hope it helps clarify.Cooncan
Thank you very much, I tried that in the beginning. However, I found some source for the http-authentication, which is easier than rewriting the whole cfg-code. I requested a pull into the node repository, so I hope the update will be there soon.Footmark
F
2

The authentication can be send as part of the http header:

if(cfg.user && cfg.pass) {
  req.headers['Authorization'] = "Basic " + new Buffer(cfg.user+":"+cfg.pass).toString('base64');
}

The username and password can be set with a 'auth'-function:

function auth_db(user, password, callback) {
  cfg.user = user;
  cfg.pass = password;
  return relax({db: "_session", method: "GET"}, callback);
}
Footmark answered 6/10, 2011 at 12:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.