|
|
@ -30,16 +30,17 @@ using Nancy;
|
|
|
|
using Nancy.Extensions;
|
|
|
|
using Nancy.Extensions;
|
|
|
|
using Nancy.Responses.Negotiation;
|
|
|
|
using Nancy.Responses.Negotiation;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using NLog;
|
|
|
|
|
|
|
|
|
|
|
|
using PlexRequests.Api.Interfaces;
|
|
|
|
using PlexRequests.Api.Interfaces;
|
|
|
|
using PlexRequests.Api.Models;
|
|
|
|
|
|
|
|
using PlexRequests.Api.Models.Plex;
|
|
|
|
using PlexRequests.Api.Models.Plex;
|
|
|
|
using PlexRequests.Core;
|
|
|
|
using PlexRequests.Core;
|
|
|
|
using PlexRequests.Core.SettingModels;
|
|
|
|
using PlexRequests.Core.SettingModels;
|
|
|
|
|
|
|
|
using PlexRequests.Helpers;
|
|
|
|
using PlexRequests.UI.Models;
|
|
|
|
using PlexRequests.UI.Models;
|
|
|
|
|
|
|
|
|
|
|
|
namespace PlexRequests.UI.Modules
|
|
|
|
namespace PlexRequests.UI.Modules
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// TODO: Add ability to logout
|
|
|
|
|
|
|
|
public class UserLoginModule : NancyModule
|
|
|
|
public class UserLoginModule : NancyModule
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public UserLoginModule(ISettingsService<AuthenticationSettings> auth, IPlexApi api) : base("userlogin")
|
|
|
|
public UserLoginModule(ISettingsService<AuthenticationSettings> auth, IPlexApi api) : base("userlogin")
|
|
|
@ -54,6 +55,8 @@ namespace PlexRequests.UI.Modules
|
|
|
|
private ISettingsService<AuthenticationSettings> AuthService { get; }
|
|
|
|
private ISettingsService<AuthenticationSettings> AuthService { get; }
|
|
|
|
private IPlexApi Api { get; }
|
|
|
|
private IPlexApi Api { get; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
|
|
|
|
|
public Negotiator Index()
|
|
|
|
public Negotiator Index()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var settings = AuthService.GetSettings();
|
|
|
|
var settings = AuthService.GetSettings();
|
|
|
@ -63,7 +66,7 @@ namespace PlexRequests.UI.Modules
|
|
|
|
private Response LoginUser()
|
|
|
|
private Response LoginUser()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var username = Request.Form.username.Value;
|
|
|
|
var username = Request.Form.username.Value;
|
|
|
|
|
|
|
|
Log.Debug("Username \"{0}\" attempting to login",username);
|
|
|
|
if (string.IsNullOrWhiteSpace(username))
|
|
|
|
if (string.IsNullOrWhiteSpace(username))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return Response.AsJson(new JsonResponseModel { Result = false, Message = "Incorrect User or Password" });
|
|
|
|
return Response.AsJson(new JsonResponseModel { Result = false, Message = "Incorrect User or Password" });
|
|
|
@ -72,39 +75,49 @@ namespace PlexRequests.UI.Modules
|
|
|
|
var authenticated = false;
|
|
|
|
var authenticated = false;
|
|
|
|
|
|
|
|
|
|
|
|
var settings = AuthService.GetSettings();
|
|
|
|
var settings = AuthService.GetSettings();
|
|
|
|
|
|
|
|
Log.Debug("Settings: ");
|
|
|
|
|
|
|
|
Log.Debug(settings.DumpJson());
|
|
|
|
|
|
|
|
|
|
|
|
if (IsUserInDeniedList(username, settings))
|
|
|
|
if (IsUserInDeniedList(username, settings))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
Log.Debug("User is in denied list, not allowing them to authenticate");
|
|
|
|
return Response.AsJson(new JsonResponseModel { Result = false, Message = "Incorrect User or Password" });
|
|
|
|
return Response.AsJson(new JsonResponseModel { Result = false, Message = "Incorrect User or Password" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var password = string.Empty;
|
|
|
|
var password = string.Empty;
|
|
|
|
if (settings.UsePassword)
|
|
|
|
if (settings.UsePassword)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
Log.Debug("Using password");
|
|
|
|
password = Request.Form.password.Value;
|
|
|
|
password = Request.Form.password.Value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (settings.UserAuthentication && settings.UsePassword) // Authenticate with Plex
|
|
|
|
if (settings.UserAuthentication && settings.UsePassword) // Authenticate with Plex
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
Log.Debug("Need to auth and also provide pass");
|
|
|
|
var signedIn = (PlexAuthentication)Api.SignIn(username, password);
|
|
|
|
var signedIn = (PlexAuthentication)Api.SignIn(username, password);
|
|
|
|
if (signedIn.user?.authentication_token != null)
|
|
|
|
if (signedIn.user?.authentication_token != null)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
Log.Debug("Correct credentials, checking if the user is in the friends list");
|
|
|
|
authenticated = CheckIfUserIsInPlexFriends(username, settings.PlexAuthToken);
|
|
|
|
authenticated = CheckIfUserIsInPlexFriends(username, settings.PlexAuthToken);
|
|
|
|
|
|
|
|
Log.Debug("Friends list result = {0}", authenticated);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(settings.UserAuthentication) // Check against the users in Plex
|
|
|
|
else if(settings.UserAuthentication) // Check against the users in Plex
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
Log.Debug("Need to auth");
|
|
|
|
authenticated = CheckIfUserIsInPlexFriends(username, settings.PlexAuthToken);
|
|
|
|
authenticated = CheckIfUserIsInPlexFriends(username, settings.PlexAuthToken);
|
|
|
|
|
|
|
|
Log.Debug("Friends list result = {0}", authenticated);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(!settings.UserAuthentication) // No auth, let them pass!
|
|
|
|
else if(!settings.UserAuthentication) // No auth, let them pass!
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
Log.Debug("No need to auth");
|
|
|
|
authenticated = true;
|
|
|
|
authenticated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (authenticated)
|
|
|
|
if (authenticated)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
Log.Debug("We are authenticated! Setting session.");
|
|
|
|
// Add to the session (Used in the BaseModules)
|
|
|
|
// Add to the session (Used in the BaseModules)
|
|
|
|
Session[SessionKeys.UsernameKey] = (string)username;
|
|
|
|
Session[SessionKeys.UsernameKey] = (string)username;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -116,6 +129,7 @@ namespace PlexRequests.UI.Modules
|
|
|
|
|
|
|
|
|
|
|
|
private Response Logout()
|
|
|
|
private Response Logout()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
Log.Debug("Logging Out");
|
|
|
|
if (Session[SessionKeys.UsernameKey] != null)
|
|
|
|
if (Session[SessionKeys.UsernameKey] != null)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Session.Delete(SessionKeys.UsernameKey);
|
|
|
|
Session.Delete(SessionKeys.UsernameKey);
|
|
|
@ -126,6 +140,8 @@ namespace PlexRequests.UI.Modules
|
|
|
|
private bool CheckIfUserIsInPlexFriends(string username, string authToken)
|
|
|
|
private bool CheckIfUserIsInPlexFriends(string username, string authToken)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var users = Api.GetUsers(authToken);
|
|
|
|
var users = Api.GetUsers(authToken);
|
|
|
|
|
|
|
|
Log.Debug("Plex Users: ");
|
|
|
|
|
|
|
|
Log.Debug(users.DumpJson());
|
|
|
|
return users.User.Any(x => x.Username == username);
|
|
|
|
return users.User.Any(x => x.Username == username);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|