From 6054f0d436c9412a2c4a96eab504dd8bf247e953 Mon Sep 17 00:00:00 2001 From: Jamie Rees Date: Fri, 26 Feb 2016 20:05:43 +0000 Subject: [PATCH] more work --- RequestPlex.Api/PlexApi.cs | 43 +++++++++++++ RequestPlex.Api/RequestPlex.Api.csproj | 9 +++ RequestPlex.Api/packages.config | 2 + RequestPlex.Core/Setup.cs | 2 +- RequestPlex.Core/UserMapper.cs | 2 +- RequestPlex.Store/SettingsModel.cs | 2 + RequestPlex.Store/SqlTables.sql | 4 +- RequestPlex.UI/Content/search.js | 12 ---- RequestPlex.UI/Content/site.js | 13 ++++ RequestPlex.UI/Models/PlexAuth.cs | 14 ++++ RequestPlex.UI/Modules/AdminModule.cs | 31 ++++++++- RequestPlex.UI/RequestPlex.UI.csproj | 5 +- RequestPlex.UI/Views/Admin/Settings.cshtml | 74 +++++++++++++++++++++- RequestPlex.UI/Views/Admin/_Sidebar.cshtml | 1 - RequestPlex.UI/Views/Shared/_Layout.cshtml | 1 + 15 files changed, 192 insertions(+), 23 deletions(-) create mode 100644 RequestPlex.Api/PlexApi.cs create mode 100644 RequestPlex.UI/Content/site.js create mode 100644 RequestPlex.UI/Models/PlexAuth.cs diff --git a/RequestPlex.Api/PlexApi.cs b/RequestPlex.Api/PlexApi.cs new file mode 100644 index 000000000..6febf79a5 --- /dev/null +++ b/RequestPlex.Api/PlexApi.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Collections.Specialized; +using System.Diagnostics; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using Nancy.Json; + +namespace RequestPlex.Api +{ + public class PlexApi + { + public void GetToken(string username, string password) + { + var plainTextBytes = System.Text.Encoding.UTF8.GetBytes("username:password"); + string auth = System.Convert.ToBase64String(plainTextBytes); + + using (var client = new WebClient()) + { + var values = new NameValueCollection + { + ["Authorization"] = "Basic " + auth, + ["X-Plex-Client-Identifier"] = "RequestPlex0001", + ["X-Plex-Product"] = "Request Plex", + ["X-Plex-Version"] = "0.1.0" + }; + + client.Headers.Add(values); + + var response = client.UploadString("https://plex.tv/users/sign_in.json", ""); + + var json = new JavaScriptSerializer(); + dynamic result = json.DeserializeObject(response); + + var token = result["user"]["authentication_token"]; + + Debug.WriteLine(token); + } + } + } +} diff --git a/RequestPlex.Api/RequestPlex.Api.csproj b/RequestPlex.Api/RequestPlex.Api.csproj index 1feb7d421..e8df82888 100644 --- a/RequestPlex.Api/RequestPlex.Api.csproj +++ b/RequestPlex.Api/RequestPlex.Api.csproj @@ -30,6 +30,14 @@ 4 + + ..\packages\Dapper.1.42\lib\net45\Dapper.dll + True + + + ..\packages\Nancy.1.4.3\lib\net40\Nancy.dll + True + ..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll True @@ -54,6 +62,7 @@ + diff --git a/RequestPlex.Api/packages.config b/RequestPlex.Api/packages.config index bab29a22b..50b670221 100644 --- a/RequestPlex.Api/packages.config +++ b/RequestPlex.Api/packages.config @@ -1,5 +1,7 @@  + + diff --git a/RequestPlex.Core/Setup.cs b/RequestPlex.Core/Setup.cs index f02cc6355..5321b67fd 100644 --- a/RequestPlex.Core/Setup.cs +++ b/RequestPlex.Core/Setup.cs @@ -10,7 +10,7 @@ namespace RequestPlex.Core public void SetupDb() { var db = new DbConfiguration(new SqliteFactory()); - db.CreateDatabase(); + db.CheckDb(); TableCreation.CreateTables(db.DbConnection()); } } diff --git a/RequestPlex.Core/UserMapper.cs b/RequestPlex.Core/UserMapper.cs index 2a6f5c820..89f724143 100644 --- a/RequestPlex.Core/UserMapper.cs +++ b/RequestPlex.Core/UserMapper.cs @@ -37,7 +37,7 @@ namespace RequestPlex.Core var db = new DbConfiguration(new SqliteFactory()); var repo = new UserRepository(db); var users = repo.GetAll(); - var userRecord = users.FirstOrDefault(u => u.UserName == username && u.Password == password); // TODO hashing + var userRecord = users.FirstOrDefault(u => u.UserName.Equals(username, StringComparison.InvariantCultureIgnoreCase) && u.Password.Equals(password)); // TODO hashing if (userRecord == null) { diff --git a/RequestPlex.Store/SettingsModel.cs b/RequestPlex.Store/SettingsModel.cs index 7b6ed8bad..573f3dfec 100644 --- a/RequestPlex.Store/SettingsModel.cs +++ b/RequestPlex.Store/SettingsModel.cs @@ -8,5 +8,7 @@ namespace RequestPlex.Store public class SettingsModel : Entity { public int Port { get; set; } + public bool UserAuthentication { get; set; } + public string PlexAuthToken { get; set; } } } diff --git a/RequestPlex.Store/SqlTables.sql b/RequestPlex.Store/SqlTables.sql index 8b035ae0f..90cdc81ac 100644 --- a/RequestPlex.Store/SqlTables.sql +++ b/RequestPlex.Store/SqlTables.sql @@ -11,7 +11,9 @@ CREATE TABLE IF NOT EXISTS User CREATE TABLE IF NOT EXISTS Settings ( Id INTEGER PRIMARY KEY AUTOINCREMENT, - Port INTEGER NOT NULL + Port INTEGER NOT NULL, + UserAuthentication INTEGER NOT NULL, + PlexAuthToken varchar(50) ); CREATE TABLE IF NOT EXISTS Requested diff --git a/RequestPlex.UI/Content/search.js b/RequestPlex.UI/Content/search.js index e4ebdf7c9..0f0898521 100644 --- a/RequestPlex.UI/Content/search.js +++ b/RequestPlex.UI/Content/search.js @@ -17,7 +17,6 @@ $("#tvSearchContent").on("keyup", function (e) { tvimer = setTimeout(tvSearch(), 400); }); - $("#test").click(function (e) { e.preventDefault(); @@ -104,14 +103,3 @@ function buildTvShowContext(result) { }; return context; } - -function generateNotify(message, type) { - // type = danger, warning, info, successs - $.notify({ - // options - message: message - }, { - // settings - type: type - }); -} \ No newline at end of file diff --git a/RequestPlex.UI/Content/site.js b/RequestPlex.UI/Content/site.js new file mode 100644 index 000000000..8f69d979c --- /dev/null +++ b/RequestPlex.UI/Content/site.js @@ -0,0 +1,13 @@ + + + +function generateNotify(message, type) { + // type = danger, warning, info, successs + $.notify({ + // options + message: message + }, { + // settings + type: type + }); +} \ No newline at end of file diff --git a/RequestPlex.UI/Models/PlexAuth.cs b/RequestPlex.UI/Models/PlexAuth.cs new file mode 100644 index 000000000..3c90b131e --- /dev/null +++ b/RequestPlex.UI/Models/PlexAuth.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RequestPlex.UI.Models +{ + public class PlexAuth + { + public string username { get; set; } + public string password { get; set; } + } +} diff --git a/RequestPlex.UI/Modules/AdminModule.cs b/RequestPlex.UI/Modules/AdminModule.cs index 837a41e51..e8cc8032c 100644 --- a/RequestPlex.UI/Modules/AdminModule.cs +++ b/RequestPlex.UI/Modules/AdminModule.cs @@ -1,10 +1,17 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.ComponentModel; using System.Dynamic; - +using System.Linq; using Nancy; using Nancy.Extensions; +using Nancy.ModelBinding; using Nancy.Security; - +using Newtonsoft.Json; +using RequestPlex.Api; using RequestPlex.Core; +using RequestPlex.UI.Models; namespace RequestPlex.UI.Modules { @@ -12,12 +19,14 @@ namespace RequestPlex.UI.Modules { public AdminModule() { +#if !DEBUG this.RequiresAuthentication(); +#endif Get["admin/"] = _ => { dynamic model = new ExpandoObject(); model.Errored = Request.Query.error.HasValue; - + model.Port = null; var s = new SettingsService(); var settings = s.GetSettings(); if (settings != null) @@ -42,6 +51,22 @@ namespace RequestPlex.UI.Modules s.SaveSettings(port); + return Context.GetRedirect("~/admin"); + }; + + Post["admin/requestauth"] = _ => + { + var user = this.Bind(); + + if (string.IsNullOrEmpty(user.username) || string.IsNullOrEmpty(user.password)) + { + return Context.GetRedirect("~/admin?error=true"); + } + + var plex = new PlexApi(); + plex.GetToken(user.username, user.password); + + return Context.GetRedirect("~/admin"); }; diff --git a/RequestPlex.UI/RequestPlex.UI.csproj b/RequestPlex.UI/RequestPlex.UI.csproj index 9a1e1b910..fe38dc27e 100644 --- a/RequestPlex.UI/RequestPlex.UI.csproj +++ b/RequestPlex.UI/RequestPlex.UI.csproj @@ -108,6 +108,7 @@ PreserveNewest + @@ -136,6 +137,9 @@ PreserveNewest + + PreserveNewest + @@ -193,7 +197,6 @@ - diff --git a/RequestPlex.UI/Views/Admin/Settings.cshtml b/RequestPlex.UI/Views/Admin/Settings.cshtml index db61016f0..c4f586161 100644 --- a/RequestPlex.UI/Views/Admin/Settings.cshtml +++ b/RequestPlex.UI/Views/Admin/Settings.cshtml @@ -1,9 +1,22 @@ @Html.Partial("/Admin/_Sidebar") @{ - var port = Model.Port ?? 0; + int port; + if (Model.Port == null) + { + port = 3579; + } + else + { + port = Model.Port; + } + + //if (string.IsNullOrEmpty(Model.PlexAuthToken)) + //{ + // Model.PlexAuthToken = string.Empty; + //} }
-
+
Request Plex Settings
@@ -12,6 +25,32 @@
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+
+
+
+ +
+
+ +
+ Please note, you will have to restart after changing these settings. +
@@ -29,4 +68,33 @@ Please enter in a correct port number
-} \ No newline at end of file +} + + \ No newline at end of file diff --git a/RequestPlex.UI/Views/Admin/_Sidebar.cshtml b/RequestPlex.UI/Views/Admin/_Sidebar.cshtml index 6aeec9384..9e21b91e3 100644 --- a/RequestPlex.UI/Views/Admin/_Sidebar.cshtml +++ b/RequestPlex.UI/Views/Admin/_Sidebar.cshtml @@ -4,6 +4,5 @@ CouchPotato Settings Sonarr Settings Sickbeard Settings - Authentication
\ No newline at end of file diff --git a/RequestPlex.UI/Views/Shared/_Layout.cshtml b/RequestPlex.UI/Views/Shared/_Layout.cshtml index 86fa867c2..fabadd7d5 100644 --- a/RequestPlex.UI/Views/Shared/_Layout.cshtml +++ b/RequestPlex.UI/Views/Shared/_Layout.cshtml @@ -11,6 +11,7 @@ +