Started the user auth

This commit is contained in:
tidusjar 2016-03-02 15:18:06 +00:00
parent 48fd182e52
commit a8b6137ef8
9 changed files with 115 additions and 22 deletions

View file

@ -4,6 +4,7 @@ using Nancy;
using Nancy.Authentication.Forms; using Nancy.Authentication.Forms;
using Nancy.Bootstrapper; using Nancy.Bootstrapper;
using Nancy.Diagnostics; using Nancy.Diagnostics;
using Nancy.Session;
using Nancy.TinyIoc; using Nancy.TinyIoc;
using RequestPlex.Core; using RequestPlex.Core;
@ -40,7 +41,9 @@ namespace RequestPlex.UI
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines) protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{ {
CookieBasedSessions.Enable(pipelines);
StaticConfiguration.DisableErrorTraces = false; StaticConfiguration.DisableErrorTraces = false;
base.ApplicationStartup(container, pipelines); base.ApplicationStartup(container, pipelines);
// Enable forms auth // Enable forms auth

View file

@ -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";
}
}

View file

@ -26,7 +26,7 @@
#endregion #endregion
using System.Dynamic; using System.Dynamic;
using System.Linq; using System.Linq;
using System.Web.UI;
using Nancy; using Nancy;
using Nancy.Extensions; using Nancy.Extensions;
using Nancy.ModelBinding; using Nancy.ModelBinding;

View file

@ -28,12 +28,11 @@ namespace RequestPlex.UI.Modules
Post["/login"] = x => Post["/login"] = x =>
{ {
var userId = UserMapper.ValidateUser((string)Request.Form.Username, (string)Request.Form.Password);
var userId = UserMapper.ValidateUser((string)this.Request.Form.Username, (string)this.Request.Form.Password);
if (userId == null) 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; DateTime? expiry = null;
if (Request.Form.RememberMe.HasValue) if (Request.Form.RememberMe.HasValue)
@ -63,8 +62,6 @@ namespace RequestPlex.UI.Modules
var userId = UserMapper.CreateUser(Request.Form.Username, Request.Form.Password); var userId = UserMapper.CreateUser(Request.Form.Username, Request.Form.Password);
return this.LoginAndRedirect((Guid)userId); return this.LoginAndRedirect((Guid)userId);
}; };
} }
} }
} }

View file

@ -50,7 +50,7 @@ namespace RequestPlex.UI.Modules
Post["/delete"] = _ => Post["/delete"] = _ =>
{ {
var convertedType = (string)Request.Form.type == "movie" ? RequestType.Movie : RequestType.TvShow; 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); 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); var currentEntity = Service.GetAll().FirstOrDefault(x => x.Tmdbid == tmdbId && x.Type == type);
Service.Delete(currentEntity); Service.Delete(currentEntity);

View file

@ -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("");
}
}
}

View file

@ -48,21 +48,13 @@ namespace RequestPlex.UI
s.SetupDb(); s.SetupDb();
var uri = GetStartupUri(); var uri = GetStartupUri();
try
{
using (WebApp.Start<Startup>(uri))
{
Console.WriteLine($"Request Plex is running on {uri}");
Console.WriteLine("Press any key to exit");
Console.ReadLine();
}
}
catch (Exception e)
{
throw;
}
using (WebApp.Start<Startup>(uri))
{
Console.WriteLine($"Request Plex is running on {uri}");
Console.WriteLine("Press any key to exit");
Console.ReadLine();
}
} }
private static void WriteOutVersion() private static void WriteOutVersion()

View file

@ -131,8 +131,10 @@
</Content> </Content>
<Compile Include="Models\PlexAuth.cs" /> <Compile Include="Models\PlexAuth.cs" />
<Compile Include="Models\RequestViewModel.cs" /> <Compile Include="Models\RequestViewModel.cs" />
<Compile Include="Models\SessionKeys.cs" />
<Compile Include="Modules\AdminModule.cs" /> <Compile Include="Modules\AdminModule.cs" />
<Compile Include="Modules\IndexModule.cs" /> <Compile Include="Modules\IndexModule.cs" />
<Compile Include="Modules\UserLoginModule.cs" />
<Compile Include="Modules\LoginModule.cs" /> <Compile Include="Modules\LoginModule.cs" />
<Compile Include="Modules\RequestsModule.cs" /> <Compile Include="Modules\RequestsModule.cs" />
<Compile Include="Modules\SearchModule.cs" /> <Compile Include="Modules\SearchModule.cs" />
@ -219,6 +221,9 @@
<Content Include="Views\Admin\CouchPotato.cshtml"> <Content Include="Views\Admin\CouchPotato.cshtml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content> </Content>
<Content Include="Views\UserLogin\Index.cshtml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Include="Web.Debug.config"> <None Include="Web.Debug.config">
<DependentUpon>web.config</DependentUpon> <DependentUpon>web.config</DependentUpon>
</None> </None>

View file

@ -0,0 +1,7 @@
<form method="POST">
Username <input class="form-control" type="text" name="Username"/>
<br/>
Password <input class="form-control" name="Password" type="password"/>
<br/>
<input class="btn btn-success" type="submit" value="Login"/>
</form>