Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Constants for security configuration
- local MAX_ATTEMPTS = 3 -- Maximum number of incorrect password attempts allowed
- local LOCKOUT_TIME = 30 -- Lockout time in seconds after reaching maximum attempts
- local USERS_FOLDER = "/disk/users/"
- local ERROR_FOLDER = "/disk/error/"
- local BSOD_PROGRAM = "BSOD.lua"
- local CURRENT_USER_FILE = ".currentusr"
- local SHOW_ALL_USERS_FILE = "/disk/config/security/login/ShowAllUsers.cfg"
- -- Utility function to draw a centered popup window with a text-based border
- local function drawPopupWindow(headerText, contentLines, windowWidth, windowHeight)
- term.clear()
- local w, h = term.getSize()
- -- Determine dimensions of the popup
- local maxLength = #headerText
- for _, line in ipairs(contentLines) do
- maxLength = math.max(maxLength, #line)
- end
- windowWidth = windowWidth or (maxLength + 4)
- windowHeight = windowHeight or (#contentLines + 4)
- local xStart = math.floor(w / 2 - windowWidth / 2)
- local yStart = math.floor(h / 2 - windowHeight / 2)
- -- Draw border with animation
- term.setCursorPos(xStart, yStart)
- term.write("+" .. string.rep("-", windowWidth - 2) .. "+")
- for i = 1, windowHeight - 2 do
- term.setCursorPos(xStart, yStart + i)
- term.write("|" .. string.rep(" ", windowWidth - 2) .. "|")
- os.sleep(0.05)
- end
- term.setCursorPos(xStart, yStart + windowHeight - 1)
- term.write("+" .. string.rep("-", windowWidth - 2) .. "+")
- -- Draw header
- term.setCursorPos(xStart + 2, yStart + 1)
- term.write(headerText)
- -- Draw content
- for i, line in ipairs(contentLines) do
- term.setCursorPos(xStart + 2, yStart + 1 + i + 1)
- term.write(line)
- os.sleep(0.05)
- end
- end
- -- Function to reset the password
- local function resetPassword(username)
- drawPopupWindow("Password Reset", {"Select a recovery method:"})
- print("1. Insert a valid security card")
- print("2. Enter a recovery key")
- write("Option: ")
- local choice = read()
- if choice == "1" then
- -- Security card method
- if insertSecurityCard(username) then
- drawPopupWindow("Password Reset", {"Security card verified."})
- else
- drawPopupWindow("Password Reset Failed", {"Invalid or missing security card."})
- return
- end
- elseif choice == "2" then
- -- Recovery key method
- local recoveryKeyPath = fs.combine(USERS_FOLDER .. username, "rckey.txt")
- if not fs.exists(recoveryKeyPath) then
- drawPopupWindow("Password Reset Failed", {"Recovery key not found."})
- return
- end
- drawPopupWindow("Password Reset", {"Enter your recovery key:"})
- write("Recovery Key: ")
- local enteredKey = read("*")
- local file = fs.open(recoveryKeyPath, "r")
- local storedKey = file.readLine()
- file.close()
- if enteredKey ~= storedKey then
- drawPopupWindow("Password Reset Failed", {"Incorrect recovery key."})
- return
- end
- else
- drawPopupWindow("Password Reset Failed", {"Invalid option selected."})
- return
- end
- -- Request new password
- drawPopupWindow("Password Reset", {"Enter your new password:"})
- write("New Password: ")
- local newPassword = read("*")
- write("Confirm Password: ")
- local confirmPassword = read("*")
- if newPassword ~= confirmPassword then
- drawPopupWindow("Password Reset Failed", {"Passwords do not match."})
- return
- end
- -- Update the password
- local passwordFile = fs.combine(USERS_FOLDER .. username, "password.txt")
- local file = fs.open(passwordFile, "w")
- file.write(newPassword)
- file.close()
- drawPopupWindow("Password Reset Successful", {"Your password has been updated."})
- os.sleep(2)
- saveCurrentUser(username)
- shell.run("/disk/os/gui")
- end
- -- Function to list all users
- local function listUsers()
- local users = fs.list(USERS_FOLDER)
- local usernames = {}
- for _, user in ipairs(users) do
- local userDir = fs.combine(USERS_FOLDER, user)
- if fs.isDir(userDir) then
- table.insert(usernames, user)
- end
- end
- return usernames
- end
- -- Function to save the current user
- local function saveCurrentUser(username)
- if fs.exists(CURRENT_USER_FILE) then
- fs.delete(CURRENT_USER_FILE)
- end
- local file = fs.open(CURRENT_USER_FILE, "w")
- file.write(username)
- file.close()
- end
- -- Function to handle security card login
- local function insertSecurityCard(username)
- local idFolder = fs.combine(USERS_FOLDER .. username, "ID")
- if not fs.exists(idFolder) then
- return false
- end
- while true do
- drawPopupWindow("Doggy OS Security", {"Insert a valid security card to login or reset password.", "Press ENTER to exit."})
- local event, key = os.pullEvent()
- if event == "key" and key == keys.enter then
- return false
- elseif event == "disk" or event == "disk_insert" then
- local peripherals = peripheral.getNames()
- for _, name in ipairs(peripherals) do
- if peripheral.getType(name) == "drive" then
- local diskID = disk.getID(name)
- if diskID then
- local idFile = fs.combine(idFolder, tostring(diskID) .. ".file")
- if fs.exists(idFile) then
- return true
- end
- end
- end
- end
- drawPopupWindow("Security Check", {"Invalid security card."})
- os.sleep(2)
- end
- end
- end
- -- Main function
- local function main()
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- drawPopupWindow("Doggy OS Login", {"Enter username:"})
- local username = read()
- drawPopupWindow("Doggy OS Login", {"Enter your password or type @reset to reset password."})
- local enteredPassword = read("*")
- if enteredPassword == "@reset" then
- resetPassword(username)
- else
- -- Add your login logic here
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement