diff --git a/RequestPlex.Api/Models/MovieSearch.cs b/RequestPlex.Api/Models/MovieSearch.cs deleted file mode 100644 index 1b49a9629..000000000 --- a/RequestPlex.Api/Models/MovieSearch.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace RequestPlex.Api.Models -{ - public class MovieResult - { - public bool adult { get; set; } - public string backdrop_path { get; set; } - public List genre_ids { get; set; } - public int id { get; set; } - public string original_language { get; set; } - public string original_title { get; set; } - public string overview { get; set; } - public string release_date { get; set; } - public string poster_path { get; set; } - public double popularity { get; set; } - public string title { get; set; } - public bool video { get; set; } - public double vote_average { get; set; } - public int vote_count { get; set; } - } - - public class MovieSearch - { - public int page { get; set; } - public List results { get; set; } - public int total_pages { get; set; } - public int total_results { get; set; } - } -} diff --git a/RequestPlex.Api/Models/PlexAuthentication.cs b/RequestPlex.Api/Models/PlexAuthentication.cs new file mode 100644 index 000000000..cd0dcf76f --- /dev/null +++ b/RequestPlex.Api/Models/PlexAuthentication.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Newtonsoft.Json; + +namespace RequestPlex.Api.Models +{ + public class PlexAuthentication + { + public User user { get; set; } + } + public class Subscription + { + public bool active { get; set; } + public string status { get; set; } + public object plan { get; set; } + public object features { get; set; } + } + + public class Roles + { + public List roles { get; set; } + } + + public class User + { + public string email { get; set; } + public string joined_at { get; set; } + public string username { get; set; } + public string title { get; set; } + public string authentication_token { get; set; } + public Subscription subscription { get; set; } + public Roles roles { get; set; } + public List entitlements { get; set; } + public object confirmed_at { get; set; } + public int forum_id { get; set; } + } +} diff --git a/RequestPlex.Api/Models/PlexUserRequest.cs b/RequestPlex.Api/Models/PlexUserRequest.cs new file mode 100644 index 000000000..34177a4ba --- /dev/null +++ b/RequestPlex.Api/Models/PlexUserRequest.cs @@ -0,0 +1,14 @@ + +namespace RequestPlex.Api.Models +{ + public class PlexUserRequest + { + public UserRequest user { get; set; } + } + + public class UserRequest + { + public string login { get; set; } + public string password { get; set; } + } +} diff --git a/RequestPlex.Api/PlexApi.cs b/RequestPlex.Api/PlexApi.cs index 6febf79a5..79690949e 100644 --- a/RequestPlex.Api/PlexApi.cs +++ b/RequestPlex.Api/PlexApi.cs @@ -1,43 +1,51 @@ 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; +using RequestPlex.Api.Models; +using RestSharp; namespace RequestPlex.Api { public class PlexApi { - public void GetToken(string username, string password) + public PlexAuthentication 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 userModel = new PlexUserRequest { - var values = new NameValueCollection + user = new UserRequest { - ["Authorization"] = "Basic " + auth, - ["X-Plex-Client-Identifier"] = "RequestPlex0001", - ["X-Plex-Product"] = "Request Plex", - ["X-Plex-Version"] = "0.1.0" - }; + password = password, + login = username + }, + }; + var request = new RestRequest + { + Method = Method.POST, + }; - client.Headers.Add(values); + request.AddHeader("X-Plex-Client-Identifier", "Test213"); + request.AddHeader("X-Plex-Product", "Request Plex"); + request.AddHeader("X-Plex-Version", "0.0.1"); + request.AddHeader("Content-Type", "application/json"); + + request.AddJsonBody(userModel); - var response = client.UploadString("https://plex.tv/users/sign_in.json", ""); + var api = new ApiRequest(); + return api.Execute(request, new Uri("https://plex.tv/users/sign_in.json")); + } - var json = new JavaScriptSerializer(); - dynamic result = json.DeserializeObject(response); + public void GetUsers(string authToken) + { + var request = new RestRequest + { + Method = Method.POST, + }; - var token = result["user"]["authentication_token"]; + request.AddHeader("X-Plex-Client-Identifier", "Test213"); + request.AddHeader("X-Plex-Product", "Request Plex"); + request.AddHeader("X-Plex-Version", "0.0.1"); + request.AddHeader("X-Plex-Token", authToken); + request.AddHeader("Content-Type", "application/json"); - Debug.WriteLine(token); - } } } } + diff --git a/RequestPlex.Api/RequestPlex.Api.csproj b/RequestPlex.Api/RequestPlex.Api.csproj index e8df82888..c04ba5870 100644 --- a/RequestPlex.Api/RequestPlex.Api.csproj +++ b/RequestPlex.Api/RequestPlex.Api.csproj @@ -61,7 +61,8 @@ - + + diff --git a/RequestPlex.Core/SettingsService.cs b/RequestPlex.Core/SettingsService.cs index 81b33cc5d..c9d86a44e 100644 --- a/RequestPlex.Core/SettingsService.cs +++ b/RequestPlex.Core/SettingsService.cs @@ -12,7 +12,7 @@ namespace RequestPlex.Core { public class SettingsService { - public void SaveSettings(int port) + public void SaveSettings(SettingsModel model) { var db = new DbConfiguration(new SqliteFactory()); var repo = new GenericRepository(db); @@ -20,13 +20,12 @@ namespace RequestPlex.Core var existingSettings = repo.GetAll().FirstOrDefault(); if (existingSettings != null) { - existingSettings.Port = port; + existingSettings = model; repo.Update(existingSettings); return; } - var newSettings = new SettingsModel { Port = port }; - repo.Insert(newSettings); + repo.Insert(model); } public SettingsModel GetSettings() diff --git a/RequestPlex.UI/Modules/AdminModule.cs b/RequestPlex.UI/Modules/AdminModule.cs index e8cc8032c..1aa0a6392 100644 --- a/RequestPlex.UI/Modules/AdminModule.cs +++ b/RequestPlex.UI/Modules/AdminModule.cs @@ -11,6 +11,7 @@ using Nancy.Security; using Newtonsoft.Json; using RequestPlex.Api; using RequestPlex.Core; +using RequestPlex.Store; using RequestPlex.UI.Models; namespace RequestPlex.UI.Modules @@ -32,6 +33,7 @@ namespace RequestPlex.UI.Modules if (settings != null) { model.Port = settings.Port; + model.PlexAuthToken = settings.PlexAuthToken; } return View["/Admin/Settings", model]; @@ -39,16 +41,10 @@ namespace RequestPlex.UI.Modules Post["admin/"] = _ => { - var portString = (string)Request.Form.portNumber; - int port; - - if (!int.TryParse(portString, out port)) - { - return Context.GetRedirect("~/admin?error=true"); - } + var model = this.Bind(); var s = new SettingsService(); - s.SaveSettings(port); + s.SaveSettings(model); return Context.GetRedirect("~/admin"); @@ -64,12 +60,36 @@ namespace RequestPlex.UI.Modules } var plex = new PlexApi(); - plex.GetToken(user.username, user.password); + var model = plex.GetToken(user.username, user.password); + var s = new SettingsService(); + var oldSettings = s.GetSettings(); + if (oldSettings != null) + { + oldSettings.PlexAuthToken = model.user.authentication_token; + s.SaveSettings(oldSettings); + } + else + { + var newModel = new SettingsModel + { + PlexAuthToken = model.user.authentication_token + }; + s.SaveSettings(newModel); + } + return Context.GetRedirect("~/admin"); }; + Get["admin/getusers"] = _ => + { + var api = new PlexApi(); + + + return View["/Admin/Settings"]; + }; + } } } \ No newline at end of file diff --git a/RequestPlex.UI/Views/Admin/Settings.cshtml b/RequestPlex.UI/Views/Admin/Settings.cshtml index c4f586161..df14a4ed9 100644 --- a/RequestPlex.UI/Views/Admin/Settings.cshtml +++ b/RequestPlex.UI/Views/Admin/Settings.cshtml @@ -1,6 +1,7 @@ @Html.Partial("/Admin/_Sidebar") @{ int port; + var authToken = string.Empty; if (Model.Port == null) { port = 3579; @@ -10,10 +11,14 @@ port = Model.Port; } - //if (string.IsNullOrEmpty(Model.PlexAuthToken)) - //{ - // Model.PlexAuthToken = string.Empty; - //} + if (Model.PlexAuthToken == null) + { + authToken = string.Empty; + } + else + { + authToken = Model.PlexAuthToken; + } }
@@ -22,24 +27,24 @@
- +
- +
- +
- +
@@ -48,8 +53,28 @@
+
+ +
+ +
+
+ Current users that are allowed to authenticate: + + +
+
+ +
+
+ +
+
+
+
- Please note, you will have to restart after changing these settings. + Please note, you will have to restart after changing these settings.
@@ -73,6 +98,12 @@ \ No newline at end of file