From a64c72d4fc04ceb24cad80ad5e8bd4105c2f2123 Mon Sep 17 00:00:00 2001 From: juhnsa Date: Tue, 26 Aug 2025 14:55:19 +0200 Subject: [PATCH] Extend client api by authentication --- tg_client/tg_client.lua | 61 ++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/tg_client/tg_client.lua b/tg_client/tg_client.lua index da9228f..ecd9262 100644 --- a/tg_client/tg_client.lua +++ b/tg_client/tg_client.lua @@ -15,18 +15,16 @@ local function establish_connection() for _, side in ipairs(peripherals) do if peripheral.getType(side) == "modem" then rednet.open(side) - print("Opened modem: "..side) return true end end - print("No modem found!") return false end function validate_clearance(computer_id, clearance_level) if establish_connection() then - tg_server_id = rednet.lookup("tg", tg_server_name) - rednet.send(tg_server_id, "validate_clearance,"..computer_id..","..clearance_level) + tg_server_id = rednet.lookup('tg', tg_server_name) + rednet.send(tg_server_id, "validate_clearance,"..computer_id..","..clearance_level,"tg") local id, msg, proto = rednet.receive("tg") if msg == "granted" then return true @@ -41,11 +39,11 @@ end function validate_key(minimum_clearance) if establish_connection() then if fs.exists("/disk/key") then - key_file = fs.open("/disk/key","r") - key = key_file.readLine() + local key_file = fs.open("/disk/key","r") + local key = key_file.readLine() tg_server_id = rednet.lookup('tg', tg_server_name) rednet.send(tg_server_id, "validate_key,"..key..","..minimum_clearance, "tg") - id, msg, _ = rednet.receive("tg") + local id, msg, _ = rednet.receive("tg") if msg == "authentication successful" then return true else @@ -59,27 +57,40 @@ function validate_key(minimum_clearance) end end -if fs.exists('/disk/key') then - key_file = fs.open('/disk/key', 'r') - key = key_file.readLine() - if establish_connection() then - tg_server_id = rednet.lookup("tg", tg_server_name) - rednet.send(tg_server_id, "authenticate,"..key,"tg") - local id, msg, proto = rednet.receive("tg") +function authenticate() + if fs.exists('/disk/key') then + local key_file = fs.open('/disk/key', 'r') + local key = key_file.readLine() + if establish_connection() then + tg_server_id = rednet.lookup("tg", tg_server_name) + rednet.send(tg_server_id, "authenticate,"..key,"tg") + local id, msg, proto = rednet.receive("tg") - if msg == "authentication successful" then - key_parts = split(key,',') - local card_holder = key_parts[1] - local card_clearance = key_parts[2] - print("Authentication was successful.") - print("Hello "..card_holder..", you were granted clearance level "..card_clearance.." for this computer.") + if msg == "authentication successful" then + local key_parts = split(key,',') + local card_holder = key_parts[1] + local card_clearance = key_parts[2] + return true, _, card_holder, card_clearance + else + print("Authentication failed.") + return false, "authentication failed" + end else - print("Authentication failed.") + return false, "modem not found" end + key_file.close() else - return false, "modem not found" + print("Key file not found. Aborting.") + return false, "key file not found" + end +end + +if args[1] == "auth" or args[1] == "authenticate" then + local success, error, card_holder, card_clearance = authenticate() + if success then + print("Authentication was successful.") + print("Hello "..card_holder..", you were granted clearance level "..card_clearance.." for this computer.") + else + print("Authentication failed: "..error) end - key_file.close() -else - print("Key file not found. Aborting.") end \ No newline at end of file