diff --git a/ShiftOS.Server.WebAdmin/App.config b/ShiftOS.Server.WebAdmin/App.config new file mode 100644 index 0000000..88fa402 --- /dev/null +++ b/ShiftOS.Server.WebAdmin/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ShiftOS.Server.WebAdmin/Program.cs b/ShiftOS.Server.WebAdmin/Program.cs new file mode 100644 index 0000000..970f236 --- /dev/null +++ b/ShiftOS.Server.WebAdmin/Program.cs @@ -0,0 +1,188 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Nancy; +using Nancy.Authentication.Forms; +using Nancy.Bootstrapper; +using Nancy.Hosting.Self; +using Nancy.Security; +using Nancy.TinyIoc; +using Newtonsoft.Json; + +namespace ShiftOS.Server.WebAdmin +{ + class Program + { + static void Main(string[] args) + { + var HostConf = new HostConfiguration(); + HostConf.UrlReservations.CreateAutomatically = true; + + using(var nancy = new NancyHost(HostConf, new Uri("http://localhost:13371/mudadmin"))) + { + nancy.Start(); + Console.WriteLine($"[{DateTime.Now}] Initiating on localhost:13371..."); + Console.ReadLine(); + } + } + } + + public static class PageBuilder + { + public static string Build(string page) + { + string templatehtml = Properties.Resources.HtmlTemplate; + switch (page) + { + case "login": + templatehtml = templatehtml.Replace("{body}", Properties.Resources.LoginView); + break; + } + return templatehtml; + } + } + + public class MudUserIdentity : IUserIdentity + { + public MudUserIdentity(string username) + { + _username = username; + } + + public IEnumerable Claims + { + get + { + return SystemManager.GetClaims(_username); + } + } + + private string _username = ""; + + public string UserName + { + get + { + return _username; + } + } + } + + public static class SystemManager + { + public static List GetClaims(string username) + { + foreach (var user in JsonConvert.DeserializeObject>(ShiftOS.Server.Program.ReadEncFile("users.json"))) + { + if(user.Username == username) + { + return user.Claims; + } + } + return new List(new[] { "User" }); + } + + public static bool Login(string username, string password, out Guid id) + { + foreach (var user in JsonConvert.DeserializeObject>(ShiftOS.Server.Program.ReadEncFile("users.json"))) + { + if (user.Username == username && user.Password == Encryption.Encrypt(password)) + { + id = user.ID; + return true; + } + } + id = new Guid(); + return false; + } + + public static MudUserIdentity GetIdentity(Guid id) + { + foreach (var user in JsonConvert.DeserializeObject>(ShiftOS.Server.Program.ReadEncFile("users.json"))) + { + if (user.ID == id) + { + return new WebAdmin.MudUserIdentity(user.Username); + } + } + return null; + } + } + + public class MudUser + { + public string Username { get; set; } + public string Password { get; set; } + public List Claims { get; set; } + public Guid ID { get; set; } + } + + public class MudBootstrapper : DefaultNancyBootstrapper + { + protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines) + { + var formsAuthConfiguration = new FormsAuthenticationConfiguration(); + formsAuthConfiguration.RedirectUrl = "~/login"; + formsAuthConfiguration.UserMapper = container.Resolve(); + FormsAuthentication.Enable(pipelines, formsAuthConfiguration); + base.ApplicationStartup(container, pipelines); + } + } + + + public class MudUserMapper : IUserMapper + { + public IUserIdentity GetUserFromIdentifier(Guid identifier, NancyContext context) + { + return SystemManager.GetIdentity(identifier); + } + } + + public class LoginModule : NancyModule + { + public LoginModule() + { + Get["/login"] = parameters => + { + return PageBuilder.Build("login"); + }; + + Get["/logout"] = parameters => + { + return this.Logout("/"); + }; + + Post["/login"] = parameters => + { + Guid id = new Guid(); + if (SystemManager.Login(parameters.username, parameters.password, out id) == true) + { + return this.Login(id); + } + else + { + return PageBuilder.Build("loginFailed"); + } + }; + } + } + + public class UserModule : NancyModule + { + public UserModule() + { + this.RequiresAuthentication(); + this.RequiresClaims("User"); + Get["/"] = _ => + { + return PageBuilder.Build("status"); + }; + Get["/status"] = _ => + { + return PageBuilder.Build("status"); + }; + } + } +} diff --git a/ShiftOS.Server.WebAdmin/Properties/AssemblyInfo.cs b/ShiftOS.Server.WebAdmin/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..431a97c --- /dev/null +++ b/ShiftOS.Server.WebAdmin/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.Server.WebAdmin")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ShiftOS.Server.WebAdmin")] +[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("b29fdd06-e6fe-40a2-8258-283728ced81a")] + +// 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.Server.WebAdmin/Properties/Resources.Designer.cs b/ShiftOS.Server.WebAdmin/Properties/Resources.Designer.cs new file mode 100644 index 0000000..00f939c --- /dev/null +++ b/ShiftOS.Server.WebAdmin/Properties/Resources.Designer.cs @@ -0,0 +1,102 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace ShiftOS.Server.WebAdmin.Properties { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ShiftOS.Server.WebAdmin.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to <html> + /// <head> + /// <title>Multi-user domain &bull; ShiftOS</title> + /// <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> + /// + /// <link rel="stylesheet" href="css/theme.css"/> + /// + /// <!-- Latest compiled and minified JavaScript --> + /// <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkf [rest of string was truncated]";. + /// + internal static string HtmlTemplate { + get { + return ResourceManager.GetString("HtmlTemplate", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to <h3>Access denied.</h3> + /// + ///<p>You require a higher authentication level to access this part of the multi-user domain. Please enter the username and password of whom has access to this sector.</p> + /// + ///<form method="post" action=""> + /// <table class="table"> + /// <tr> + /// <td><strong>Username:</strong></td> + /// <td><input class="form-control" type="text" name="username"/></td> + /// </tr> + /// <tr> + /// <td><strong>Password:</strong></td> + /// <td><input class="form-control" type="password" name="password"/></td> + /// </tr [rest of string was truncated]";. + /// + internal static string LoginView { + get { + return ResourceManager.GetString("LoginView", resourceCulture); + } + } + } +} diff --git a/ShiftOS.Server.WebAdmin/Properties/Resources.resx b/ShiftOS.Server.WebAdmin/Properties/Resources.resx new file mode 100644 index 0000000..5a705f4 --- /dev/null +++ b/ShiftOS.Server.WebAdmin/Properties/Resources.resx @@ -0,0 +1,127 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 + + + + ..\Resources\HtmlTemplate.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252 + + + ..\Resources\LoginView.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252 + + \ No newline at end of file diff --git a/ShiftOS.Server.WebAdmin/Resources/HtmlTemplate.txt b/ShiftOS.Server.WebAdmin/Resources/HtmlTemplate.txt new file mode 100644 index 0000000..7f10c59 --- /dev/null +++ b/ShiftOS.Server.WebAdmin/Resources/HtmlTemplate.txt @@ -0,0 +1,52 @@ + + + Multi-user domain • ShiftOS + + + + + + + + + + + + + +
+
+ {body} +
+
+ + \ No newline at end of file diff --git a/ShiftOS.Server.WebAdmin/Resources/LoginView.txt b/ShiftOS.Server.WebAdmin/Resources/LoginView.txt new file mode 100644 index 0000000..3b12e87 --- /dev/null +++ b/ShiftOS.Server.WebAdmin/Resources/LoginView.txt @@ -0,0 +1,20 @@ +

Access denied.

+ +

You require a higher authentication level to access this part of the multi-user domain. Please enter the username and password of whom has access to this sector.

+ +
+ + + + + + + + + + + + +
Username:
Password:
Login +
+
\ No newline at end of file diff --git a/ShiftOS.Server.WebAdmin/ShiftOS.Server.WebAdmin.csproj b/ShiftOS.Server.WebAdmin/ShiftOS.Server.WebAdmin.csproj new file mode 100644 index 0000000..ce9fc77 --- /dev/null +++ b/ShiftOS.Server.WebAdmin/ShiftOS.Server.WebAdmin.csproj @@ -0,0 +1,104 @@ + + + + + Debug + AnyCPU + {B29FDD06-E6FE-40A2-8258-283728CED81A} + Exe + Properties + ShiftOS.Server.WebAdmin + ShiftOS.Server.WebAdmin + v4.5.2 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Nancy.1.4.3\lib\net40\Nancy.dll + True + + + ..\packages\Nancy.Authentication.Forms.1.4.1\lib\net40\Nancy.Authentication.Forms.dll + True + + + ..\packages\Nancy.Authentication.Stateless.1.4.1\lib\net40\Nancy.Authentication.Stateless.dll + True + + + ..\packages\Nancy.Hosting.Self.1.4.1\lib\net40\Nancy.Hosting.Self.dll + True + + + ..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll + True + + + + + + + + + + + + + + + True + True + Resources.resx + + + + + + + + + {226c63b4-e60d-4949-b4e7-7a2ddbb96776} + ShiftOS.Server + + + + + ResXFileCodeGenerator + Resources.Designer.cs + + + + + + + + + + + \ No newline at end of file diff --git a/ShiftOS.Server.WebAdmin/packages.config b/ShiftOS.Server.WebAdmin/packages.config new file mode 100644 index 0000000..a403e33 --- /dev/null +++ b/ShiftOS.Server.WebAdmin/packages.config @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file