From 46b4bb61a81354de5ac2260c0505e79dfcf06a5b Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 11 Feb 2017 18:51:20 -0500 Subject: Begin admin panel work. --- ShiftOS.Server.WebAdmin/Program.cs | 188 +++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 ShiftOS.Server.WebAdmin/Program.cs (limited to 'ShiftOS.Server.WebAdmin/Program.cs') 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"); + }; + } + } +} -- cgit v1.2.3