Improved disk login logic safety.

This commit is contained in:
Andrew Lalis 2023-08-29 17:25:57 -04:00
parent 2d458615ec
commit 6a8dea7b95
1 changed files with 28 additions and 15 deletions

43
atm.lua
View File

@ -18,35 +18,47 @@ local function drawFrame()
g.drawText(term, 2, 1, "ATM", colors.white, colors.black) g.drawText(term, 2, 1, "ATM", colors.white, colors.black)
end end
local function tryReadDiskCredentials(name)
if disk.hasData(name) then
local dataFile = fs.combine(disk.getMountPath(name), "bank-credentials.json")
if fs.exists(dataFile) then
local f = io.open(dataFile, "r")
local content = textutils.unserializeJSON(f:read("*a"))
f:close()
if (
content ~= nil and
content.username and
type(content.username) == "string" and
content.password and
type(content.password) == "string"
) then
return content
end
end
end
return nil
end
local function showLoginUI() local function showLoginUI()
drawFrame() drawFrame()
g.drawTextCenter(term, W/2, 3, "Welcome to HandieBank ATM!", colors.green, colors.white) g.drawTextCenter(term, W/2, 3, "Welcome to HandieBank ATM!", colors.green, colors.white)
g.drawTextCenter(term, W/2, 5, "Insert your card below, or click to login.", colors.black, colors.white) g.drawTextCenter(term, W/2, 5, "Insert your card below, or click to login.", colors.black, colors.white)
g.fillRect(term, W/2 - 3, 7, 9, 3, colors.green) g.fillRect(term, 22, 7, 9, 3, colors.green)
g.drawTextCenter(term, W/2, 8, "Login", colors.white, colors.green) g.drawTextCenter(term, W/2, 8, "Login", colors.white, colors.green)
while true do while true do
local event, p1, p2, p3 = os.pullEvent() local event, p1, p2, p3 = os.pullEvent()
if event == "disk" then if event == "disk" then
local side = p1 local credentials = tryReadDiskCredentials(p1)
if disk.hasData(side) then if credentials then
local mountPath = disk.getMountPath(side) return credentials
local dataFile = fs.combine(disk.getMountPath(side), "bank-credentials.json")
if fs.exists(dataFile) then
local f = io.open(dataFile, "r")
local content = f:read("*a")
f:close()
return textutils.unserializeJSON(content)
else
disk.eject(side)
end
else else
disk.eject(side) disk.eject(p1)
end end
elseif event == "mouse_click" then elseif event == "mouse_click" then
local button = p1 local button = p1
local x = p2 local x = p2
local y = p3 local y = p3
if button == 1 and x >= (W/2 - 3) and x <= (W/2 + 4) and y >= 7 and y <= 9 then if button == 1 and x >= 22 and x <= 30 and y >= 7 and y <= 9 then
-- TODO: Show login input elements. -- TODO: Show login input elements.
return {username = "bleh", password = "bleh"} return {username = "bleh", password = "bleh"}
end end
@ -57,5 +69,6 @@ end
while true do while true do
local credentials = showLoginUI() local credentials = showLoginUI()
g.clear(term, colors.black) g.clear(term, colors.black)
print("Credentials: " .. textutils.serialize(credentials))
return return
end end