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 不会接受身份验证。