如何在 Cloudant 的 _user DB 中分配角色

Mor*_*yes 4 javascript couchdb node.js express cloudant

如何向 Cloudant 用户数据库中的用户添加角色?(_users) 我无法使用 Google 或 Cloudant Docs 解决这个问题。我看到有人提到 Cloudant _user 数据库,但尚未找到如何使用它。

我用nodejs和express构建了一个API。和

const { CloudantV1 } = require('@ibm-cloud/cloudant');
const { BasicAuthenticator } = require('ibm-cloud-sdk-core');
Run Code Online (Sandbox Code Playgroud)

我可以使用 service.putCloudantSecurityConfiguration() 创建 ApiKey 并分配角色。

service.getSecurity() 的结果如下:

cloudant: {
'apikey-01beebbe10ae46ad9e86cc16a2937939': [ '_replicator', '_writer', '_reader' ],
'apikey-3044abd26f324792a8be5809a5521400': [ '_writer', '_reader' ],
'apikey-8d98aa26f1d246f6b736f313bd45d630': [ '_writer', '_reader' ],
'apikey-231cdadcf38945e9ab48125adddc0fdb': [],
'apikey-7ec3ebdd9c5b4aa691685fae251a255d': [ '_writer', '_reader' ],
'apikey-061e3a0ae05d486583dda500ee6685f6': [ '_writer', '_reader' ],
'apikey-0324472243bd4c0da6ea6e9022e102c8': [ '_writer', '_reader' ]
Run Code Online (Sandbox Code Playgroud)

}

当我查看 service.getSessionInformation(); 时 结果,我看到:

"result": {
  "userCtx": {
    "roles": [],
    "name": "apikey-01beebbe10ae46ad9e86cc16a2937939"
  },
  "ok": true,
  "info": {
    "authentication_handlers": [
      "iam",
      "cookie",
      "default",
      "local"
    ],
    "authenticated": "default",
    "authentication_db": "bm-cc-us-south-18/users"
  }
}
Run Code Online (Sandbox Code Playgroud)

角色数组为空!

我想将这些新的 apikey 凭据与 Pouchdb 一起使用,如下所示:

    localDB.replicate.to(https://<apikey>:<pass>@<cloudantURL>/<dbname>
    ).on('complete', function () {
      // yay, we're done!
    }).on('error', function (err) {
      // boo, something went wrong!
    });
Run Code Online (Sandbox Code Playgroud)

我收到错误:

消息:“您无权访问此数据库。”

感谢您的帮助。

Gly*_*ird 5

正如您所发现的,Cloudant 允许您创建能够访问一个或多个数据库的 api 密钥/密码对。您可以针对每个数据库设置每个 API 密钥的权限,就像您已经完成的那样。

端点GET /session(您使用service.getSessionInformation()JavaScript 函数访问的端点)不会在数组中返回 API 密钥的权限roles,但不要推迟 - 您的 API 密钥确实具有您在第一步中定义的权限,因此您应该能够使用该 API 密钥正常读取/写入/查询数据。

请注意,此身份验证机制独立于 CouchDB 风格的_users数据库。


例如使用 PouchDB

const PouchDB = require('pouchdb')
const db = new PouchDB('test')
const username = 'apikey-somevalue'
const password = 'somepassword'
const hostname = 'myhost.cloudant.com'
const dbname = 'pouch'
const remoteDB = `https://${username}:${password}@${hostname}/${dbname}`
    
db.sync(remoteDB).on('complete', async function () { 
 console.log('done')
 const docs = await db.allDocs()
 console.log(docs)
}).on('error', function (err) {
  console.error(err)
});
Run Code Online (Sandbox Code Playgroud)