Added User authentication

This commit is contained in:
LukePulverenti Luke Pulverenti luke pulverenti 2012-09-06 14:38:29 -04:00
parent a529f07869
commit 3538789e46
13 changed files with 168 additions and 17 deletions

View File

@ -292,7 +292,8 @@ namespace MediaBrowser.Api
{ {
Id = user.Id, Id = user.Id,
Name = user.Name, Name = user.Name,
HasImage = !string.IsNullOrEmpty(user.PrimaryImagePath) HasImage = !string.IsNullOrEmpty(user.PrimaryImagePath),
HasPassword = !string.IsNullOrEmpty(user.Password)
}; };
} }
} }

View File

@ -0,0 +1,28 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using MediaBrowser.Common.Net.Handlers;
using MediaBrowser.Controller;
using MediaBrowser.Model.DTO;
using MediaBrowser.Model.Entities;
namespace MediaBrowser.Api.HttpHandlers
{
class UserAuthenticationHandler : BaseSerializationHandler<AuthenticationResult>
{
protected override async Task<AuthenticationResult> GetObjectToSerialize()
{
Guid userId = Guid.Parse(QueryString["userid"]);
User user = Kernel.Instance.Users.First(u => u.Id == userId);
string password = await GetFormValue("password").ConfigureAwait(false);
AuthenticationResult result = new AuthenticationResult()
{
Success = true
};
return result;
}
}
}

View File

@ -73,6 +73,7 @@
<Compile Include="HttpHandlers\ServerConfigurationHandler.cs" /> <Compile Include="HttpHandlers\ServerConfigurationHandler.cs" />
<Compile Include="HttpHandlers\StudioHandler.cs" /> <Compile Include="HttpHandlers\StudioHandler.cs" />
<Compile Include="HttpHandlers\StudiosHandler.cs" /> <Compile Include="HttpHandlers\StudiosHandler.cs" />
<Compile Include="HttpHandlers\UserAuthenticationHandler.cs" />
<Compile Include="HttpHandlers\UsersHandler.cs" /> <Compile Include="HttpHandlers\UsersHandler.cs" />
<Compile Include="HttpHandlers\VideoHandler.cs" /> <Compile Include="HttpHandlers\VideoHandler.cs" />
<Compile Include="HttpHandlers\WeatherHandler.cs" /> <Compile Include="HttpHandlers\WeatherHandler.cs" />

View File

@ -117,6 +117,10 @@ namespace MediaBrowser.Api
{ {
return new PluginAssemblyHandler(); return new PluginAssemblyHandler();
} }
else if (IsUrlMatch("/api/UserAuthentication", localPath))
{
return new UserAuthenticationHandler();
}
return null; return null;
} }

View File

@ -13,18 +13,22 @@ namespace MediaBrowser.ApiInteraction
{ {
return Serializer.Deserialize<T>(stream); return Serializer.Deserialize<T>(stream);
} }
if (format == ApiInteraction.SerializationFormats.Jsv) else if (format == ApiInteraction.SerializationFormats.Jsv)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
else if (format == ApiInteraction.SerializationFormats.Json)
using (StreamReader streamReader = new StreamReader(stream))
{ {
using (JsonReader jsonReader = new JsonTextReader(streamReader)) using (StreamReader streamReader = new StreamReader(stream))
{ {
return JsonSerializer.Create(new JsonSerializerSettings()).Deserialize<T>(jsonReader); using (JsonReader jsonReader = new JsonTextReader(streamReader))
{
return JsonSerializer.Create(new JsonSerializerSettings()).Deserialize<T>(jsonReader);
}
} }
} }
throw new NotImplementedException();
} }
public static object DeserializeFromStream(Stream stream, SerializationFormats format, Type type) public static object DeserializeFromStream(Stream stream, SerializationFormats format, Type type)
@ -33,18 +37,22 @@ namespace MediaBrowser.ApiInteraction
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
if (format == ApiInteraction.SerializationFormats.Jsv) else if (format == ApiInteraction.SerializationFormats.Jsv)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
else if (format == ApiInteraction.SerializationFormats.Json)
using (StreamReader streamReader = new StreamReader(stream))
{ {
using (JsonReader jsonReader = new JsonTextReader(streamReader)) using (StreamReader streamReader = new StreamReader(stream))
{ {
return JsonSerializer.Create(new JsonSerializerSettings()).Deserialize(jsonReader, type); using (JsonReader jsonReader = new JsonTextReader(streamReader))
{
return JsonSerializer.Create(new JsonSerializerSettings()).Deserialize(jsonReader, type);
}
} }
} }
throw new NotImplementedException();
} }
public static void Configure() public static void Configure()

View File

@ -7,6 +7,7 @@ using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.DTO; using MediaBrowser.Model.DTO;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Weather; using MediaBrowser.Model.Weather;
using System.Text;
namespace MediaBrowser.ApiInteraction namespace MediaBrowser.ApiInteraction
{ {
@ -648,6 +649,24 @@ namespace MediaBrowser.ApiInteraction
} }
} }
/// <summary>
/// Authenticates a user and returns the result
/// </summary>
public async Task<AuthenticationResult> AuthenticateUser(Guid userId, string password)
{
string url = ApiUrl + "/UserAuthentication?userId=" + userId;
url += "&dataformat=" + SerializationFormat.ToString();
HttpContent content = new StringContent("password=" + password, Encoding.UTF8, "application/x-www-form-urlencoded");
HttpResponseMessage msg = await HttpClient.PostAsync(url, content).ConfigureAwait(false);
using (Stream stream = await msg.Content.ReadAsStreamAsync().ConfigureAwait(false))
{
return DeserializeFromStream<AuthenticationResult>(stream);
}
}
/// <summary> /// <summary>
/// This is a helper around getting a stream from the server that contains serialized data /// This is a helper around getting a stream from the server that contains serialized data
/// </summary> /// </summary>
@ -663,11 +682,11 @@ namespace MediaBrowser.ApiInteraction
{ {
if (url.IndexOf('?') == -1) if (url.IndexOf('?') == -1)
{ {
url += "?dataformat=" + serializationFormat.ToString().ToLower(); url += "?dataformat=" + serializationFormat.ToString();
} }
else else
{ {
url += "&dataformat=" + serializationFormat.ToString().ToLower(); url += "&dataformat=" + serializationFormat.ToString();
} }
return GetStreamAsync(url); return GetStreamAsync(url);

View File

@ -13,12 +13,16 @@ namespace MediaBrowser.ApiInteraction
{ {
return Serializer.Deserialize<T>(stream); return Serializer.Deserialize<T>(stream);
} }
if (format == ApiInteraction.SerializationFormats.Jsv) else if (format == ApiInteraction.SerializationFormats.Jsv)
{ {
return TypeSerializer.DeserializeFromStream<T>(stream); return TypeSerializer.DeserializeFromStream<T>(stream);
} }
else if (format == ApiInteraction.SerializationFormats.Json)
{
return JsonSerializer.DeserializeFromStream<T>(stream);
}
return JsonSerializer.DeserializeFromStream<T>(stream); throw new NotImplementedException();
} }
public static object DeserializeFromStream(Stream stream, SerializationFormats format, Type type) public static object DeserializeFromStream(Stream stream, SerializationFormats format, Type type)
@ -27,12 +31,16 @@ namespace MediaBrowser.ApiInteraction
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
if (format == ApiInteraction.SerializationFormats.Jsv) else if (format == ApiInteraction.SerializationFormats.Jsv)
{ {
return TypeSerializer.DeserializeFromStream(type, stream); return TypeSerializer.DeserializeFromStream(type, stream);
} }
else if (format == ApiInteraction.SerializationFormats.Json)
{
return JsonSerializer.DeserializeFromStream(type, stream);
}
return JsonSerializer.DeserializeFromStream(type, stream); throw new NotImplementedException();
} }
public static void Configure() public static void Configure()

View File

@ -65,6 +65,7 @@
</Reference> </Reference>
<Reference Include="System.Runtime.Remoting" /> <Reference Include="System.Runtime.Remoting" />
<Reference Include="System.Runtime.Serialization" /> <Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Web" />
<Reference Include="System.Windows.Interactivity"> <Reference Include="System.Windows.Interactivity">
<HintPath>..\packages\MahApps.Metro.0.9.0.0\lib\net40\System.Windows.Interactivity.dll</HintPath> <HintPath>..\packages\MahApps.Metro.0.9.0.0\lib\net40\System.Windows.Interactivity.dll</HintPath>
</Reference> </Reference>

View File

@ -1,11 +1,14 @@
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Web;
using MediaBrowser.Common.Logging; using MediaBrowser.Common.Logging;
namespace MediaBrowser.Common.Net.Handlers namespace MediaBrowser.Common.Net.Handlers
@ -374,5 +377,65 @@ namespace MediaBrowser.Common.Net.Handlers
return StatusCode == 200 || StatusCode == 206; return StatusCode == 200 || StatusCode == 206;
} }
} }
private Hashtable _FormValues = null;
/// <summary>
/// Gets a value from form POST data
/// </summary>
protected async Task<string> GetFormValue(string name)
{
if (_FormValues == null)
{
_FormValues = await GetFormValues(HttpListenerContext.Request).ConfigureAwait(false);
}
if (_FormValues.ContainsKey(name))
{
return _FormValues[name].ToString();
}
return null;
}
/// <summary>
/// Extracts form POST data from a request
/// </summary>
private async Task<Hashtable> GetFormValues(HttpListenerRequest request)
{
Hashtable formVars = new Hashtable();
if (request.HasEntityBody)
{
if (request.ContentType.Equals("application/x-www-form-urlencoded", StringComparison.OrdinalIgnoreCase))
{
using (Stream requestBody = request.InputStream)
{
using (StreamReader reader = new StreamReader(requestBody, request.ContentEncoding))
{
string s = await reader.ReadToEndAsync().ConfigureAwait(false);
string[] pairs = s.Split('&');
for (int x = 0; x < pairs.Length; x++)
{
string pair = pairs[x];
int index = pair.IndexOf('=');
if (index != -1)
{
string name = pair.Substring(0, index);
string value = pair.Substring(index + 1);
formVars.Add(name, value);
}
}
}
}
}
}
return formVars;
}
} }
} }

View File

@ -0,0 +1,12 @@
using System;
using ProtoBuf;
namespace MediaBrowser.Model.DTO
{
[ProtoContract]
public class AuthenticationResult
{
[ProtoMember(1)]
public bool Success { get; set; }
}
}

View File

@ -14,5 +14,8 @@ namespace MediaBrowser.Model.DTO
[ProtoMember(3)] [ProtoMember(3)]
public bool HasImage { get; set; } public bool HasImage { get; set; }
[ProtoMember(4)]
public bool HasPassword { get; set; }
} }
} }

View File

@ -3,6 +3,8 @@ namespace MediaBrowser.Model.Entities
{ {
public class User : BaseEntity public class User : BaseEntity
{ {
public string Password { get; set; }
public string MaxParentalRating { get; set; } public string MaxParentalRating { get; set; }
public int RecentItemDays { get; set; } public int RecentItemDays { get; set; }

View File

@ -35,6 +35,7 @@
<Compile Include="Configuration\BaseApplicationConfiguration.cs" /> <Compile Include="Configuration\BaseApplicationConfiguration.cs" />
<Compile Include="Configuration\ServerConfiguration.cs" /> <Compile Include="Configuration\ServerConfiguration.cs" />
<Compile Include="DTO\AudioInfo.cs" /> <Compile Include="DTO\AudioInfo.cs" />
<Compile Include="DTO\AuthenticationResult.cs" />
<Compile Include="DTO\DTOBaseItem.cs" /> <Compile Include="DTO\DTOBaseItem.cs" />
<Compile Include="DTO\DTOUser.cs" /> <Compile Include="DTO\DTOUser.cs" />
<Compile Include="DTO\VideoInfo.cs" /> <Compile Include="DTO\VideoInfo.cs" />