From a8b6137ef8da6cda481e24b61ba6bbf6a960ec3e Mon Sep 17 00:00:00 2001 From: tidusjar Date: Wed, 2 Mar 2016 15:18:06 +0000 Subject: [PATCH] Started the user auth --- RequestPlex.UI/Bootstrapper.cs | 3 ++ RequestPlex.UI/Models/Sessionkeys.cs | 33 ++++++++++++ RequestPlex.UI/Modules/AdminModule.cs | 2 +- RequestPlex.UI/Modules/LoginModule.cs | 7 +-- RequestPlex.UI/Modules/RequestsModule.cs | 4 +- RequestPlex.UI/Modules/UserLoginModule.cs | 56 +++++++++++++++++++++ RequestPlex.UI/Program.cs | 18 ++----- RequestPlex.UI/RequestPlex.UI.csproj | 5 ++ RequestPlex.UI/Views/UserLogin/Index.cshtml | 7 +++ 9 files changed, 114 insertions(+), 21 deletions(-) create mode 100644 RequestPlex.UI/Models/Sessionkeys.cs create mode 100644 RequestPlex.UI/Modules/UserLoginModule.cs create mode 100644 RequestPlex.UI/Views/UserLogin/Index.cshtml diff --git a/RequestPlex.UI/Bootstrapper.cs b/RequestPlex.UI/Bootstrapper.cs index f271e381d..169a601f5 100644 --- a/RequestPlex.UI/Bootstrapper.cs +++ b/RequestPlex.UI/Bootstrapper.cs @@ -4,6 +4,7 @@ using Nancy; using Nancy.Authentication.Forms; using Nancy.Bootstrapper; using Nancy.Diagnostics; +using Nancy.Session; using Nancy.TinyIoc; using RequestPlex.Core; @@ -40,7 +41,9 @@ namespace RequestPlex.UI protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines) { + CookieBasedSessions.Enable(pipelines); StaticConfiguration.DisableErrorTraces = false; + base.ApplicationStartup(container, pipelines); // Enable forms auth diff --git a/RequestPlex.UI/Models/Sessionkeys.cs b/RequestPlex.UI/Models/Sessionkeys.cs new file mode 100644 index 000000000..d7cbc0f24 --- /dev/null +++ b/RequestPlex.UI/Models/Sessionkeys.cs @@ -0,0 +1,33 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: SessionKeys.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +namespace RequestPlex.UI.Models +{ + public class SessionKeys + { + public const string UsernameKey = "Username"; + } +} diff --git a/RequestPlex.UI/Modules/AdminModule.cs b/RequestPlex.UI/Modules/AdminModule.cs index deba84a1d..04c09ba88 100644 --- a/RequestPlex.UI/Modules/AdminModule.cs +++ b/RequestPlex.UI/Modules/AdminModule.cs @@ -26,7 +26,7 @@ #endregion using System.Dynamic; using System.Linq; -using System.Web.UI; + using Nancy; using Nancy.Extensions; using Nancy.ModelBinding; diff --git a/RequestPlex.UI/Modules/LoginModule.cs b/RequestPlex.UI/Modules/LoginModule.cs index 4465c958e..4ea2c0c1c 100644 --- a/RequestPlex.UI/Modules/LoginModule.cs +++ b/RequestPlex.UI/Modules/LoginModule.cs @@ -28,12 +28,11 @@ namespace RequestPlex.UI.Modules Post["/login"] = x => { - - var userId = UserMapper.ValidateUser((string)this.Request.Form.Username, (string)this.Request.Form.Password); + var userId = UserMapper.ValidateUser((string)Request.Form.Username, (string)Request.Form.Password); if (userId == null) { - return this.Context.GetRedirect("~/login?error=true&username=" + (string)this.Request.Form.Username); + return Context.GetRedirect("~/login?error=true&username=" + (string)Request.Form.Username); } DateTime? expiry = null; if (Request.Form.RememberMe.HasValue) @@ -63,8 +62,6 @@ namespace RequestPlex.UI.Modules var userId = UserMapper.CreateUser(Request.Form.Username, Request.Form.Password); return this.LoginAndRedirect((Guid)userId); }; - - } } } \ No newline at end of file diff --git a/RequestPlex.UI/Modules/RequestsModule.cs b/RequestPlex.UI/Modules/RequestsModule.cs index 3fec747ed..87478c498 100644 --- a/RequestPlex.UI/Modules/RequestsModule.cs +++ b/RequestPlex.UI/Modules/RequestsModule.cs @@ -50,7 +50,7 @@ namespace RequestPlex.UI.Modules Post["/delete"] = _ => { var convertedType = (string)Request.Form.type == "movie" ? RequestType.Movie : RequestType.TvShow; - return Delete((int)Request.Form.id, convertedType); + return DeleteRequest((int)Request.Form.id, convertedType); }; } @@ -106,7 +106,7 @@ namespace RequestPlex.UI.Modules return Response.AsJson(viewModel); } - private Response Delete(int tmdbId, RequestType type) + private Response DeleteRequest(int tmdbId, RequestType type) { var currentEntity = Service.GetAll().FirstOrDefault(x => x.Tmdbid == tmdbId && x.Type == type); Service.Delete(currentEntity); diff --git a/RequestPlex.UI/Modules/UserLoginModule.cs b/RequestPlex.UI/Modules/UserLoginModule.cs new file mode 100644 index 000000000..3acb55b34 --- /dev/null +++ b/RequestPlex.UI/Modules/UserLoginModule.cs @@ -0,0 +1,56 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: UserLoginModule.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +using Nancy; + +using RequestPlex.UI.Models; + +namespace RequestPlex.UI.Modules +{ + // TODO: Check the settings to see if we need to authenticate + // TODO: Add ability to logout + // TODO: Create UserLogin page + // TODO: If we need to authenticate we need to check if they are in Plex + // TODO: Allow the user of a username only or a Username and password + public class UserLoginModule : NancyModule + { + public UserLoginModule() : base("userlogin") + { + Get["/"] = _ => View["Index"]; + Post["/"] = x => LoginUser(); + } + + private Response LoginUser() + { + var username = Request.Form.username; + + // Add to the session + Request.Session[SessionKeys.UsernameKey] = username; + + return Response.AsJson(""); + } + } +} \ No newline at end of file diff --git a/RequestPlex.UI/Program.cs b/RequestPlex.UI/Program.cs index a34b3d5d0..8af87e2cb 100644 --- a/RequestPlex.UI/Program.cs +++ b/RequestPlex.UI/Program.cs @@ -48,21 +48,13 @@ namespace RequestPlex.UI s.SetupDb(); var uri = GetStartupUri(); - try - { - using (WebApp.Start(uri)) - { - Console.WriteLine($"Request Plex is running on {uri}"); - Console.WriteLine("Press any key to exit"); - Console.ReadLine(); - } - } - catch (Exception e) + + using (WebApp.Start(uri)) { - - throw; + Console.WriteLine($"Request Plex is running on {uri}"); + Console.WriteLine("Press any key to exit"); + Console.ReadLine(); } - } private static void WriteOutVersion() diff --git a/RequestPlex.UI/RequestPlex.UI.csproj b/RequestPlex.UI/RequestPlex.UI.csproj index b0ac33a7c..535c3e9f8 100644 --- a/RequestPlex.UI/RequestPlex.UI.csproj +++ b/RequestPlex.UI/RequestPlex.UI.csproj @@ -131,8 +131,10 @@ + + @@ -219,6 +221,9 @@ Always + + Always + web.config diff --git a/RequestPlex.UI/Views/UserLogin/Index.cshtml b/RequestPlex.UI/Views/UserLogin/Index.cshtml new file mode 100644 index 000000000..e1f1fa2ce --- /dev/null +++ b/RequestPlex.UI/Views/UserLogin/Index.cshtml @@ -0,0 +1,7 @@ +
+ Username +
+ Password +
+ +
\ No newline at end of file