API cleanup

pull/2888/head
ConfusedPolarBear 4 years ago
parent 4be476ec53
commit 329980c727

@ -75,18 +75,15 @@ namespace Emby.Server.Implementations.QuickConnect
{ {
if (State != QuickConnectState.Active) if (State != QuickConnectState.Active)
{ {
throw new InvalidOperationException("Quick connect is not active on this server"); throw new ArgumentException("Quick connect is not active on this server");
} }
} }
/// <inheritdoc/> /// <inheritdoc/>
public QuickConnectResult Activate() public void Activate()
{ {
SetEnabled(QuickConnectState.Active);
DateActivated = DateTime.Now; DateActivated = DateTime.Now;
SetEnabled(QuickConnectState.Active);
return new QuickConnectResult();
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -149,19 +146,6 @@ namespace Emby.Server.Implementations.QuickConnect
return result; return result;
} }
public List<QuickConnectResultDto> GetCurrentRequests()
{
return GetCurrentRequestsInternal().Select(x => (QuickConnectResultDto)x).ToList();
}
/// <inheritdoc/>
public List<QuickConnectResult> GetCurrentRequestsInternal()
{
ExpireRequests();
AssertActive();
return _currentRequests.Values.ToList();
}
/// <inheritdoc/> /// <inheritdoc/>
public string GenerateCode() public string GenerateCode()
{ {
@ -215,7 +199,7 @@ namespace Emby.Server.Implementations.QuickConnect
UserId = auth.UserId UserId = auth.UserId
}); });
_logger.LogInformation("Allowing device {0} to login as user {1} with quick connect code {2}", result.FriendlyName, auth.User.Name, result.Code); _logger.LogInformation("Allowing device {0} to login as user {1} with quick connect code {2}", result.FriendlyName, auth.User.Username, result.Code);
return true; return true;
} }
@ -269,11 +253,8 @@ namespace Emby.Server.Implementations.QuickConnect
return Hex.Encode(bytes); return Hex.Encode(bytes);
} }
/// <summary> /// <inheritdoc/>
/// Expire quick connect requests that are over the time limit. If <paramref name="expireAll"/> is true, all requests are unconditionally expired. public void ExpireRequests(bool expireAll = false)
/// </summary>
/// <param name="expireAll">If true, all requests will be expired.</param>
private void ExpireRequests(bool expireAll = false)
{ {
// Check if quick connect should be deactivated // Check if quick connect should be deactivated
if (State == QuickConnectState.Active && DateTime.Now > DateActivated.AddMinutes(Timeout) && !expireAll) if (State == QuickConnectState.Active && DateTime.Now > DateActivated.AddMinutes(Timeout) && !expireAll)
@ -309,9 +290,7 @@ namespace Emby.Server.Implementations.QuickConnect
private void ReloadConfiguration() private void ReloadConfiguration()
{ {
var available = _config.Configuration.QuickConnectAvailable; State = _config.Configuration.QuickConnectAvailable ? QuickConnectState.Available : QuickConnectState.Unavailable;
State = available ? QuickConnectState.Available : QuickConnectState.Unavailable;
} }
} }
} }

@ -1,5 +1,4 @@
using System; using System;
using System.Collections.Generic;
using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net; using MediaBrowser.Controller.Net;
@ -24,18 +23,12 @@ namespace MediaBrowser.Api.QuickConnect
public string Secret { get; set; } public string Secret { get; set; }
} }
[Route("/QuickConnect/List", "GET", Summary = "Lists all quick connect requests")]
[Authenticated]
public class QuickConnectList : IReturn<List<QuickConnectResultDto>>
{
}
[Route("/QuickConnect/Authorize", "POST", Summary = "Authorizes a pending quick connect request")] [Route("/QuickConnect/Authorize", "POST", Summary = "Authorizes a pending quick connect request")]
[Authenticated] [Authenticated]
public class Authorize : IReturn<QuickConnectResultDto> public class Authorize : IReturn<bool>
{ {
[ApiMember(Name = "Lookup", Description = "Quick connect public lookup", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")] [ApiMember(Name = "Code", Description = "Quick connect identifying code", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
public string Lookup { get; set; } public string Code { get; set; }
} }
[Route("/QuickConnect/Deauthorize", "POST", Summary = "Deletes all quick connect authorization tokens for the current user")] [Route("/QuickConnect/Deauthorize", "POST", Summary = "Deletes all quick connect authorization tokens for the current user")]
@ -62,8 +55,9 @@ namespace MediaBrowser.Api.QuickConnect
[Route("/QuickConnect/Activate", "POST", Summary = "Temporarily activates quick connect for the time period defined in the server configuration")] [Route("/QuickConnect/Activate", "POST", Summary = "Temporarily activates quick connect for the time period defined in the server configuration")]
[Authenticated] [Authenticated]
public class Activate : IReturn<QuickConnectState> public class Activate : IReturn<bool>
{ {
} }
public class QuickConnectService : BaseApiService public class QuickConnectService : BaseApiService
@ -96,18 +90,9 @@ namespace MediaBrowser.Api.QuickConnect
return _quickConnect.CheckRequestStatus(request.Secret); return _quickConnect.CheckRequestStatus(request.Secret);
} }
public object Get(QuickConnectList request)
{
if(_quickConnect.State != QuickConnectState.Active)
{
return Array.Empty<QuickConnectResultDto>();
}
return _quickConnect.GetCurrentRequests();
}
public object Get(QuickConnectStatus request) public object Get(QuickConnectStatus request)
{ {
_quickConnect.ExpireRequests();
return _quickConnect.State; return _quickConnect.State;
} }
@ -120,55 +105,27 @@ namespace MediaBrowser.Api.QuickConnect
public object Post(Authorize request) public object Post(Authorize request)
{ {
bool result = _quickConnect.AuthorizeRequest(Request, request.Lookup); return _quickConnect.AuthorizeRequest(Request, request.Code);
Logger.LogInformation("Result of authorizing quick connect {0}: {1}", request.Lookup[..10], result);
return result;
} }
public object Post(Activate request) public object Post(Activate request)
{ {
string name = _authContext.GetAuthorizationInfo(Request).User.Name;
if(_quickConnect.State == QuickConnectState.Unavailable) if(_quickConnect.State == QuickConnectState.Unavailable)
{ {
return new QuickConnectResult() return false;
{
Error = "Quick connect is not enabled on this server"
};
} }
else if(_quickConnect.State == QuickConnectState.Available) string name = _authContext.GetAuthorizationInfo(Request).User.Username;
{
var result = _quickConnect.Activate();
if (string.IsNullOrEmpty(result.Error))
{
Logger.LogInformation("{name} temporarily activated quick connect", name);
}
return result; Logger.LogInformation("{name} temporarily activated quick connect", name);
} _quickConnect.Activate();
else if(_quickConnect.State == QuickConnectState.Active) return true;
{
return new QuickConnectResult()
{
Error = ""
};
}
return new QuickConnectResult()
{
Error = "Unknown current state"
};
} }
public object Post(Available request) public object Post(Available request)
{ {
_quickConnect.SetEnabled(request.Status); _quickConnect.SetEnabled(request.Status);
return _quickConnect.State; return _quickConnect.State;
} }
} }

@ -38,8 +38,7 @@ namespace MediaBrowser.Controller.QuickConnect
/// <summary> /// <summary>
/// Temporarily activates quick connect for a short amount of time. /// Temporarily activates quick connect for a short amount of time.
/// </summary> /// </summary>
/// <returns>A quick connect result object indicating success.</returns> void Activate();
QuickConnectResult Activate();
/// <summary> /// <summary>
/// Changes the status of quick connect. /// Changes the status of quick connect.
@ -61,26 +60,20 @@ namespace MediaBrowser.Controller.QuickConnect
/// <returns>Quick connect result.</returns> /// <returns>Quick connect result.</returns>
QuickConnectResult CheckRequestStatus(string secret); QuickConnectResult CheckRequestStatus(string secret);
/// <summary>
/// Returns all current quick connect requests as DTOs. Does not include sensitive information.
/// </summary>
/// <returns>List of all quick connect results.</returns>
List<QuickConnectResultDto> GetCurrentRequests();
/// <summary>
/// Returns all current quick connect requests (including sensitive information).
/// </summary>
/// <returns>List of all quick connect results.</returns>
List<QuickConnectResult> GetCurrentRequestsInternal();
/// <summary> /// <summary>
/// Authorizes a quick connect request to connect as the calling user. /// Authorizes a quick connect request to connect as the calling user.
/// </summary> /// </summary>
/// <param name="request">HTTP request object.</param> /// <param name="request">HTTP request object.</param>
/// <param name="lookup">Identifying code for the request..</param> /// <param name="code">Identifying code for the request.</param>
/// <returns>A boolean indicating if the authorization completed successfully.</returns> /// <returns>A boolean indicating if the authorization completed successfully.</returns>
bool AuthorizeRequest(IRequest request, string code); bool AuthorizeRequest(IRequest request, string code);
/// <summary>
/// Expire quick connect requests that are over the time limit. If <paramref name="expireAll"/> is true, all requests are unconditionally expired.
/// </summary>
/// <param name="expireAll">If true, all requests will be expired.</param>
public void ExpireRequests(bool expireAll = false);
/// <summary> /// <summary>
/// Deletes all quick connect access tokens for the provided user. /// Deletes all quick connect access tokens for the provided user.
/// </summary> /// </summary>

@ -1,41 +0,0 @@
using System;
namespace MediaBrowser.Model.QuickConnect
{
/// <summary>
/// Stores the non-sensitive results of an incoming quick connect request.
/// </summary>
public class QuickConnectResultDto
{
/// <summary>
/// Gets a value indicating whether this request is authorized.
/// </summary>
public bool Authenticated { get; private set; }
/// <summary>
/// Gets the user facing code used so the user can quickly differentiate this request from others.
/// </summary>
public string? Code { get; private set; }
/// <summary>
/// Gets the device friendly name.
/// </summary>
public string? FriendlyName { get; private set; }
/// <summary>
/// Cast an internal quick connect result to a DTO by removing all sensitive properties.
/// </summary>
/// <param name="result">QuickConnectResult object to cast.</param>
public static implicit operator QuickConnectResultDto(QuickConnectResult result)
{
QuickConnectResultDto resultDto = new QuickConnectResultDto
{
Authenticated = result.Authenticated,
Code = result.Code,
FriendlyName = result.FriendlyName,
};
return resultDto;
}
}
}
Loading…
Cancel
Save