使用 node.js、nano 和 CouchDB 进行身份验证

发布于 2024-12-08 10:33:48 字数 307 浏览 0 评论 0 原文

有没有办法在初始化后更改nano中的配置参数?我想用以下命令初始化 nano:

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

然后在用户提交登录表单后更改用户和密码。我总是收到错误:

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

或者我应该分叉 nano 并编写一个 auth 函数来调整值?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼藏柔 2024-12-15 10:33:48

我现在无法测试它,但是,查看源代码,您可以注意到两件事:

然后我认为您需要使用身份验证参数将 url 配置选项设置为新值:

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

或者您可以保留配置对象,如 couch.example.js 并执行以下操作:

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

更新:这是一个完整的示例:

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);
});

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);
});
倾城月光淡如水﹏ 2024-12-15 10:33:48

身份验证可以作为 http 标头的一部分发送:

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

用户名和密码可以使用“auth”函数设置:

function auth_db(user, password, callback) {
  cfg.user = user;
  cfg.pass = password;
  return relax({db: "_session", method: "GET"}, callback);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文