【问题标题】:CouchDB with Auth0带有 Auth0 的 CouchDB
【发布时间】:2017-01-12 21:25:02
【问题描述】:

是否可以将 Auth0 与 CouchDB 或 Cloudant 一起使用?如果是这样,有人知道教程、代码示例或 github 项目的示例吗?

这个问题已经在 Auth0-Forum 中提出(不是我提出的),但没有回应:https://auth0.com/forum/t/can-you-use-auth0-with-couchdb-or-cloudant/3127

在我的特殊情况下,我想将带有 Auth0 的 Ionic 2 应用程序连接到没有中间 (API) 层的 CouchDB 实例。

任何帮助将不胜感激!

【问题讨论】:

    标签: couchdb ionic2 auth0


    【解决方案1】:

    用户会在 couchdb 中拥有自己的数据库吗? 因为没有服务器端中间件,您将无法限制仅访问用户数据。 如果是这种情况,您可以考虑使用 oauth。

    我对 Auth0 并不深入,但它似乎支持它https://auth0.com/docs/oauth2-examples 也是 CouchDB http://docs.couchdb.org/en/stable/api/server/authn.html#oauth-authentication

    【讨论】:

    • 感谢您的回复。我希望每个用户都有一个自己的数据库,因为在客户端我想使用 pouchdb。
    【解决方案2】:

    couch_jwt_auth 插件允许用户通过 JWT 令牌进行身份验证,因此允许这种情况。甚至还有一个example project 介绍如何将它与 pouchdb 和 Auth0 一起使用,所以猜猜这​​对你可能有点用处。

    我正在使用相同的插件来验证用户身份,它可以完美运行。我实际上维护了一个fork,它允许使用非对称密钥 (RS256) 进行 JWT 签名验证 - 是的,一旦我有足够的信心,就会有一个拉取请求。

    【讨论】:

    • 感谢您的回复。这个项目对我来说似乎很有趣。是否可以将 couch_jwt_auth 插件与 Cloudant 一起使用?
    • 抱歉,我没有使用 cloudant 的经验。我不确定它是否提供与 couchdb 相同的插件 API,或者是否可以在您的 cloudant 实例上安装插件。
    • 我会接受你的回答。让它在 Cloudant 上运行是另一个主题!
    • 刚意识到我一开始就打开了一个 WIP 拉取请求。我正在尝试将其合并,如果您对非对称密钥感兴趣,请随时查看:github.com/softapalvelin/couch_jwt_auth/pull/4
    • 为什么不使用 _users 数据库?这似乎是使用沙发的主要好处..
    【解决方案3】:

    试试这个:https://github.com/softapalvelin/couch_jwt_auth 这个插件允许用户通过 JWT 令牌进行身份验证。

    【讨论】:

    • 为什么不使用沙发用户 db?
    【解决方案4】:

    CouchDB 3.1 支持开箱即用的 JWT 身份验证。

    Couch docs on JWT auth are here

    基本上,更新配置的 [chttpd] 部分以包含下面的 JWT 部分。好吧,您可以删除默认和/或 cookie,但保留它们非常有用:

    [chttpd]
    authentication_handlers = {chttpd_auth, cookie_authentication_handler}, {chttpd_auth, jwt_authentication_handler}, {chttpd_auth, default_authentication_handler}
    

    然后添加此部分,文档中没有示例提及:

    [jwt_auth]
    ; List of claims to validate
    ; As of couch 3.1, can NOT require issuer:
    ; required_claims = {iss, "http://issuer"} <-- does not work
    ; I left it blank and it's fine
    required_claims =
    

    然后添加或取消注释此部分:

    [jwt_keys]
    ; Configure at least one key here if using the JWT auth handler.
    ; I found I had to inclue the 'kid' claim, even if it is just "_default".
    ; REMINDER: BASE64 ENCODED!
    hmac:_default = aGVsbG8=
    hmac:demo = aGVsbG8=
    

    那是“你好”,base64 编码。

    要为此获取 JWT,请执行以下操作:

    // once off...
    const secret = 'hello'
    
    // then, in an express-jwt handler somewhere...
    const token = sign(data, secret, { subject: user.name, audience, algorithm, keyid: 'demo' });
    return res.send({ error: null, token })
    

    我发现主题 (sub)、受众 (aud)、算法 (alg) 和 keyid (kid) 都必须包含在 JWT 声明中,否则 CouchDB 不会接受身份验证。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-13
      • 2018-05-17
      • 2023-04-05
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多