Compare commits

..
1 Commits
Author SHA1 Message Date
juhnsa 6fa2e49578 Resolve #1 2025-08-26 13:33:03 +02:00
3 changed files with 62 additions and 19 deletions

No files matched your search

+24 -3
View File
@@ -1,3 +1,5 @@
tg_server_name = "tg_server"
local args = {...}
local function split(str, divider)
@@ -21,7 +23,7 @@ local function establish_connection()
return false
end
function validate_clearance(computer_id, clearance_level, tg_server_name)
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)
@@ -36,12 +38,31 @@ function validate_clearance(computer_id, clearance_level, tg_server_name)
end
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()
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")
if msg == "authentication successful" then
return true
else
return false, "authentication error: "..msg
end
else
return false, "key file not found"
end
else
return false, "modem not found"
end
end
if fs.exists('/disk/key') then
key_file = fs.open('/disk/key', 'r')
key = key_file.readLine()
if establish_connection() then
local tg_server_name = "tg_server"
tg_server_id = rednet.lookup("tg", tg_server_name)
rednet.send(tg_server_id, "authenticate,"..key,"tg")
local id, msg, proto = rednet.receive("tg")
+1 -1
View File
@@ -1,7 +1,7 @@
local config = {
name = "tg_server",
modem_side = "top",
clearance_expiry = 600, -- Seconds until clearance will expire
clearance_expiry = 600, -- Minutes until clearance will expire
key_file = "verified_keys.txt", -- Name of the key file, relative to tg_server.lua
}
return config
+37 -15
View File
@@ -34,7 +34,28 @@ local function check_for_outdated_sessions()
#clearances .. " active session(s) remain.")
end
local function validate_key(card_holder, card_clearance, random_chars)
local card_hash = hash(card_holder..','..card_clearance..','..random_chars)
local key_file = fs.open(dir..config.key_file, 'r')
local card_authentic = false
while true do
local trusted_hash = key_file.readLine()
if trusted_hash == card_hash then
print("Valid card "..card_hash)
card_authentic = true
return true, card_hash
end
if not line then break end -- Stop when EOF reached
end
key_file.close()
if card_authentic == false then
print("Unknown card "..card_hash)
return false
end
end
rednet.open(config.modem_side)
rednet.host('tg', config.name)
@@ -69,28 +90,29 @@ while true do
print("Beginning transaction for authentication with "..id)
local card_holder = msg_parts[2]
local card_clearance = msg_parts[3]
local card_hash = hash(card_holder..','..card_clearance..','..msg_parts[4])
local key_file = fs.open(dir..config.key_file, 'r')
local card_authentic = false
while true do
local trusted_hash = key_file.readLine()
if trusted_hash == card_hash then
local is_authentic, card_hash = validate_key(card_holder, card_clearance, msg_parts[4])
if is_authentic then
table.insert(clearances, { computer_id = tonumber(id), clearance_level = tonumber(card_clearance), time_granted = os.clock() })
rednet.send(id, "authentication successful", "tg")
print("Granted card "..card_hash.." clearance level "..card_clearance)
card_authentic = true
end
if not line then break end -- Stop when EOF reached
end
key_file.close()
if card_authentic == false then
else
print("Unknown card "..card_hash)
rednet.send(id, "authentication failed", "tg")
end
print("Transaction finished")
elseif msg_parts[1] == 'validate_key' then
print("Beginning transaction for single-validation with "..id)
local card_holder = msg_parts[2]
local card_clearance = msg_parts[3]
local minimum_clearance = msg_parts[5]
if validate_key(card_holder, card_clearance, msg_parts[4]) and tonumber(card_clearance) >= tonumber(minimum_clearance) then
rednet.send(id, "authentication successful", "tg")
else
rednet.send(id, "authentication failed", "tg")
end
print("Transaction finished")
end
end