Files
jellyfin-jellyfin-1/MediaBrowser.Api/PinLoginService.cs

244 lines
7.7 KiB
C#
Raw Normal View History

2016-02-04 12:50:24 -05:00
using System;
using System.Collections.Concurrent;
using System.Globalization;
2016-02-21 01:25:25 -05:00
using System.Threading.Tasks;
2016-02-04 12:50:24 -05:00
using MediaBrowser.Common.Extensions;
2016-02-21 01:25:25 -05:00
using MediaBrowser.Controller.Library;
2016-02-04 12:50:24 -05:00
using MediaBrowser.Controller.Net;
2016-02-21 01:25:25 -05:00
using MediaBrowser.Controller.Session;
2016-02-04 12:50:24 -05:00
using MediaBrowser.Model.Connect;
2016-02-21 01:25:25 -05:00
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Session;
2016-02-04 12:50:24 -05:00
using ServiceStack;
namespace MediaBrowser.Api
{
[Route("/Auth/Pin", "POST", Summary = "Creates a pin request")]
public class CreatePinRequest : IReturn<PinCreationResult>
{
2016-02-04 13:03:40 -05:00
[ApiMember(Name = "DeviceId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
2016-02-04 12:50:24 -05:00
public string DeviceId { get; set; }
2016-02-21 01:25:25 -05:00
[ApiMember(Name = "AppName", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string AppName { get; set; }
2016-02-04 12:50:24 -05:00
}
[Route("/Auth/Pin", "GET", Summary = "Gets pin status")]
public class GetPinStatusRequest : IReturn<PinStatusResult>
{
2016-02-04 13:03:40 -05:00
[ApiMember(Name = "DeviceId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
2016-02-04 12:50:24 -05:00
public string DeviceId { get; set; }
2016-02-04 13:03:40 -05:00
[ApiMember(Name = "Pin", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
2016-02-04 12:50:24 -05:00
public string Pin { get; set; }
}
[Route("/Auth/Pin/Exchange", "POST", Summary = "Exchanges a pin")]
public class ExchangePinRequest : IReturn<PinExchangeResult>
{
2016-02-04 13:03:40 -05:00
[ApiMember(Name = "DeviceId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
2016-02-04 12:50:24 -05:00
public string DeviceId { get; set; }
2016-02-04 13:03:40 -05:00
[ApiMember(Name = "Pin", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
2016-02-04 12:50:24 -05:00
public string Pin { get; set; }
}
[Route("/Auth/Pin/Validate", "POST", Summary = "Validates a pin")]
[Authenticated]
2016-02-21 01:25:25 -05:00
public class ValidatePinRequest : IReturn<SessionInfoDto>
2016-02-04 12:50:24 -05:00
{
2016-02-04 13:03:40 -05:00
[ApiMember(Name = "Pin", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
2016-02-04 12:50:24 -05:00
public string Pin { get; set; }
}
public class PinLoginService : BaseApiService
{
2016-02-21 01:25:25 -05:00
private static readonly ConcurrentDictionary<string, MyPinStatus> _activeRequests = new ConcurrentDictionary<string, MyPinStatus>(StringComparer.OrdinalIgnoreCase);
private readonly ISessionManager _sessionManager;
private readonly IUserManager _userManager;
public PinLoginService(ISessionManager sessionManager, IUserManager userManager)
{
_sessionManager = sessionManager;
_userManager = userManager;
}
2016-02-04 12:50:24 -05:00
public object Post(CreatePinRequest request)
{
2016-02-21 01:25:25 -05:00
if (string.IsNullOrWhiteSpace(request.DeviceId))
{
throw new ArgumentNullException("DeviceId");
}
if (string.IsNullOrWhiteSpace(request.AppName))
{
throw new ArgumentNullException("AppName");
}
2016-02-04 13:03:40 -05:00
var pin = GetNewPin();
2016-02-04 12:50:24 -05:00
var value = new MyPinStatus
{
CreationTimeUtc = DateTime.UtcNow,
IsConfirmed = false,
IsExpired = false,
2016-02-04 13:03:40 -05:00
Pin = pin,
2016-02-21 01:25:25 -05:00
DeviceId = request.DeviceId,
AppName = request.AppName
2016-02-04 12:50:24 -05:00
};
2016-02-04 13:03:40 -05:00
_activeRequests.AddOrUpdate(pin, value, (k, v) => value);
2016-02-04 12:50:24 -05:00
return ToOptimizedResult(new PinCreationResult
{
DeviceId = request.DeviceId,
IsConfirmed = false,
IsExpired = false,
Pin = pin
});
}
public object Get(GetPinStatusRequest request)
{
MyPinStatus status;
2016-02-04 13:03:40 -05:00
if (!_activeRequests.TryGetValue(request.Pin, out status))
2016-02-04 12:50:24 -05:00
{
2016-02-21 01:25:25 -05:00
Logger.Debug("Pin {0} not found.", request.Pin);
2016-02-04 12:50:24 -05:00
throw new ResourceNotFoundException();
}
2016-02-04 13:03:40 -05:00
EnsureValid(request.DeviceId, status);
2016-02-04 12:50:24 -05:00
return ToOptimizedResult(new PinStatusResult
{
Pin = status.Pin,
IsConfirmed = status.IsConfirmed,
IsExpired = status.IsExpired
});
}
2016-02-21 01:25:25 -05:00
public async Task<object> Post(ExchangePinRequest request)
2016-02-04 12:50:24 -05:00
{
MyPinStatus status;
2016-02-04 13:03:40 -05:00
if (!_activeRequests.TryGetValue(request.Pin, out status))
2016-02-04 12:50:24 -05:00
{
2016-02-21 01:25:25 -05:00
Logger.Debug("Pin {0} not found.", request.Pin);
2016-02-04 12:50:24 -05:00
throw new ResourceNotFoundException();
}
2016-02-04 13:03:40 -05:00
EnsureValid(request.DeviceId, status);
2016-02-04 12:50:24 -05:00
2016-02-04 13:03:40 -05:00
if (!status.IsConfirmed)
2016-02-04 12:50:24 -05:00
{
throw new ResourceNotFoundException();
}
2016-02-21 01:25:25 -05:00
var auth = AuthorizationContext.GetAuthorizationInfo(Request);
var user = _userManager.GetUserById(status.UserId);
var result = await _sessionManager.CreateNewSession(new AuthenticationRequest
2016-02-04 12:50:24 -05:00
{
2016-02-21 01:25:25 -05:00
App = auth.Client,
AppVersion = auth.Version,
DeviceId = auth.DeviceId,
DeviceName = auth.Device,
RemoteEndPoint = Request.RemoteIp,
Username = user.Name
}).ConfigureAwait(false);
return ToOptimizedResult(result);
2016-02-04 12:50:24 -05:00
}
2016-02-21 01:25:25 -05:00
public object Post(ValidatePinRequest request)
2016-02-04 12:50:24 -05:00
{
2016-02-04 13:03:40 -05:00
MyPinStatus status;
if (!_activeRequests.TryGetValue(request.Pin, out status))
{
throw new ResourceNotFoundException();
}
EnsureValid(status);
status.IsConfirmed = true;
status.UserId = AuthorizationContext.GetAuthorizationInfo(Request).UserId;
2016-02-21 01:25:25 -05:00
return ToOptimizedResult(new ValidatePinResult
{
AppName = status.AppName
});
2016-02-04 13:03:40 -05:00
}
private void EnsureValid(string requestedDeviceId, MyPinStatus status)
{
if (!string.Equals(requestedDeviceId, status.DeviceId, StringComparison.OrdinalIgnoreCase))
{
2016-02-21 01:25:25 -05:00
Logger.Debug("Pin device Id's do not match. requestedDeviceId: {0}, status.DeviceId: {1}", requestedDeviceId, status.DeviceId);
2016-02-04 13:03:40 -05:00
throw new ResourceNotFoundException();
}
EnsureValid(status);
2016-02-04 12:50:24 -05:00
}
2016-02-04 13:03:40 -05:00
private void EnsureValid(MyPinStatus status)
2016-02-04 12:50:24 -05:00
{
if ((DateTime.UtcNow - status.CreationTimeUtc).TotalMinutes > 10)
{
status.IsExpired = true;
}
2016-02-04 13:03:40 -05:00
if (status.IsExpired)
{
2016-02-21 01:25:25 -05:00
Logger.Debug("Pin {0} is expired", status.Pin);
2016-02-04 13:03:40 -05:00
throw new ResourceNotFoundException();
}
2016-02-04 12:50:24 -05:00
}
2016-02-04 13:03:40 -05:00
private string GetNewPin()
2016-02-04 12:50:24 -05:00
{
2016-02-04 13:03:40 -05:00
var pin = GetNewPinInternal();
while (IsPinActive(pin))
{
pin = GetNewPinInternal();
}
return pin;
}
private string GetNewPinInternal()
{
2016-02-21 01:25:25 -05:00
return new Random().Next(10000, 99999).ToString(CultureInfo.InvariantCulture);
2016-02-04 12:50:24 -05:00
}
2016-02-04 13:03:40 -05:00
private bool IsPinActive(string pin)
2016-02-04 12:50:24 -05:00
{
2016-02-04 13:03:40 -05:00
MyPinStatus status;
if (!_activeRequests.TryGetValue(pin, out status))
{
2016-02-21 01:25:25 -05:00
return false;
2016-02-04 13:03:40 -05:00
}
if (status.IsExpired)
{
2016-02-21 01:25:25 -05:00
return false;
2016-02-04 13:03:40 -05:00
}
2016-02-21 01:25:25 -05:00
return true;
2016-02-04 12:50:24 -05:00
}
public class MyPinStatus : PinStatusResult
{
public DateTime CreationTimeUtc { get; set; }
2016-02-04 13:03:40 -05:00
public string DeviceId { get; set; }
public string UserId { get; set; }
2016-02-21 01:25:25 -05:00
public string AppName { get; set; }
2016-02-04 12:50:24 -05:00
}
}
2016-02-21 01:25:25 -05:00
public class ValidatePinResult
{
public string AppName { get; set; }
}
2016-02-04 12:50:24 -05:00
}