jellyfin-server/MediaBrowser.Api/PinLoginService.cs

203 lines
5.9 KiB
C#
Raw Normal View History

2016-02-04 17:50:24 +00:00
using System;
using System.Collections.Concurrent;
using System.Globalization;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Connect;
using ServiceStack;
namespace MediaBrowser.Api
{
[Route("/Auth/Pin", "POST", Summary = "Creates a pin request")]
public class CreatePinRequest : IReturn<PinCreationResult>
{
2016-02-04 18:03:40 +00:00
[ApiMember(Name = "DeviceId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
2016-02-04 17:50:24 +00:00
public string DeviceId { get; set; }
}
[Route("/Auth/Pin", "GET", Summary = "Gets pin status")]
public class GetPinStatusRequest : IReturn<PinStatusResult>
{
2016-02-04 18:03:40 +00:00
[ApiMember(Name = "DeviceId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
2016-02-04 17:50:24 +00:00
public string DeviceId { get; set; }
2016-02-04 18:03:40 +00:00
[ApiMember(Name = "Pin", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
2016-02-04 17:50:24 +00:00
public string Pin { get; set; }
}
[Route("/Auth/Pin/Exchange", "POST", Summary = "Exchanges a pin")]
public class ExchangePinRequest : IReturn<PinExchangeResult>
{
2016-02-04 18:03:40 +00:00
[ApiMember(Name = "DeviceId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
2016-02-04 17:50:24 +00:00
public string DeviceId { get; set; }
2016-02-04 18:03:40 +00:00
[ApiMember(Name = "Pin", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
2016-02-04 17:50:24 +00:00
public string Pin { get; set; }
}
[Route("/Auth/Pin/Validate", "POST", Summary = "Validates a pin")]
[Authenticated]
public class ValidatePinRequest : IReturnVoid
{
2016-02-04 18:03:40 +00:00
[ApiMember(Name = "Pin", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
2016-02-04 17:50:24 +00:00
public string Pin { get; set; }
}
public class PinLoginService : BaseApiService
{
private readonly ConcurrentDictionary<string, MyPinStatus> _activeRequests = new ConcurrentDictionary<string, MyPinStatus>(StringComparer.OrdinalIgnoreCase);
public object Post(CreatePinRequest request)
{
2016-02-04 18:03:40 +00:00
var pin = GetNewPin();
2016-02-04 17:50:24 +00:00
var value = new MyPinStatus
{
CreationTimeUtc = DateTime.UtcNow,
IsConfirmed = false,
IsExpired = false,
2016-02-04 18:03:40 +00:00
Pin = pin,
DeviceId = request.DeviceId
2016-02-04 17:50:24 +00:00
};
2016-02-04 18:03:40 +00:00
_activeRequests.AddOrUpdate(pin, value, (k, v) => value);
2016-02-04 17:50:24 +00:00
return ToOptimizedResult(new PinCreationResult
{
DeviceId = request.DeviceId,
IsConfirmed = false,
IsExpired = false,
Pin = pin
});
}
public object Get(GetPinStatusRequest request)
{
MyPinStatus status;
2016-02-04 18:03:40 +00:00
if (!_activeRequests.TryGetValue(request.Pin, out status))
2016-02-04 17:50:24 +00:00
{
throw new ResourceNotFoundException();
}
2016-02-04 18:03:40 +00:00
EnsureValid(request.DeviceId, status);
2016-02-04 17:50:24 +00:00
return ToOptimizedResult(new PinStatusResult
{
Pin = status.Pin,
IsConfirmed = status.IsConfirmed,
IsExpired = status.IsExpired
});
}
public object Post(ExchangePinRequest request)
{
MyPinStatus status;
2016-02-04 18:03:40 +00:00
if (!_activeRequests.TryGetValue(request.Pin, out status))
2016-02-04 17:50:24 +00:00
{
throw new ResourceNotFoundException();
}
2016-02-04 18:03:40 +00:00
EnsureValid(request.DeviceId, status);
2016-02-04 17:50:24 +00:00
2016-02-04 18:03:40 +00:00
if (!status.IsConfirmed)
2016-02-04 17:50:24 +00:00
{
throw new ResourceNotFoundException();
}
return ToOptimizedResult(new PinExchangeResult
{
2016-02-04 18:03:40 +00:00
// TODO: Add access token
UserId = status.UserId
2016-02-04 17:50:24 +00:00
});
}
public void Post(ValidatePinRequest request)
{
2016-02-04 18:03:40 +00:00
MyPinStatus status;
if (!_activeRequests.TryGetValue(request.Pin, out status))
{
throw new ResourceNotFoundException();
}
EnsureValid(status);
status.IsConfirmed = true;
status.UserId = AuthorizationContext.GetAuthorizationInfo(Request).UserId;
}
private void EnsureValid(string requestedDeviceId, MyPinStatus status)
{
if (!string.Equals(requestedDeviceId, status.DeviceId, StringComparison.OrdinalIgnoreCase))
{
throw new ResourceNotFoundException();
}
EnsureValid(status);
2016-02-04 17:50:24 +00:00
}
2016-02-04 18:03:40 +00:00
private void EnsureValid(MyPinStatus status)
2016-02-04 17:50:24 +00:00
{
if ((DateTime.UtcNow - status.CreationTimeUtc).TotalMinutes > 10)
{
status.IsExpired = true;
}
2016-02-04 18:03:40 +00:00
if (status.IsExpired)
{
throw new ResourceNotFoundException();
}
2016-02-04 17:50:24 +00:00
}
2016-02-04 18:03:40 +00:00
private string GetNewPin()
2016-02-04 17:50:24 +00:00
{
2016-02-04 18:03:40 +00:00
var pin = GetNewPinInternal();
while (IsPinActive(pin))
{
pin = GetNewPinInternal();
}
return pin;
}
private string GetNewPinInternal()
{
var length = 5;
2016-02-04 17:50:24 +00:00
var pin = string.Empty;
while (pin.Length < length)
{
var digit = new Random().Next(0, 9);
pin += digit.ToString(CultureInfo.InvariantCulture);
}
return pin;
}
2016-02-04 18:03:40 +00:00
private bool IsPinActive(string pin)
2016-02-04 17:50:24 +00:00
{
2016-02-04 18:03:40 +00:00
MyPinStatus status;
if (!_activeRequests.TryGetValue(pin, out status))
{
return true;
}
if (status.IsExpired)
{
return true;
}
return false;
2016-02-04 17:50:24 +00:00
}
public class MyPinStatus : PinStatusResult
{
public DateTime CreationTimeUtc { get; set; }
2016-02-04 18:03:40 +00:00
public string DeviceId { get; set; }
public string UserId { get; set; }
2016-02-04 17:50:24 +00:00
}
}
}