more work

This commit is contained in:
Jamie Rees 2016-02-26 20:05:43 +00:00
parent 81c492aa14
commit 6054f0d436
15 changed files with 192 additions and 23 deletions

View file

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

View file

@ -30,6 +30,14 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Dapper, Version=1.40.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapper.1.42\lib\net45\Dapper.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nancy, Version=1.4.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nancy.1.4.3\lib\net40\Nancy.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> <Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath> <HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private> <Private>True</Private>
@ -54,6 +62,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="ApiRequest.cs" /> <Compile Include="ApiRequest.cs" />
<Compile Include="Models\MovieSearch.cs" /> <Compile Include="Models\MovieSearch.cs" />
<Compile Include="PlexApi.cs" />
<Compile Include="TheMovieDbApi.cs" /> <Compile Include="TheMovieDbApi.cs" />
<Compile Include="MovieBase.cs" /> <Compile Include="MovieBase.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />

View file

@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="Dapper" version="1.42" targetFramework="net452" />
<package id="Nancy" version="1.4.3" targetFramework="net452" />
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net452" /> <package id="Newtonsoft.Json" version="7.0.1" targetFramework="net452" />
<package id="RestSharp" version="105.2.3" targetFramework="net452" /> <package id="RestSharp" version="105.2.3" targetFramework="net452" />
<package id="TMDbLib" version="0.9.0.0-alpha" targetFramework="net452" /> <package id="TMDbLib" version="0.9.0.0-alpha" targetFramework="net452" />

View file

@ -10,7 +10,7 @@ namespace RequestPlex.Core
public void SetupDb() public void SetupDb()
{ {
var db = new DbConfiguration(new SqliteFactory()); var db = new DbConfiguration(new SqliteFactory());
db.CreateDatabase(); db.CheckDb();
TableCreation.CreateTables(db.DbConnection()); TableCreation.CreateTables(db.DbConnection());
} }
} }

View file

@ -37,7 +37,7 @@ namespace RequestPlex.Core
var db = new DbConfiguration(new SqliteFactory()); var db = new DbConfiguration(new SqliteFactory());
var repo = new UserRepository<UserModel>(db); var repo = new UserRepository<UserModel>(db);
var users = repo.GetAll(); 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) if (userRecord == null)
{ {

View file

@ -8,5 +8,7 @@ namespace RequestPlex.Store
public class SettingsModel : Entity public class SettingsModel : Entity
{ {
public int Port { get; set; } public int Port { get; set; }
public bool UserAuthentication { get; set; }
public string PlexAuthToken { get; set; }
} }
} }

View file

@ -11,7 +11,9 @@ CREATE TABLE IF NOT EXISTS User
CREATE TABLE IF NOT EXISTS Settings CREATE TABLE IF NOT EXISTS Settings
( (
Id INTEGER PRIMARY KEY AUTOINCREMENT, 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 CREATE TABLE IF NOT EXISTS Requested

View file

@ -17,7 +17,6 @@ $("#tvSearchContent").on("keyup", function (e) {
tvimer = setTimeout(tvSearch(), 400); tvimer = setTimeout(tvSearch(), 400);
}); });
$("#test").click(function (e) { $("#test").click(function (e) {
e.preventDefault(); e.preventDefault();
@ -104,14 +103,3 @@ function buildTvShowContext(result) {
}; };
return context; return context;
} }
function generateNotify(message, type) {
// type = danger, warning, info, successs
$.notify({
// options
message: message
}, {
// settings
type: type
});
}

View file

@ -0,0 +1,13 @@

function generateNotify(message, type) {
// type = danger, warning, info, successs
$.notify({
// options
message: message
}, {
// settings
type: type
});
}

View file

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

View file

@ -1,10 +1,17 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic; using System.Dynamic;
using System.Linq;
using Nancy; using Nancy;
using Nancy.Extensions; using Nancy.Extensions;
using Nancy.ModelBinding;
using Nancy.Security; using Nancy.Security;
using Newtonsoft.Json;
using RequestPlex.Api;
using RequestPlex.Core; using RequestPlex.Core;
using RequestPlex.UI.Models;
namespace RequestPlex.UI.Modules namespace RequestPlex.UI.Modules
{ {
@ -12,12 +19,14 @@ namespace RequestPlex.UI.Modules
{ {
public AdminModule() public AdminModule()
{ {
#if !DEBUG
this.RequiresAuthentication(); this.RequiresAuthentication();
#endif
Get["admin/"] = _ => Get["admin/"] = _ =>
{ {
dynamic model = new ExpandoObject(); dynamic model = new ExpandoObject();
model.Errored = Request.Query.error.HasValue; model.Errored = Request.Query.error.HasValue;
model.Port = null;
var s = new SettingsService(); var s = new SettingsService();
var settings = s.GetSettings(); var settings = s.GetSettings();
if (settings != null) if (settings != null)
@ -42,6 +51,22 @@ namespace RequestPlex.UI.Modules
s.SaveSettings(port); s.SaveSettings(port);
return Context.GetRedirect("~/admin");
};
Post["admin/requestauth"] = _ =>
{
var user = this.Bind<PlexAuth>();
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"); return Context.GetRedirect("~/admin");
}; };

View file

@ -108,6 +108,7 @@
<Content Include="Content\custom.css"> <Content Include="Content\custom.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
<Compile Include="Models\PlexAuth.cs" />
<Compile Include="Modules\AdminModule.cs" /> <Compile Include="Modules\AdminModule.cs" />
<Compile Include="Modules\IndexModule.cs" /> <Compile Include="Modules\IndexModule.cs" />
<Compile Include="Modules\LoginModule.cs" /> <Compile Include="Modules\LoginModule.cs" />
@ -136,6 +137,9 @@
<Content Include="Content\jquery-2.2.1.min.js"> <Content Include="Content\jquery-2.2.1.min.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
<Content Include="Content\site.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="icon.png" /> <Content Include="icon.png" />
<None Include="app.config" /> <None Include="app.config" />
<Content Include="packages.config" /> <Content Include="packages.config" />
@ -193,7 +197,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Auth\" /> <Folder Include="Auth\" />
<Folder Include="Models\" />
<Folder Include="Views\Requests\" /> <Folder Include="Views\Requests\" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View file

@ -1,9 +1,22 @@
@Html.Partial("/Admin/_Sidebar") @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;
//}
} }
<div class="col-sm-8"> <div class="col-sm-8">
<form class="form-horizontal" method="POST"> <form class="form-horizontal" method="POST" id="mainForm">
<fieldset> <fieldset>
<legend>Request Plex Settings</legend> <legend>Request Plex Settings</legend>
<div class="form-group"> <div class="form-group">
@ -12,6 +25,32 @@
<input type="text" class="form-control" id="portNumber" name="portNumber" placeholder="Port Number" value="@port"> <input type="text" class="form-control" id="portNumber" name="portNumber" placeholder="Port Number" value="@port">
</div> </div>
</div> </div>
<div class="form-group">
<label for="authToken" class="col-lg-2 control-label">Plex Authorization Token</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="authToken" name="authToken" placeholder="Plex Auth Token" value="@*@Model.PlexAuthToken*@">
</div>
</div>
<div class="form-group">
<label for="userpass" class="col-lg-2 control-label">Username and Password</label>
<div class="col-lg-4">
<input type="text" class="form-control" id="username" name="username" placeholder="Username">
</div>
<div class="col-lg-4 col-lg-push-1">
<input type="text" class="form-control" id="password" name="password" placeholder="Password">
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button id="requestToken" class="btn btn-primary">Request Token</button>
</div>
</div>
<div>
<small>Please note, you will have to restart after changing these settings.</small>
</div>
<div class="form-group"> <div class="form-group">
<div class="col-lg-10 col-lg-offset-2"> <div class="col-lg-10 col-lg-offset-2">
<button type="reset" class="btn btn-default">Cancel</button> <button type="reset" class="btn btn-default">Cancel</button>
@ -29,4 +68,33 @@
<button type="button" class="close" data-dismiss="alert"><i class="fa fa-times"></i></button> <button type="button" class="close" data-dismiss="alert"><i class="fa fa-times"></i></button>
Please enter in a correct port number Please enter in a correct port number
</div> </div>
} }
<script>
$(function () {
$('#requestToken').click(function (e) {
var $form = $("#mainForm");
$.ajax({
type: $form.prop("method"),
url: "admin/requestauth",
data: $form.serialize(),
dataType: "json",
success: function (response) {
console.log(response);
if (response.Result === true) {
generateNotify("Success!", "success");
$('#authToken').val(response.authToken);
} else {
generateNotify(response.Message, "warning");
}
},
error: function (e) {
console.log(e);
generateNotify("Something went wrong!", "danger");
}
});
});
});
</script>

View file

@ -4,6 +4,5 @@
<a class="list-group-item" href="/couchpotato">CouchPotato Settings</a> <a class="list-group-item" href="/couchpotato">CouchPotato Settings</a>
<a class="list-group-item" href="/sonarr">Sonarr Settings</a> <a class="list-group-item" href="/sonarr">Sonarr Settings</a>
<a class="list-group-item" href="/sickbeard">Sickbeard Settings</a> <a class="list-group-item" href="/sickbeard">Sickbeard Settings</a>
<a class="list-group-item" href="/userauth">Authentication</a>
</div> </div>
</div> </div>

View file

@ -11,6 +11,7 @@
<script src="/Content/handlebars.js"></script> <script src="/Content/handlebars.js"></script>
<script src="/Content/bootstrap.min.js"></script> <script src="/Content/bootstrap.min.js"></script>
<script src="/Content/bootstrap-notify.min.js"></script> <script src="/Content/bootstrap-notify.min.js"></script>
<script src="/Content/site.js"></script>
<nav class="navbar navbar-default"> <nav class="navbar navbar-default">
<div class="container-fluid"> <div class="container-fluid">