lua-resty-http-digest
OpenResty 的 Lua HTTP Digest 访问认证
$ opm get knight42/lua-resty-http-digest
名称
lua-resty-http-digest
状态
beta
描述
lua-resty-http-digest 库在 OpenResty 的 Lua 中实现了“HTTP Digest 访问认证”(由 RFC7616 规范)。
概要
lua_package_path "/path/to/lua-resty-http-digest/lib/?.lua;;";
location = /protected {
access_by_lua_block {
local http_digest = require 'resty.http_digest'
local t, err = http_digest:new({
get_password = function(name) return 'root' end,
realm = 'example.com',
nonce_age = 60,
auth_timeout = 60,
max_replays = 20,
algorithm = http_digest.MD5,
redis = {
db = '0',
host = '127.0.0.1',
port = 6379,
timeout = 1000,
keepalive_idle_timeout = 20000,
keepalive_pool_size = 5,
},
})
if err then
ngx.status = 500
ngx.log(ngx.ERR, err)
ngx.exit(500)
end
local auth, err = t:authenticate()
if ngx.status == ngx.HTTP_UNAUTHORIZED then
-- send challenge
ngx.exit(ngx.status)
end
if err then
ngx.log(ngx.ERR, err)
ngx.exit(ngx.status)
end
ngx.say('Welcome ' .. auth.username)
}
}
方法
new
语法: t, err = class:new(opts)
创建一个认证器对象。错误时返回 nil
和错误信息字符串。
它接受一个 opts
表格参数。支持以下选项
get_password
: 一个函数,用于根据给定的用户名返回(password, error)
。必选。函数签名为:
(username: string) -> (password: string, error)
realm
可选。默认:
ngx.var.http_host
nonce_age
: nonce 的 TTL(生存时间),单位为秒可选。默认:
60
auth_timeout
: 指定服务器向客户端发送挑战后,挑战保持有效的秒数。可选。默认:
60
max_replays
: nonce 可以重用的次数。可选。默认:
20
algorithm
可选。默认:
http_digest.MD5
。可用值:http_digest.MD5
,http_digest.SHA256
redis.db
可选。默认:
0
redis.host
可选。默认:
127.0.0.1
redis.port
可选。默认:
6379
redis.timeout
可选。默认:
1000
(1 秒)
redis.keepalive_idle_timeout
可选。默认:
nil
。另请参阅 https://github.com/openresty/lua-resty-redis#set_keepalive
redis.keepalive_pool_size
可选。默认:
nil
另请参阅 https://github.com/openresty/lua-resty-redis#set_keepalive
authenticate
语法: info, err = t:authenticate()
验证 Authorization
头并返回从 Authorization
头提取的信息。如果发生错误,它将设置相应的状态代码并返回错误消息。
安装
限制
realm
不能包含空格字符或引号仅支持
MD5
和SHA256
算法,未实现*-sess
算法仅支持
auth
qop
作者
GitHub @knight42
许可证
lua-resty-http-digest 采用 MIT 许可证。
作者
曾健 (knight42)
许可证
mit
版本
-
OpenResty 的 Lua HTTP Digest 访问认证 2019-12-24 05:38:05