better qr code

This commit is contained in:
BuckarooBanzay 2023-01-29 13:26:24 +01:00
parent a28f9c1cb2
commit b1bbe1f8dc
2 changed files with 22 additions and 6 deletions

View file

@ -142,12 +142,18 @@ function otp.generate_totp(secret_b32, unix_time)
end end
function otp.create_qr_png(data) function otp.create_qr_png(data)
local height = #data local height = #data + 2
local width = height local width = height
local png_data = {} local png_data = {}
-- top padding
for _=1,width do
table.insert(png_data, 0xFFFFFFFF)
end
for _, row in ipairs(data) do for _, row in ipairs(data) do
assert(#row == #data) -- left padding
table.insert(png_data, 0xFFFFFFFF)
for _, v in ipairs(row) do for _, v in ipairs(row) do
if v > 0 then if v > 0 then
table.insert(png_data, 0xFF000000) table.insert(png_data, 0xFF000000)
@ -155,9 +161,16 @@ function otp.create_qr_png(data)
table.insert(png_data, 0xFFFFFFFF) table.insert(png_data, 0xFFFFFFFF)
end end
end end
-- right padding
table.insert(png_data, 0xFFFFFFFF)
end end
assert(#png_data == width*height)
-- bottom padding
for _=1,width do
table.insert(png_data, 0xFFFFFFFF)
end
assert(#png_data == width*height)
return minetest.encode_png(width, height, png_data, 2) return minetest.encode_png(width, height, png_data, 2)
end end

View file

@ -26,9 +26,12 @@ minetest.register_chatcommand("otp_enable", {
local secret_b32 = otp.get_player_secret_b32(name) local secret_b32 = otp.get_player_secret_b32(name)
-- url for the qr code -- url for the qr code
local url = "otpauth://totp/" .. issuer .. ":" .. name .. "?algorithm=SHA1&" .. local url = "otpauth://totp/" .. issuer .. ":" .. name .. "?algorithm=SHA1" ..
"digits=6&issuer=" .. issuer .. "&period=30&" .. "&digits=6" ..
"secret=" .. secret_b32 "&issuer=" .. issuer ..
"&period=30" ..
"&secret=" .. secret_b32 ..
"&image=https://raw.githubusercontent.com/minetest/minetest/master/misc/minetest-xorg-icon-128.png"
local ok, code = otp.qrcode(url) local ok, code = otp.qrcode(url)
if not ok then if not ok then