Merge pull request 'Resolve #1' (#3) from issue-1 into main

Reviewed-on: #3
This commit was merged in pull request #3.
This commit is contained in:
Juhn-Vitus Saß committed 2025-08-26 13:35:40 +02:00
commit 6d063ddf97
2 files changed
+61 -18

No files matched your search

+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