fediauth/onboard.lua

97 lines
3.6 KiB
Lua
Raw Normal View History

2023-09-27 10:57:15 +00:00
local FORMNAME = "fediauth-onboard"
local FORMNAMEFEDI = "fediauth-onboard-fedi"
2023-01-28 19:30:44 +00:00
2023-09-27 10:57:15 +00:00
local feditempstore = {}
function string.starts(String,Start)
return string.sub(String,1,string.len(Start))==Start
end
minetest.register_chatcommand("fediauth_off", {
description = "Disable the fediauth verification",
privs = { fediauth_enabled = true, interact = true },
2023-01-29 11:13:50 +00:00
func = function(name)
-- clear priv
local privs = minetest.get_player_privs(name)
2023-09-27 10:57:15 +00:00
privs.fediauth_enabled = nil
2023-01-29 11:13:50 +00:00
minetest.set_player_privs(name, privs)
2023-09-27 10:57:15 +00:00
return true, "fediauth login disabled"
2023-01-29 11:13:50 +00:00
end
})
2023-01-28 19:30:44 +00:00
2023-09-27 10:57:15 +00:00
minetest.register_chatcommand("fediauth_on", {
description = "Enable the fediauth verification",
privs = { interact = true },
2023-01-28 19:30:44 +00:00
func = function(name)
2023-09-27 10:57:15 +00:00
local secret_b32 = fediauth.get_player_secret_b32(name)
2023-02-02 13:34:50 +00:00
2023-09-27 10:57:15 +00:00
local formspec_account = "size[9,10]" ..
"label[1,7;Input your fediverse account handle]" ..
"image[1.5,0.6;7,7;fediverse.png]" ..
"field[1,9;4,1;fediverse_account_url;@nick@example.com;]" ..
"button[5,8.7;3,1;submit;Send code]"
2023-01-29 11:13:50 +00:00
2023-09-27 10:57:15 +00:00
minetest.show_formspec(name, FORMNAMEFEDI, formspec_account)
2023-01-29 11:13:50 +00:00
end
2023-01-28 19:30:44 +00:00
})
minetest.register_on_player_receive_fields(function(player, formname, fields)
2023-09-27 10:57:15 +00:00
if formname ~= FORMNAME and formname ~= FORMNAMEFEDI then
2023-01-28 19:30:44 +00:00
return
end
if fields.fediverse_account_url then
2023-09-27 10:57:15 +00:00
local playername = player:get_player_name()
-- basic prevent mention spam and limit length
if not string.starts(fields.fediverse_account_url, "@") or string.len(fields.fediverse_account_url) < 3 or string.len(fields.fediverse_account_url) > 100 then
minetest.chat_send_player(playername, minetest.colorize("#ff0000", "Try again, your input is incorrect"))
return
end
2023-09-29 01:11:18 +00:00
local fedihandle = fields.fediverse_account_url:split("@")
if #fedihandle ~= 2 then
minetest.chat_send_player(playername, minetest.colorize("#ff0000", "Incorrect format"))
return
end
if fediauth.check_for_restricted_instance(fedihandle[2]) then
2023-09-29 01:11:18 +00:00
minetest.chat_send_player(playername, minetest.colorize("#ff0000", fedihandle[2] .. " has restricted, try another..."))
return
end
2023-09-27 10:57:15 +00:00
local secret_b32 = fediauth.get_player_secret_b32(playername)
local codeseq = fediauth.give_code(secret_b32)
if fediauth.is_home_instance(fedihandle[2]) then
2023-10-02 18:21:59 +00:00
fediauth.send_code(codeseq[1], "@" .. fedihandle[1], playername)
else
2023-10-02 18:21:59 +00:00
fediauth.send_code(codeseq[1], "@" .. fedihandle[1] .. "@" .. fedihandle[2], playername)
end
2023-09-27 10:57:15 +00:00
feditempstore[playername] = fields.fediverse_account_url
local formspec = "size[9,10]" ..
"label[1,7;Check code on " .. minetest.formspec_escape(fields.fediverse_account_url) .. "]" ..
"field[1,9;4,1;code;Code;]" ..
"button_exit[5,8.7;3,1;submit;Verify]"
minetest.show_formspec(playername, FORMNAME, formspec)
end
2023-09-27 10:57:15 +00:00
2023-01-28 19:30:44 +00:00
if fields.code then
2023-01-29 11:13:50 +00:00
local playername = player:get_player_name()
2023-09-27 10:57:15 +00:00
local secret_b32 = fediauth.get_player_secret_b32(playername)
if fediauth.check_code(secret_b32, fields.code) then
2023-01-29 11:13:50 +00:00
-- set priv
local privs = minetest.get_player_privs(playername)
2023-09-27 10:57:15 +00:00
privs.fediauth_enabled = true
2023-01-29 11:13:50 +00:00
minetest.set_player_privs(playername, privs)
2023-09-27 10:57:15 +00:00
fediauth.verified_checkmark(player, true)
if feditempstore[playername] then
fediauth.storage:set_string(playername .. "_fedi", feditempstore[playername])
feditempstore[playername] = nil
end
2023-01-29 11:13:50 +00:00
2023-09-27 10:57:15 +00:00
minetest.chat_send_player(playername, "Code validation succeeded, fediauth login enabled")
2023-01-28 19:30:44 +00:00
else
2023-01-29 11:13:50 +00:00
minetest.chat_send_player(playername, "Code validation failed!")
2023-01-28 19:30:44 +00:00
end
end
2023-09-27 10:57:15 +00:00
end)