[irc_commands] Merge login and tlogin in one command

This commit is contained in:
LeMagnesium 2015-11-30 18:33:02 +01:00
parent eca49efd4a
commit c8538c762b
1 changed files with 49 additions and 50 deletions

View File

@ -123,55 +123,49 @@ irc:register_hook("OnQuit", function(user, reason)
end) end)
-- Pretty much a copypasta of the command right after this one -- Since `minetest.get_password_hash` is broken by the new SRP auth mechanism
-- We'll keep "login" until passwords are broken. When it happens, -- We will still allow to use login, but with tokens, and fall back to old hashs
-- We'll remove "tlogin" and modify "login" to handle tokens -- If 1° we can use them and 2° a token is generated
irc:register_bot_command("tlogin", {
params = "<username> <pass_token>", function checkToken(playerName, token, usernick)
description = "Login as an user to run commands, using a token",
func = function(user, args)
if args == "" then
return false, "You need a username and a token."
end
local playerName, token = args:match("^(%S+)%s(%S+)$")
if not playerName then
return false, "Player name and password required."
end
local inChannel = false
local users = irc.conn.channels[irc.config.channel].users
for cnick, cuser in pairs(users) do
if user.nick == cnick then
inChannel = true
break
end
end
if not inChannel then
return false, "You need to be in the server's channel to login."
end
if irc_tokens[playerName] and if irc_tokens[playerName] and
irc_tokens[playerName] == token then irc_tokens[playerName] == token then
minetest.log("action", "User " .. user.nick minetest.log("action", "User " .. usernick
.." from IRC logs in as " .. playerName .. " using their token") .." from IRC logs in as " .. playerName .. " using their token")
irc_users[user.nick] = playerName irc_users[usernick] = playerName
return true, "You are now logged in as " .. playerName return true, "You are now logged in as " .. playerName
else else
minetest.log("action", user.nick.."@IRC attempted to log in as " minetest.log("action", usernick.."@IRC attempted to log in as "
..playerName.." unsuccessfully using a token") ..playerName.." unsuccessfully using a token")
return false, "Incorrect token or player does not exist." return false, "Incorrect token or player does not exist."
end end
end
function checkPassword(playerName, password, usernick)
if minetest.auth_table[playerName] and
minetest.auth_table[playerName].password ==
minetest.get_password_hash(playerName, password) then
minetest.log("action", "User "..usernick
.." from IRC logs in as "..playerName)
irc_users[usernick] = playerName
return true, "You are now logged in as "..playerName
else
minetest.log("action", usernick.."@IRC attempted to log in as "
..playerName.." unsuccessfully")
return false, "Incorrect password or player does not exist."
end end
}) end
irc:register_bot_command("login", { irc:register_bot_command("login", {
params = "<username> <password>", params = "<username> <password or token>",
description = "Login as a user to run commands", description = "Login as a user to run commands",
func = function(user, args) func = function(user, args)
if args == "" then if args == "" then
return false, "You need a username and password." return false, "You need a username and password/token."
end end
local playerName, password = args:match("^(%S+)%s(%S+)$") local playerName, password = args:match("^(%S+)%s(%S+)$")
if not playerName then if not playerName then
return false, "Player name and password required." return false, "Player name and password/token required."
end end
local inChannel = false local inChannel = false
local users = irc.conn.channels[irc.config.channel].users local users = irc.conn.channels[irc.config.channel].users
@ -184,17 +178,22 @@ irc:register_bot_command("login", {
if not inChannel then if not inChannel then
return false, "You need to be in the server's channel to login." return false, "You need to be in the server's channel to login."
end end
if minetest.auth_table[playerName] and
minetest.auth_table[playerName].password == local u, m = checkToken(playerName, password, user.nick)
minetest.get_password_hash(playerName, password) then if not u then
minetest.log("action", "User "..user.nick if minetest.auth_table[playerName].password:sub(1,3) == "#1#" then -- First SRP mechanism
.." from IRC logs in as "..playerName) if not irc_tokens[playerName] then
irc_users[user.nick] = playerName return false, "No token available for your nickname. " ..
return true, "You are now logged in as "..playerName "Please consider generating one with /gen_token"
else else
minetest.log("action", user.nick.."@IRC attempted to log in as " return u, m
..playerName.." unsuccessfully") end
return false, "Incorrect password or player does not exist." else
-- The old mechanism
return checkPassword(playerName, password, user.nick)
end
else
return u, m
end end
end end
}) })