From cdfba45faaa9202c69bdfe1a2f9e92140e0ecdae Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 30 Apr 2017 17:50:50 -0400 Subject: [PATCH] unite stuffs --- ShiftOS.Unite/Properties/AssemblyInfo.cs | 36 +++++ ShiftOS.Unite/ShiftOS.Unite.csproj | 54 ++++++++ ShiftOS.WinForms/Applications/Dialog.cs | 7 +- ShiftOS.WinForms/Oobe.cs | 119 +++++++++++------ ShiftOS.WinForms/ShiftOS.WinForms.csproj | 9 ++ ShiftOS.WinForms/UniteLoginDialog.Designer.cs | 123 ++++++++++++++++++ ShiftOS.WinForms/UniteLoginDialog.cs | 90 +++++++++++++ ShiftOS.WinForms/UniteLoginDialog.resx | 120 +++++++++++++++++ ShiftOS_TheReturn/SaveSystem.cs | 7 +- ShiftOS_TheReturn/ShiftOS.Engine.csproj | 2 + ShiftOS_TheReturn/UniteClient.cs | 68 ++++++++++ ShiftOS_TheReturn/UniteTestCommands.cs | 64 +++++++++ 12 files changed, 650 insertions(+), 49 deletions(-) create mode 100644 ShiftOS.Unite/Properties/AssemblyInfo.cs create mode 100644 ShiftOS.Unite/ShiftOS.Unite.csproj create mode 100644 ShiftOS.WinForms/UniteLoginDialog.Designer.cs create mode 100644 ShiftOS.WinForms/UniteLoginDialog.cs create mode 100644 ShiftOS.WinForms/UniteLoginDialog.resx create mode 100644 ShiftOS_TheReturn/UniteClient.cs create mode 100644 ShiftOS_TheReturn/UniteTestCommands.cs diff --git a/ShiftOS.Unite/Properties/AssemblyInfo.cs b/ShiftOS.Unite/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..837b88a --- /dev/null +++ b/ShiftOS.Unite/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("ShiftOS.Unite")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ShiftOS.Unite")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("e57ff1e6-3780-4510-b7ef-64731ec81bb8")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ShiftOS.Unite/ShiftOS.Unite.csproj b/ShiftOS.Unite/ShiftOS.Unite.csproj new file mode 100644 index 0000000..ed03ccc --- /dev/null +++ b/ShiftOS.Unite/ShiftOS.Unite.csproj @@ -0,0 +1,54 @@ + + + + + Debug + AnyCPU + {E57FF1E6-3780-4510-B7EF-64731EC81BB8} + Library + Properties + ShiftOS.Unite + ShiftOS.Unite + v4.5.2 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ShiftOS.WinForms/Applications/Dialog.cs b/ShiftOS.WinForms/Applications/Dialog.cs index 6e358e6..2abc705 100644 --- a/ShiftOS.WinForms/Applications/Dialog.cs +++ b/ShiftOS.WinForms/Applications/Dialog.cs @@ -69,7 +69,7 @@ namespace ShiftOS.WinForms.Applications { } - internal void OpenInternal(string title, string msg) + internal void OpenInternal(string title, string msg, Action c) { Title = title; AppearanceManager.SetupDialog(this); @@ -80,7 +80,7 @@ namespace ShiftOS.WinForms.Applications btnok.Click += (o, a) => { AppearanceManager.Close(this); - OpenCallback?.Invoke(); + c?.Invoke(); }; } @@ -89,8 +89,7 @@ namespace ShiftOS.WinForms.Applications public void Open(string title, string msg, Action c = null) { - OpenCallback = c; - new Dialog().OpenInternal(title, msg); + new Dialog().OpenInternal(title, msg, c); } public void PromptTextInternal(string title, string message, Action callback, bool isPassword) diff --git a/ShiftOS.WinForms/Oobe.cs b/ShiftOS.WinForms/Oobe.cs index 309495d..6339588 100644 --- a/ShiftOS.WinForms/Oobe.cs +++ b/ShiftOS.WinForms/Oobe.cs @@ -36,6 +36,7 @@ using Newtonsoft.Json; using ShiftOS.Engine; using ShiftOS.Objects; using ShiftOS.Objects.ShiftFS; +using ShiftOS.Unite; using ShiftOS.WinForms.Tools; namespace ShiftOS.WinForms @@ -219,55 +220,93 @@ You must join the digital society, rise up the ranks, and save us. t.Start(); } + public void PerformUniteLogin() + { + + } + public void PromptForLogin() { - ServerMessageReceived MessageReceived = null; - MessageReceived = (msg) => + Infobox.Show("Login", "Since the last time you've played ShiftOS, some changes have been made to the login system. You must now login using your website credentials.", () => { - if(msg.Name == "mud_savefile") + Infobox.PromptYesNo("Website account", "Do you have an account at http://getshiftos.ml?", (hasAccount) => { - SaveSystem.CurrentSave = JsonConvert.DeserializeObject(msg.Contents); - SaveSystem.SaveGame(); - Application.Restart(); - } - else if(msg.Name == "mud_notfound") - { - ServerManager.MessageReceived -= MessageReceived; - - PromptForLogin(); - } - }; - ServerManager.MessageReceived += MessageReceived; - Infobox.PromptYesNo("Login", "You are missing a digital society authentication link. Would you like to generate a new link with an existing account? Choosing \"No\" will restart the session in the out-of-box experience.", (result)=> - { - if (result == true) - { - Infobox.PromptText("Login", "Please enter your digital society username.", (uname) => + if(hasAccount == true) { - Infobox.PromptText("Login", "Please enter your password.", (pword) => + var loginDialog = new UniteLoginDialog((success)=> { - ServerManager.SendMessage("mud_login", JsonConvert.SerializeObject(new + string token = success; + var uClient = new UniteClient("http://getshiftos.ml", token); + Infobox.Show("Welcome to ShiftOS.", $"Hello, {uClient.GetDisplayName()}! We've signed you into your account. We'll now try to link your ShiftOS account with your save file.", () => { - username = uname, - password = pword - })); - }, true); - }); - } - else - { - //restart in OOBE - if (Objects.ShiftFS.Utils.FileExists(Paths.GetPath("user.dat"))) - { - Utils.Delete(Paths.GetPath("user.dat")); + ServerMessageReceived smr = null; + smr = (msg) => + { + ServerManager.MessageReceived -= smr; + if (msg.Name == "mud_savefile") + { + SaveSystem.CurrentSave = JsonConvert.DeserializeObject(msg.Contents); + SaveSystem.SaveGame(); + } + else + { + LinkSaveFile(token); + } + }; + ServerManager.MessageReceived += smr; + ServerManager.SendMessage("mud_token_login", token); + }); + }); + AppearanceManager.SetupDialog(loginDialog); } - string json = Utils.ExportMount(0); - System.IO.File.WriteAllText(Paths.SaveFile, json); - System.Diagnostics.Process.Start(Application.ExecutablePath); - Environment.Exit(0); - } + }); }); - + } + + public void LinkSaveFile(string token) + { + Infobox.PromptText("Enter username", "Please enter the username you used for your save file before these changes.", (cuname) => + { + Infobox.PromptText("Enter password", "Now, please enter the corresponding password.", (cpass) => + { + ServerMessageReceived nsmr = null; + nsmr = (nmsg) => + { + ServerManager.MessageReceived -= nsmr; + if (nmsg.Name == "mud_savefile") + { + var save = JsonConvert.DeserializeObject(nmsg.Contents); + save.UniteAuthToken = token; + Infobox.Show("That'll do it.", "Your save has been linked up! Next time you log into the ShiftOS site, your Codepoints should show on your profile. There's just a few more things we have to do.", () => + { + SaveSystem.CurrentSave = save; + SaveSystem.SaveGame(); + }); + } + else + { + Infobox.Show("Uh oh.", "We couldn't find a save file with those values. Please try again", () => + { + LinkSaveFile(token); + }); + } + }; + ServerManager.MessageReceived += nsmr; + ServerManager.SendMessage("mud_login", JsonConvert.SerializeObject(new + { + username = cuname, + password = cpass + })); + }, true); + }); + } + + public void ForceReboot() + { + string json = Utils.ExportMount(0); + System.IO.File.WriteAllText(Paths.SaveFile, json); + System.Diagnostics.Process.Start(Application.ExecutablePath); + Environment.Exit(0); } public void StartTrailer() diff --git a/ShiftOS.WinForms/ShiftOS.WinForms.csproj b/ShiftOS.WinForms/ShiftOS.WinForms.csproj index cf104fe..97ba8ef 100644 --- a/ShiftOS.WinForms/ShiftOS.WinForms.csproj +++ b/ShiftOS.WinForms/ShiftOS.WinForms.csproj @@ -367,6 +367,12 @@ + + UserControl + + + UniteLoginDialog.cs + @@ -525,6 +531,9 @@ MainHomepage.cs + + UniteLoginDialog.cs + WindowBorder.cs diff --git a/ShiftOS.WinForms/UniteLoginDialog.Designer.cs b/ShiftOS.WinForms/UniteLoginDialog.Designer.cs new file mode 100644 index 0000000..daf385b --- /dev/null +++ b/ShiftOS.WinForms/UniteLoginDialog.Designer.cs @@ -0,0 +1,123 @@ +namespace ShiftOS.WinForms +{ + partial class UniteLoginDialog + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Component Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.label1 = new System.Windows.Forms.Label(); + this.label2 = new System.Windows.Forms.Label(); + this.label3 = new System.Windows.Forms.Label(); + this.txtusername = new System.Windows.Forms.TextBox(); + this.txtpassword = new System.Windows.Forms.TextBox(); + this.btnlogin = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(16, 15); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(84, 13); + this.label1.TabIndex = 0; + this.label1.Tag = "header2"; + this.label1.Text = "Login to ShiftOS"; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(16, 82); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(58, 13); + this.label2.TabIndex = 1; + this.label2.Text = "Username:"; + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(16, 115); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(56, 13); + this.label3.TabIndex = 2; + this.label3.Text = "Password:"; + // + // txtusername + // + this.txtusername.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.txtusername.Location = new System.Drawing.Point(112, 79); + this.txtusername.Name = "txtusername"; + this.txtusername.Size = new System.Drawing.Size(424, 20); + this.txtusername.TabIndex = 3; + // + // txtpassword + // + this.txtpassword.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.txtpassword.Location = new System.Drawing.Point(112, 112); + this.txtpassword.Name = "txtpassword"; + this.txtpassword.Size = new System.Drawing.Size(424, 20); + this.txtpassword.TabIndex = 4; + this.txtpassword.UseSystemPasswordChar = true; + // + // btnlogin + // + this.btnlogin.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.btnlogin.Location = new System.Drawing.Point(460, 148); + this.btnlogin.Name = "btnlogin"; + this.btnlogin.Size = new System.Drawing.Size(75, 23); + this.btnlogin.TabIndex = 5; + this.btnlogin.Text = "Login"; + this.btnlogin.UseVisualStyleBackColor = true; + this.btnlogin.Click += new System.EventHandler(this.btnlogin_Click); + // + // UniteLoginDialog + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.btnlogin); + this.Controls.Add(this.txtpassword); + this.Controls.Add(this.txtusername); + this.Controls.Add(this.label3); + this.Controls.Add(this.label2); + this.Controls.Add(this.label1); + this.Name = "UniteLoginDialog"; + this.Size = new System.Drawing.Size(573, 192); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Label label1; + private System.Windows.Forms.Label label2; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.TextBox txtusername; + private System.Windows.Forms.TextBox txtpassword; + private System.Windows.Forms.Button btnlogin; + } +} diff --git a/ShiftOS.WinForms/UniteLoginDialog.cs b/ShiftOS.WinForms/UniteLoginDialog.cs new file mode 100644 index 0000000..4c85005 --- /dev/null +++ b/ShiftOS.WinForms/UniteLoginDialog.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Drawing; +using System.Data; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using ShiftOS.Engine; +using System.Net; + +namespace ShiftOS.WinForms +{ + public partial class UniteLoginDialog : UserControl, IShiftOSWindow + { + public UniteLoginDialog(Action callback) + { + InitializeComponent(); + Callback = callback; + } + + private Action Callback { get; set; } + + public void OnLoad() + { + this.ParentForm.AcceptButton = btnlogin; + } + + public void OnSkinLoad() + { + } + + public bool OnUnload() + { + return true; + } + + public void OnUpgrade() + { + } + + private void btnlogin_Click(object sender, EventArgs e) + { + string u = txtusername.Text; + string p = txtpassword.Text; + + if (string.IsNullOrWhiteSpace(u)) + { + Infobox.Show("Please enter a username.", "You must enter a proper email address."); + return; + } + + if (string.IsNullOrWhiteSpace(p)) + { + Infobox.Show("Please enter a password.", "You must enter a valid password."); + return; + } + + try + { + var webrequest = HttpWebRequest.Create("http://getshiftos.ml/Auth/Login?appname=ShiftOS&appdesc=ShiftOS+client&version=1_0_beta_2_4"); + string base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{u}:{p}")); + webrequest.Headers.Add("Authentication: Basic " + base64); + var response = webrequest.GetResponse(); + var str = response.GetResponseStream(); + var reader = new System.IO.StreamReader(str); + string result = reader.ReadToEnd(); + reader.Close(); + str.Close(); + str.Dispose(); + response.Dispose(); + Callback?.Invoke(result); + AppearanceManager.Close(this); + } +#if DEBUG + catch(Exception ex) + { + Infobox.Show("Error", ex.ToString()); + } +#else + catch + { + Infobox.Show("Login failed.", "The login attempt failed due to an incorrect username and password pair."); + } +#endif + + } + } +} diff --git a/ShiftOS.WinForms/UniteLoginDialog.resx b/ShiftOS.WinForms/UniteLoginDialog.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/ShiftOS.WinForms/UniteLoginDialog.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ShiftOS_TheReturn/SaveSystem.cs b/ShiftOS_TheReturn/SaveSystem.cs index 3e68a70..4f4e9f5 100644 --- a/ShiftOS_TheReturn/SaveSystem.cs +++ b/ShiftOS_TheReturn/SaveSystem.cs @@ -266,12 +266,9 @@ namespace ShiftOS.Engine { if (Utils.FileExists(Paths.GetPath("user.dat"))) { - var userdat = JsonConvert.DeserializeObject(Utils.ReadAllText(Paths.GetPath("user.dat"))); + string token = Utils.ReadAllText(Paths.GetPath("user.dat")); - ServerManager.SendMessage("mud_login", $@"{{ - username: ""{userdat.Username}"", - password: ""{userdat.Password}"" -}}"); + ServerManager.SendMessage("mud_token_login", token); } else { diff --git a/ShiftOS_TheReturn/ShiftOS.Engine.csproj b/ShiftOS_TheReturn/ShiftOS.Engine.csproj index f7b730f..5cd6c68 100644 --- a/ShiftOS_TheReturn/ShiftOS.Engine.csproj +++ b/ShiftOS_TheReturn/ShiftOS.Engine.csproj @@ -131,6 +131,8 @@ + + diff --git a/ShiftOS_TheReturn/UniteClient.cs b/ShiftOS_TheReturn/UniteClient.cs new file mode 100644 index 0000000..8d9eef2 --- /dev/null +++ b/ShiftOS_TheReturn/UniteClient.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace ShiftOS.Unite +{ + public class UniteClient + { + public string Token { get; private set; } + public string BaseURL { get; private set; } + + public UniteClient(string baseurl, string usertoken) + { + BaseURL = baseurl; + Token = Token; + } + + internal string MakeCall(string url) + { + var webrequest = WebRequest.Create(BaseURL + url); + webrequest.Headers.Add("Authentication: Token " + Token); + using (var response = webrequest.GetResponse()) + { + using (var stream = response.GetResponseStream()) + { + using (var reader = new System.IO.StreamReader(stream)) + { + return reader.ReadToEnd(); + } + } + } + } + + public string GetDisplayName() + { + return MakeCall("/API/GetDisplayName"); + } + + public void SetDisplayName(string value) + { + MakeCall("/API/SetDisplayName/" + value.ToString()); + } + + public string GetFullName() + { + return MakeCall("/API/GetFullName"); + } + + public void SetFullName(string value) + { + MakeCall("/API/SetFullName/" + value.ToString()); + } + + + public long GetCodepoints() + { + return Convert.ToInt64(MakeCall("/API/GetCodepoints")); + } + + public void SetCodepoints(long value) + { + MakeCall("/API/SetCodepoints/" + value.ToString()); + } + } +} diff --git a/ShiftOS_TheReturn/UniteTestCommands.cs b/ShiftOS_TheReturn/UniteTestCommands.cs new file mode 100644 index 0000000..37ab1ee --- /dev/null +++ b/ShiftOS_TheReturn/UniteTestCommands.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; + +namespace ShiftOS.Engine +{ + [Namespace("unite")] + public static class UniteTestCommands + { + [Command("login")] + [RequiresArgument("username")] + [RequiresArgument("password")] + public static bool LoginTest(Dictionary args) + { + string u = args["username"].ToString(); + string p = args["password"].ToString(); + var webrequest = HttpWebRequest.Create("http://getshiftos.ml/Auth/Login?appname=ShiftOS&appdesc=ShiftOS+client&version=1_0_beta_2_4"); + string base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{u}:{p}")); + webrequest.Headers.Add("Authentication: Basic " + base64); + var response = webrequest.GetResponse(); + var str = response.GetResponseStream(); + var reader = new System.IO.StreamReader(str); + string result = reader.ReadToEnd(); + Console.WriteLine("Server returned: " + result); + reader.Close(); + str.Close(); + str.Dispose(); + response.Dispose(); + + return true; + } + + [Command("linklogin")] + [RequiresArgument("username")] + [RequiresArgument("password")] + public static bool LinkLogin(Dictionary args) + { + string u = args["username"].ToString(); + string p = args["password"].ToString(); + var webrequest = HttpWebRequest.Create("http://getshiftos.ml/Auth/Login?appname=ShiftOS&appdesc=ShiftOS+client&version=1_0_beta_2_4"); + string base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{u}:{p}")); + webrequest.Headers.Add("Authentication: Basic " + base64); + var response = webrequest.GetResponse(); + var str = response.GetResponseStream(); + var reader = new System.IO.StreamReader(str); + string result = reader.ReadToEnd(); + //If we get this far, the token is OURS. :D + SaveSystem.CurrentSave.UniteAuthToken = result; + Console.WriteLine("Unite says \"Access Granted!\""); + SaveSystem.SaveGame(); + reader.Close(); + str.Close(); + str.Dispose(); + response.Dispose(); + + return true; + } + + + } +}