mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-09-06 03:26:07 +02:00
update login
This commit is contained in:
parent
5ca2ae6395
commit
b1e35d248b
3 changed files with 0 additions and 250 deletions
|
@ -80,7 +80,6 @@
|
|||
<Compile Include="FilterService.cs" />
|
||||
<Compile Include="IHasDtoOptions.cs" />
|
||||
<Compile Include="Library\ChapterService.cs" />
|
||||
<Compile Include="PinLoginService.cs" />
|
||||
<Compile Include="Playback\Dash\ManifestBuilder.cs" />
|
||||
<Compile Include="Playback\Dash\MpegDashService.cs" />
|
||||
<Compile Include="Playback\MediaInfoService.cs" />
|
||||
|
|
|
@ -1,243 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Globalization;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Common.Extensions;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.Net;
|
||||
using MediaBrowser.Controller.Session;
|
||||
using MediaBrowser.Model.Connect;
|
||||
using MediaBrowser.Model.Dto;
|
||||
using MediaBrowser.Model.Session;
|
||||
using ServiceStack;
|
||||
|
||||
namespace MediaBrowser.Api
|
||||
{
|
||||
[Route("/Auth/Pin", "POST", Summary = "Creates a pin request")]
|
||||
public class CreatePinRequest : IReturn<PinCreationResult>
|
||||
{
|
||||
[ApiMember(Name = "DeviceId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
|
||||
public string DeviceId { get; set; }
|
||||
[ApiMember(Name = "AppName", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
|
||||
public string AppName { get; set; }
|
||||
}
|
||||
|
||||
[Route("/Auth/Pin", "GET", Summary = "Gets pin status")]
|
||||
public class GetPinStatusRequest : IReturn<PinStatusResult>
|
||||
{
|
||||
[ApiMember(Name = "DeviceId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
|
||||
public string DeviceId { get; set; }
|
||||
[ApiMember(Name = "Pin", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
|
||||
public string Pin { get; set; }
|
||||
}
|
||||
|
||||
[Route("/Auth/Pin/Exchange", "POST", Summary = "Exchanges a pin")]
|
||||
public class ExchangePinRequest : IReturn<PinExchangeResult>
|
||||
{
|
||||
[ApiMember(Name = "DeviceId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
|
||||
public string DeviceId { get; set; }
|
||||
[ApiMember(Name = "Pin", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
|
||||
public string Pin { get; set; }
|
||||
}
|
||||
|
||||
[Route("/Auth/Pin/Validate", "POST", Summary = "Validates a pin")]
|
||||
[Authenticated]
|
||||
public class ValidatePinRequest : IReturn<SessionInfoDto>
|
||||
{
|
||||
[ApiMember(Name = "Pin", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
|
||||
public string Pin { get; set; }
|
||||
}
|
||||
|
||||
public class PinLoginService : BaseApiService
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
public object Post(CreatePinRequest request)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(request.DeviceId))
|
||||
{
|
||||
throw new ArgumentNullException("DeviceId");
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(request.AppName))
|
||||
{
|
||||
throw new ArgumentNullException("AppName");
|
||||
}
|
||||
|
||||
var pin = GetNewPin();
|
||||
|
||||
var value = new MyPinStatus
|
||||
{
|
||||
CreationTimeUtc = DateTime.UtcNow,
|
||||
IsConfirmed = false,
|
||||
IsExpired = false,
|
||||
Pin = pin,
|
||||
DeviceId = request.DeviceId,
|
||||
AppName = request.AppName
|
||||
};
|
||||
|
||||
_activeRequests.AddOrUpdate(pin, value, (k, v) => value);
|
||||
|
||||
return ToOptimizedResult(new PinCreationResult
|
||||
{
|
||||
DeviceId = request.DeviceId,
|
||||
IsConfirmed = false,
|
||||
IsExpired = false,
|
||||
Pin = pin
|
||||
});
|
||||
}
|
||||
|
||||
public object Get(GetPinStatusRequest request)
|
||||
{
|
||||
MyPinStatus status;
|
||||
|
||||
if (!_activeRequests.TryGetValue(request.Pin, out status))
|
||||
{
|
||||
Logger.Debug("Pin {0} not found.", request.Pin);
|
||||
throw new ResourceNotFoundException();
|
||||
}
|
||||
|
||||
EnsureValid(request.DeviceId, status);
|
||||
|
||||
return ToOptimizedResult(new PinStatusResult
|
||||
{
|
||||
Pin = status.Pin,
|
||||
IsConfirmed = status.IsConfirmed,
|
||||
IsExpired = status.IsExpired
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<object> Post(ExchangePinRequest request)
|
||||
{
|
||||
MyPinStatus status;
|
||||
|
||||
if (!_activeRequests.TryGetValue(request.Pin, out status))
|
||||
{
|
||||
Logger.Debug("Pin {0} not found.", request.Pin);
|
||||
throw new ResourceNotFoundException();
|
||||
}
|
||||
|
||||
EnsureValid(request.DeviceId, status);
|
||||
|
||||
if (!status.IsConfirmed)
|
||||
{
|
||||
throw new ResourceNotFoundException();
|
||||
}
|
||||
|
||||
var auth = AuthorizationContext.GetAuthorizationInfo(Request);
|
||||
var user = _userManager.GetUserById(status.UserId);
|
||||
|
||||
var result = await _sessionManager.CreateNewSession(new AuthenticationRequest
|
||||
{
|
||||
App = auth.Client,
|
||||
AppVersion = auth.Version,
|
||||
DeviceId = auth.DeviceId,
|
||||
DeviceName = auth.Device,
|
||||
RemoteEndPoint = Request.RemoteIp,
|
||||
Username = user.Name
|
||||
|
||||
}).ConfigureAwait(false);
|
||||
|
||||
return ToOptimizedResult(result);
|
||||
}
|
||||
|
||||
public object Post(ValidatePinRequest request)
|
||||
{
|
||||
MyPinStatus status;
|
||||
|
||||
if (!_activeRequests.TryGetValue(request.Pin, out status))
|
||||
{
|
||||
throw new ResourceNotFoundException();
|
||||
}
|
||||
|
||||
EnsureValid(status);
|
||||
|
||||
status.IsConfirmed = true;
|
||||
status.UserId = AuthorizationContext.GetAuthorizationInfo(Request).UserId;
|
||||
|
||||
return ToOptimizedResult(new ValidatePinResult
|
||||
{
|
||||
AppName = status.AppName
|
||||
});
|
||||
}
|
||||
|
||||
private void EnsureValid(string requestedDeviceId, MyPinStatus status)
|
||||
{
|
||||
if (!string.Equals(requestedDeviceId, status.DeviceId, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
Logger.Debug("Pin device Id's do not match. requestedDeviceId: {0}, status.DeviceId: {1}", requestedDeviceId, status.DeviceId);
|
||||
throw new ResourceNotFoundException();
|
||||
}
|
||||
|
||||
EnsureValid(status);
|
||||
}
|
||||
|
||||
private void EnsureValid(MyPinStatus status)
|
||||
{
|
||||
if ((DateTime.UtcNow - status.CreationTimeUtc).TotalMinutes > 10)
|
||||
{
|
||||
status.IsExpired = true;
|
||||
}
|
||||
|
||||
if (status.IsExpired)
|
||||
{
|
||||
Logger.Debug("Pin {0} is expired", status.Pin);
|
||||
throw new ResourceNotFoundException();
|
||||
}
|
||||
}
|
||||
|
||||
private string GetNewPin()
|
||||
{
|
||||
var pin = GetNewPinInternal();
|
||||
|
||||
while (IsPinActive(pin))
|
||||
{
|
||||
pin = GetNewPinInternal();
|
||||
}
|
||||
|
||||
return pin;
|
||||
}
|
||||
|
||||
private string GetNewPinInternal()
|
||||
{
|
||||
return new Random().Next(10000, 99999).ToString(CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
private bool IsPinActive(string pin)
|
||||
{
|
||||
MyPinStatus status;
|
||||
|
||||
if (!_activeRequests.TryGetValue(pin, out status))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (status.IsExpired)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public class MyPinStatus : PinStatusResult
|
||||
{
|
||||
public DateTime CreationTimeUtc { get; set; }
|
||||
public string DeviceId { get; set; }
|
||||
public string UserId { get; set; }
|
||||
public string AppName { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
public class ValidatePinResult
|
||||
{
|
||||
public string AppName { get; set; }
|
||||
}
|
||||
}
|
|
@ -278,9 +278,6 @@
|
|||
<Content Include="dashboard-ui\mysyncsettings.html">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="dashboard-ui\pin.html">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="dashboard-ui\robots.txt">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
|
@ -320,9 +317,6 @@
|
|||
<Content Include="dashboard-ui\scripts\autoorganizesmart.js">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="dashboard-ui\scripts\pin.js">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="dashboard-ui\scripts\searchmenu.js">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
|
|
Loading…
Reference in a new issue