Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to save the username and encrypted password to a file
- function saveCredentialsToFile(username, encryptedText, dir)
- local fileName = dir .. "/" .. username .. ".txt"
- local file = fs.open(fileName, "w")
- file.write(encryptedText)
- file.close()
- return fileName
- end
- -- Modify the create function to save credentials in bootauth
- function create(username, text)
- local userDir = "/disk/users/" .. username
- local bootAuthDir = "/disk/bootauth"
- if not fs.exists(bootAuthDir) then
- fs.makeDir(bootAuthDir)
- end
- if not fs.exists(userDir) then
- fs.makeDir(userDir)
- end
- local key = generateKey()
- local encryptedText = encrypt(text, key)
- local keyFileName = saveKeyToFile(key, userDir)
- -- Save username and encrypted password in the bootauth directory
- local credentialsFileName = saveCredentialsToFile(username, encryptedText, bootAuthDir)
- return { status = "success", encryptedText = encryptedText, keyFileName = keyFileName, credentialsFileName = credentialsFileName }
- end
- -- Function to load the encrypted text from the bootauth directory
- function loadEncryptedTextFromBootAuth(username)
- local fileName = "/disk/bootauth/" .. username .. ".txt"
- if not fs.exists(fileName) then
- print("Credentials file does not exist.")
- return nil
- end
- local file = fs.open(fileName, "r")
- local encryptedText = file.readAll()
- file.close()
- return encryptedText
- end
- -- Example Usage (remains the same)
- local tArgs = {...}
- if #tArgs < 1 then
- print("Usage: api <command> <args>")
- return
- end
- local command = tArgs[1]
- if command == "create" then
- if #tArgs < 3 then
- print("Usage: api create <username> <text>")
- return
- end
- local username = tArgs[2]
- local text = table.concat(tArgs, " ", 3)
- local result = create(username, text)
- print(textutils.serialize(result))
- elseif command == "delete" then
- if #tArgs < 2 then
- print("Usage: api delete <username>")
- return
- end
- local username = tArgs[2]
- local result = delete(username)
- print(textutils.serialize(result))
- elseif command == "authenticate" then
- if #tArgs < 3 then
- print("Usage: api authenticate <adminUsername> <adminPassword>")
- return
- end
- local adminUsername = tArgs[2]
- local adminPassword = tArgs[3]
- local result = authenticate(adminUsername, adminPassword)
- print(textutils.serialize(result))
- else
- print("Invalid command. Use 'create', 'delete', or 'authenticate'.")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement