From 22e5f14a49fb6b91d8f4ddaa49a96bc25c0aa55e Mon Sep 17 00:00:00 2001 From: Oliver Cooper Date: Mon, 1 Dec 2014 12:53:44 +1030 Subject: [PATCH] Added optional password login --- Programs/Quest.program/startup | 2 + System/.hash | 2 +- System/API/Bedrock.lua | 31 ++++++- System/API/Environment.lua | 1 - .../Quest.program/hash => System/API/Hash.lua | 2 - System/API/Settings.lua | 9 ++ System/Objects/CentrePoint.lua | 3 +- System/Objects/LoginView.lua | 88 +++++++++++++++++++ .../First Setup.program/Views/page5.view | 40 +++++++-- .../First Setup.program/Views/page6.view | 38 ++++++++ System/Programs/First Setup.program/startup | 22 ++++- System/Programs/Settings.program/startup | 56 ++++++++++++ System/Views/main.view | 10 +++ System/Views/password.view | 37 ++++++++ System/main.lua | 20 +++-- startup | 9 +- 16 files changed, 345 insertions(+), 25 deletions(-) rename Programs/Quest.program/hash => System/API/Hash.lua (97%) create mode 100644 System/Objects/LoginView.lua create mode 100644 System/Programs/First Setup.program/Views/page6.view create mode 100644 System/Views/password.view diff --git a/Programs/Quest.program/startup b/Programs/Quest.program/startup index 7450c9a..8f13fad 100644 --- a/Programs/Quest.program/startup +++ b/Programs/Quest.program/startup @@ -86,6 +86,8 @@ os.loadAPI('hash') os.loadAPI('lQuery') if OneOS then + OneOS.LoadAPI('/System/API/Hash.lua') + hash = Hash OneOS.LoadAPI('/System/API/Wireless.lua') OneOS.LoadAPI('/System/API/Peripheral.lua') else diff --git a/System/.hash b/System/.hash index 01bf26c..57d7368 100755 --- a/System/.hash +++ b/System/.hash @@ -1 +1 @@ -4.99783031E8 \ No newline at end of file +1.86449353E9 \ No newline at end of file diff --git a/System/API/Bedrock.lua b/System/API/Bedrock.lua index 038c708..6d7cd6c 100755 --- a/System/API/Bedrock.lua +++ b/System/API/Bedrock.lua @@ -1,4 +1,4 @@ ---Bedrock Build: 270 +--Bedrock Build: 271 --This code is squished down in to one, rather hard to read file. --As such it is not much good for anything other than being loaded as an API. --If you want to look at the code to learn from it, copy parts or just take a look, @@ -80,6 +80,35 @@ end colours.transparent = 0 colors.transparent = 0 +Filters = { + Greyscale = { + [colours.white] = colours.white, + [colours.orange] = colours.lightGrey, + [colours.magenta] = colours.lightGrey, + [colours.lightBlue] = colours.lightGrey, + [colours.yellow] = colours.lightGrey, + [colours.lime] = colours.lightGrey, + [colours.pink] = colours.lightGrey, + [colours.grey] = colours.grey, + [colours.lightGrey] = colours.lightGrey, + [colours.cyan] = colours.grey, + [colours.purple] = colours.grey, + [colours.blue] = colours.grey, + [colours.brown] = colours.grey, + [colours.green] = colours.grey, + [colours.red] = colours.grey, + [colours.transparent] = colours.transparent, + } +} + +function FilterColour(colour, filter) + if filter[colour] then + return filter[colour] + else + return colours.black + end +end + DrawCharacters = function (x, y, characters, textColour, bgColour) Drawing.WriteStringToBuffer(x, y, tostring(characters), textColour, bgColour) end diff --git a/System/API/Environment.lua b/System/API/Environment.lua index 0e36e35..5a23e81 100755 --- a/System/API/Environment.lua +++ b/System/API/Environment.lua @@ -36,7 +36,6 @@ GetCleanEnvironment = function(self) for k, v in pairs(cleanEnvironment) do cleanEnv[k] = v end - cleanEnv._G = cleanEnv return cleanEnv end diff --git a/Programs/Quest.program/hash b/System/API/Hash.lua similarity index 97% rename from Programs/Quest.program/hash rename to System/API/Hash.lua index ae459ca..d55d64c 100644 --- a/Programs/Quest.program/hash +++ b/System/API/Hash.lua @@ -2,8 +2,6 @@ -- Thanks to GravityScore for this! -- http://www.computercraft.info/forums2/index.php?/topic/8169-sha-256-in-pure-lua/ -- --- This is used to hash passwords sent with the secure text field. It just reduces the chance of people getting hacked. --- -- -- Adaptation of the Secure Hashing Algorithm (SHA-244/256) diff --git a/System/API/Settings.lua b/System/API/Settings.lua index 533e5b1..b13f09a 100755 --- a/System/API/Settings.lua +++ b/System/API/Settings.lua @@ -34,6 +34,11 @@ Defaults = { Type = 'Side', Label = 'Monitor Side', Default = nil + }, + Password = { + Type = 'Password', + Label = 'Password', + Default = nil } } --[[ @@ -78,6 +83,10 @@ function GetValues(self) return values end +function CheckPassword(self, password) + return Hash.sha256(password) == self:GetValues()['Password'] +end + DesktopColourChange = false function SetDesktopColourChange(func) DesktopColourChange = func diff --git a/System/Objects/CentrePoint.lua b/System/Objects/CentrePoint.lua index 79ebed2..949ff9a 100644 --- a/System/Objects/CentrePoint.lua +++ b/System/Objects/CentrePoint.lua @@ -160,7 +160,8 @@ UpdatePrograms = function(self) Type = 'ProgramPreview', Program = program, OnClick = function(prv, event, side, x, y) - if not prv.Program.Hidden and ((x == 1 and y == 1) or side == 3) and prv.Program:Close() then + if not prv.Program.Hidden and ((x == 1 and y == 1) or side == 3) then + prv.Program:Close() prv.Bedrock:GetObject('CentrePoint'):UpdatePrograms() else oldProgram = prv.Program diff --git a/System/Objects/LoginView.lua b/System/Objects/LoginView.lua new file mode 100644 index 0000000..0809f93 --- /dev/null +++ b/System/Objects/LoginView.lua @@ -0,0 +1,88 @@ +Inherit = 'View' +IsSleepMode = false + +OnDraw = function(self, x, y) + for _y, row in ipairs(Drawing.Buffer) do + for _x, pixel in ipairs(row) do + Drawing.WriteToBuffer(_x, _y, pixel[1], Drawing.FilterColour(pixel[2], Drawing.Filters.Greyscale), Drawing.FilterColour(pixel[3], Drawing.Filters.Greyscale)) + end + end +end + +TryUnlock = function(self, password) + local secureTextBox = self:GetObject('SecureTextBox') + secureTextBox.Text = '' + if password ~= '' and Settings:CheckPassword(password) then + Log.i('Password correct, unlocking.') + self.Visible = false + self.Bedrock:SetActiveObject() + self:OnUnlock(self.IsSleepMode) + else + Log.i('Password incorrect.') + local label = self:GetObject('Label') + local secureStartX = secureTextBox.X + local labelStartX = label.X + local maxDelta = 4 + local steps = { + -2, + -4, + -2, + 0, + 2, + 4, + 2, + 0, + -1, + -2, + -1, + 0, + 1, + 2, + 1, + 0 + } + if Settings:GetValues()['UseAnimations'] then + self.Bedrock:SetActiveObject() + local i = 1 + self.Bedrock:StartRepeatingTimer(function(newTimer) + secureTextBox.X = secureStartX + steps[i] + label.X = labelStartX + steps[i] + i = i + 1 + if i > #steps then + self.Bedrock:StopTimer(newTimer) + self.Bedrock:SetActiveObject(secureTextBox) + end + end, 0.05) + end + end +end + +Lock = function(self) + if Settings:GetValues()['Password'] == nil then + Log.i('No password, unlocking.') + self.Visible = false + if self.OnUnlock then + self:OnUnlock(self.IsSleepMode) + end + return + end + self.Visible = true + + local secureTextBox = self:GetObject('SecureTextBox') + secureTextBox.OnChange = function(_self, event, keychar) + if keychar == keys.enter then + self:TryUnlock(secureTextBox.Text) + end + end + self.Bedrock:SetActiveObject(secureTextBox) + + self:GetObject('ExitButton').OnClick = function(_self, event, side, x, y) + if self.IsSleepMode then + else + Shutdown(true) + end + end +end + +OnClick = function(self, event, side, x, y) +end \ No newline at end of file diff --git a/System/Programs/First Setup.program/Views/page5.view b/System/Programs/First Setup.program/Views/page5.view index bb30a33..b00b6a3 100644 --- a/System/Programs/First Setup.program/Views/page5.view +++ b/System/Programs/First Setup.program/Views/page5.view @@ -13,17 +13,36 @@ ["Width"]="100%,-6", ["Name"]="Label", ["Type"]="Label", - ["Text"]="All done! OneOS is now all setup and ready for you to use. Click 'Restart' to get going!\n\nIf the restart button does not work then hold Ctrl + R or break and replace your computer." + ["Text"]="If you want a password enter it, otherwise leave the text box blank." }, [3]={ + ["Y"]="9", + ["X"]="25%", + ["Width"]="50%", + ["Name"]="PasswordTextBox", + ["Type"]="SecureTextBox", + ["Placeholder"]="Password", + ["BackgroundColour"]=256, + ["Active"]=true + }, + [4]={ + ["Y"]="11", + ["X"]="25%", + ["Width"]="50%", + ["Name"]="ConfirmPasswordTextBox", + ["Type"]="SecureTextBox", + ["Placeholder"]="Confirm Password", + ["BackgroundColour"]=256, + }, + [5]={ ["Y"]="100%,-1", - ["X"]="100%,-9", - ["Name"]="RestartButton", + ["X"]="100%,-7", + ["Name"]="PasswordNextButton", ["Type"]="Button", - ["Text"]="Restart", + ["Text"]="Next", ["BackgroundColour"]=256 }, - [4]={ + [6]={ ["Y"]="100%,-1", ["X"]="3", ["Name"]="BackButton", @@ -31,6 +50,17 @@ ["Text"]="Back", ["BackgroundColour"]=256 }, + [7]={ + ["Y"]=13, + ["X"]=3, + ["Width"]="100%,-6", + ["Name"]="NoMatchLabel", + ["Type"]="Label", + ["TextColour"]=16384, + ["Align"]="Center", + ["Text"]="Passwords do not match!", + ["Visible"]=false + }, }, ["BackgroundColour"]=1, ["ToolBarColour"] = 128, diff --git a/System/Programs/First Setup.program/Views/page6.view b/System/Programs/First Setup.program/Views/page6.view new file mode 100644 index 0000000..bb30a33 --- /dev/null +++ b/System/Programs/First Setup.program/Views/page6.view @@ -0,0 +1,38 @@ +{ + ["Children"]={ + [1]={ + ["Y"]=1, + ["X"]=1, + ["Name"]="Toolbar", + ["Type"]="View", + ["InheritView"]="toolbar" + }, + [2]={ + ["Y"]=5, + ["X"]=3, + ["Width"]="100%,-6", + ["Name"]="Label", + ["Type"]="Label", + ["Text"]="All done! OneOS is now all setup and ready for you to use. Click 'Restart' to get going!\n\nIf the restart button does not work then hold Ctrl + R or break and replace your computer." + }, + [3]={ + ["Y"]="100%,-1", + ["X"]="100%,-9", + ["Name"]="RestartButton", + ["Type"]="Button", + ["Text"]="Restart", + ["BackgroundColour"]=256 + }, + [4]={ + ["Y"]="100%,-1", + ["X"]="3", + ["Name"]="BackButton", + ["Type"]="Button", + ["Text"]="Back", + ["BackgroundColour"]=256 + }, + }, + ["BackgroundColour"]=1, + ["ToolBarColour"] = 128, + ["ToolBarTextColour"] = 1 +} \ No newline at end of file diff --git a/System/Programs/First Setup.program/startup b/System/Programs/First Setup.program/startup index ce17bda..6846185 100644 --- a/System/Programs/First Setup.program/startup +++ b/System/Programs/First Setup.program/startup @@ -1,4 +1,5 @@ OneOS.LoadAPI('/System/API/Bedrock.lua', false) +OneOS.LoadAPI('/System/API/Hash.lua') local program = Bedrock:Initialise() @@ -6,7 +7,8 @@ Current = { Page = 1, ComputerName = nil, DesktopColour = nil, - AnimationsEnabled = nil + AnimationsEnabled = nil, + Password = nil } function LoadCurrentView() @@ -40,6 +42,21 @@ program:ObjectClick('NextButton', function(self, event, side, x, y) LoadCurrentView() end) +program:ObjectClick('PasswordNextButton', function(self, event, side, x, y) + if program:GetObject('PasswordTextBox').Text == '' then + Current.Password = nil + program:GetObject('NoMatchLabel').Visible = false + elseif program:GetObject('PasswordTextBox').Text == program:GetObject('ConfirmPasswordTextBox').Text then + Current.Password = Hash.sha256(program:GetObject('PasswordTextBox').Text) + program:GetObject('NoMatchLabel').Visible = false + else + program:GetObject('NoMatchLabel').Visible = true + return + end + Current.Page = Current.Page + 1 + LoadCurrentView() +end) + program:ObjectClick('BackButton', function(self, event, side, x, y) Current.Page = Current.Page - 1 LoadCurrentView() @@ -67,7 +84,8 @@ program:ObjectClick('RestartButton', function(self, event, side, x, y) local settings = { ComputerName = Current.ComputerName, DesktopColour = Current.DesktopColour, - UseAnimations = Current.AnimationsEnabled + UseAnimations = Current.AnimationsEnabled, + Password = Current.Password } os.setComputerLabel(settings.ComputerName) h.write(textutils.serialize(settings)) diff --git a/System/Programs/Settings.program/startup b/System/Programs/Settings.program/startup index 01e1848..271106a 100755 --- a/System/Programs/Settings.program/startup +++ b/System/Programs/Settings.program/startup @@ -1,9 +1,21 @@ OneOS.LoadAPI('/System/API/Bedrock.lua', false) +OneOS.LoadAPI('/System/API/Hash.lua') local program = Bedrock:Initialise() --TODO: monitor side setting +function changePassword(callback) + program:DisplayTextBoxWindow('Enter Password', 'Please enter your new password.', function(success, password) + if success then + OneOS.Settings:SetValue('Password', Hash.sha256(password)) + end + if callback then + callback(success) + end + end) +end + program:Run(function() program:LoadView('main') local startX = 2 @@ -43,6 +55,50 @@ program:Run(function() OneOS.Settings:SetValue(k, self.Toggle) end }) + elseif v.Type == 'Password' then + settings[k].Controls = { + + program:AddObject({ + Type = 'Button', + X = controlX, + Y = startY, + Width = ((value ~= nil) and 5 or 4), + BackgroundColour = colours.red, + ActiveBackgroundColour = colours.green, + TextColour = colours.white, + Text = ((value ~= nil) and 'Yes' or 'No'), + Toggle = (value ~= nil), + OnClick = function(self) + if self.Toggle then + self.Text = 'Yes' + self.Width = 5 + changePassword(function(success) + if not success then + self.Toggle = false + self:OnClick() + end + end) + else + self.Text = 'No' + self.Width = 4 + OneOS.Settings:SetValue('Password', nil) + end + settings[k].Controls[2].Visible = self.Toggle + end + }), + + program:AddObject({ + Type = 'Button', + X = controlX + 6, + Y = startY, + Text = 'Change', + Visible = (value ~= nil), + OnClick = function(self) + changePassword() + end + }), + + } elseif v.Type == 'Colour' then local x = controlX _colours = { diff --git a/System/Views/main.view b/System/Views/main.view index de97764..83a7e47 100755 --- a/System/Views/main.view +++ b/System/Views/main.view @@ -37,5 +37,15 @@ ["Type"]="Overlay", ["InheritView"]="overlay", }, + [6]={ + ["Name"]="LoginView", + ["Type"]="LoginView", + ["InheritView"]="password", + ["X"]=1, + ["Y"]=1, + ["Width"]="100%", + ["Height"]="100%", + ["Visible"]=false, + }, } } \ No newline at end of file diff --git a/System/Views/password.view b/System/Views/password.view new file mode 100644 index 0000000..dac75cd --- /dev/null +++ b/System/Views/password.view @@ -0,0 +1,37 @@ +{ + ["Children"]={ + [1]={ + ["Type"]="View", + ["X"]=1, + ["Y"]="50%,-4", + ["Width"]="100%", + ["Height"]=8, + ["BackgroundColour"]=8, + }, + [2]={ + ["Type"]="SecureTextBox", + ["X"]="50%,-9", + ["Y"]="50%", + ["Placeholder"]="Password...", + ["Width"]=18, + ["BackgroundColour"]=1 + }, + [3]={ + ["Type"]="Label", + ["X"]="50%,-6", + ["Y"]="50%, -2", + ["Text"]="Please Login", + ["TextColour"]=1 + }, + [4]={ + ["Name"]="ExitButton", + ["Type"]="Button", + ["X"]="100%", + ["Y"]="50%,-4", + ["Width"]=1, + ["Text"]="x", + ["TextColour"]=2048, + ["BackgroundColour"]=0, + }, + } +} \ No newline at end of file diff --git a/System/main.lua b/System/main.lua index 2b63abe..92353b2 100755 --- a/System/main.lua +++ b/System/main.lua @@ -310,7 +310,7 @@ function AutoUpdateResponse(self, event, url, data) else Log.i('OneOS is neither up to date or behind. (.version probably edited)') end - elseif Current.Program then + else Current.Program:QueueEvent(event, url, data) end end @@ -336,6 +336,7 @@ function Initialise() Log.i('GUI Loaded') Current.ProgramView = bedrock:GetObject('ProgramView') + Current.LoginView = bedrock:GetObject('LoginView') Current.Overlay = bedrock:GetObject('Overlay') Indexer.RefreshIndex() @@ -347,12 +348,17 @@ function Initialise() Current.Desktop = Helpers.OpenFile('System/Programs/Desktop.program', {isHidden = true}) - if Settings:GetValues()['StartupProgram'] then - Helpers.OpenFile('Programs/'..Settings:GetValues()['StartupProgram']) - UpdateOverlay() + Current.LoginView.OnUnlock = function(self, sleepMode) + if not sleepMode then + if Settings:GetValues()['StartupProgram'] then + Helpers.OpenFile('Programs/'..Settings:GetValues()['StartupProgram']) + UpdateOverlay() + end + UpdateOverlay() + StartDoorWireless() + CheckAutoUpdate() + end end - UpdateOverlay() - StartDoorWireless() - CheckAutoUpdate() + Current.LoginView:Lock() end) end \ No newline at end of file diff --git a/startup b/startup index 26304a2..7fc306e 100755 --- a/startup +++ b/startup @@ -1,10 +1,10 @@ -isDebug = false - cleanEnvironment = {} for k, v in pairs(_G) do cleanEnvironment[k] = v end +isDebug = false + oldTerm = term --[[ @@ -294,8 +294,9 @@ function PastebinPut(file) end function BootMenu() - local Drawing = LegacyDrawing Log.i('Entered Boot Menu') + os.loadAPI('/System/API/LegacyDrawing.lua') + local Drawing = LegacyDrawing Drawing.Clear(colours.white) Drawing.DrawCharactersCenter(nil, (-Drawing.Screen.Height/2) + 2, nil, nil, 'OneOS', colours.blue, colours.white) Drawing.DrawCharactersCenter(nil, (-Drawing.Screen.Height/2) + 3, nil, nil, 'Boot Options', colours.grey, colours.white) @@ -628,8 +629,6 @@ elseif Start() then print(v) end - local Drawing = LegacyDrawing - PrintCentered('Checking for file modifications...', Drawing.Screen.Height-3) local modified = IsSystemModified() term.clearLine()