API cleanup

This commit is contained in:
ConfusedPolarBear 2020-06-18 01:58:58 -05:00
parent 4be476ec53
commit 329980c727
4 changed files with 27 additions and 139 deletions

View File

@ -75,18 +75,15 @@ namespace Emby.Server.Implementations.QuickConnect
{
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/>
public QuickConnectResult Activate()
public void Activate()
{
SetEnabled(QuickConnectState.Active);
DateActivated = DateTime.Now;
return new QuickConnectResult();
SetEnabled(QuickConnectState.Active);
}
/// <inheritdoc/>
@ -149,19 +146,6 @@ namespace Emby.Server.Implementations.QuickConnect
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/>
public string GenerateCode()
{
@ -215,7 +199,7 @@ namespace Emby.Server.Implementations.QuickConnect
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;
}
@ -269,11 +253,8 @@ namespace Emby.Server.Implementations.QuickConnect
return Hex.Encode(bytes);
}
/// <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>
private void ExpireRequests(bool expireAll = false)
/// <inheritdoc/>
public void ExpireRequests(bool expireAll = false)
{
// Check if quick connect should be deactivated
if (State == QuickConnectState.Active && DateTime.Now > DateActivated.AddMinutes(Timeout) && !expireAll)
@ -309,9 +290,7 @@ namespace Emby.Server.Implementations.QuickConnect
private void ReloadConfiguration()
{
var available = _config.Configuration.QuickConnectAvailable;
State = available ? QuickConnectState.Available : QuickConnectState.Unavailable;
State = _config.Configuration.QuickConnectAvailable ? QuickConnectState.Available : QuickConnectState.Unavailable;
}
}
}

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net;
@ -24,18 +23,12 @@ namespace MediaBrowser.Api.QuickConnect
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")]
[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")]
public string Lookup { get; set; }
[ApiMember(Name = "Code", Description = "Quick connect identifying code", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
public string Code { get; set; }
}
[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")]
[Authenticated]
public class Activate : IReturn<QuickConnectState>
public class Activate : IReturn<bool>
{
}
public class QuickConnectService : BaseApiService
@ -96,18 +90,9 @@ namespace MediaBrowser.Api.QuickConnect
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)
{
_quickConnect.ExpireRequests();
return _quickConnect.State;
}
@ -120,55 +105,27 @@ namespace MediaBrowser.Api.QuickConnect
public object Post(Authorize request)
{
bool result = _quickConnect.AuthorizeRequest(Request, request.Lookup);
Logger.LogInformation("Result of authorizing quick connect {0}: {1}", request.Lookup[..10], result);
return result;
return _quickConnect.AuthorizeRequest(Request, request.Code);
}
public object Post(Activate request)
{
string name = _authContext.GetAuthorizationInfo(Request).User.Name;
if(_quickConnect.State == QuickConnectState.Unavailable)
{
return new QuickConnectResult()
{
Error = "Quick connect is not enabled on this server"
};
return false;
}
else if(_quickConnect.State == QuickConnectState.Available)
{
var result = _quickConnect.Activate();
string name = _authContext.GetAuthorizationInfo(Request).User.Username;
if (string.IsNullOrEmpty(result.Error))
{
Logger.LogInformation("{name} temporarily activated quick connect", name);
}
_quickConnect.Activate();
return result;
}
else if(_quickConnect.State == QuickConnectState.Active)
{
return new QuickConnectResult()
{
Error = ""
};
}
return new QuickConnectResult()
{
Error = "Unknown current state"
};
return true;
}
public object Post(Available request)
{
_quickConnect.SetEnabled(request.Status);
return _quickConnect.State;
}
}

View File

@ -38,8 +38,7 @@ namespace MediaBrowser.Controller.QuickConnect
/// <summary>
/// Temporarily activates quick connect for a short amount of time.
/// </summary>
/// <returns>A quick connect result object indicating success.</returns>
QuickConnectResult Activate();
void Activate();
/// <summary>
/// Changes the status of quick connect.
@ -61,26 +60,20 @@ namespace MediaBrowser.Controller.QuickConnect
/// <returns>Quick connect result.</returns>
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>
/// Authorizes a quick connect request to connect as the calling user.
/// </summary>
/// <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>
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>
/// Deletes all quick connect access tokens for the provided user.
/// </summary>

View File

@ -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;
}
}
}