add mongo config option: authdb, see issue #884

This commit is contained in:
Cloud Wu
2018-08-29 19:52:41 +08:00
committed by 云风
parent 8ce816de7f
commit 1c8ed8a46e

View File

@@ -87,13 +87,17 @@ local function mongo_auth(mongoc)
local pass = rawget(mongoc, "password") local pass = rawget(mongoc, "password")
local authmod = rawget(mongoc, "authmod") or "scram_sha1" local authmod = rawget(mongoc, "authmod") or "scram_sha1"
authmod = "auth_" .. authmod authmod = "auth_" .. authmod
local authdb = rawget(mongoc, "authdb")
if authdb then
authdb = mongo_client.getDB(mongoc, authdb) -- mongoc has not set metatable yet
end
return function() return function()
if user ~= nil and pass ~= nil then if user ~= nil and pass ~= nil then
-- autmod can be "mongodb_cr" or "scram_sha1" -- autmod can be "mongodb_cr" or "scram_sha1"
local auth_func = mongoc[authmod] local auth_func = mongoc[authmod]
assert(auth_func , "Invalid authmod") assert(auth_func , "Invalid authmod")
assert(auth_func(mongoc,user, pass)) assert(auth_func(authdb or mongoc, user, pass))
end end
local rs_data = mongoc:runCommand("ismaster") local rs_data = mongoc:runCommand("ismaster")
if rs_data.ok == 1 then if rs_data.ok == 1 then
@@ -156,6 +160,7 @@ function mongo.client( conf )
username = first.username, username = first.username,
password = first.password, password = first.password,
authmod = first.authmod, authmod = first.authmod,
authdb = first.authdb,
} }
obj.__id = 0 obj.__id = 0