From 329980c727cf03587ff5f4011a3af3ef2fa5e4f1 Mon Sep 17 00:00:00 2001
From: ConfusedPolarBear <33811686+ConfusedPolarBear@users.noreply.github.com>
Date: Thu, 18 Jun 2020 01:58:58 -0500
Subject: [PATCH] API cleanup
---
.../QuickConnect/QuickConnectManager.cs | 35 ++--------
.../QuickConnect/QuickConnectService.cs | 67 ++++---------------
.../QuickConnect/IQuickConnect.cs | 23 +++----
.../QuickConnect/QuickConnectResultDto.cs | 41 ------------
4 files changed, 27 insertions(+), 139 deletions(-)
delete mode 100644 MediaBrowser.Model/QuickConnect/QuickConnectResultDto.cs
diff --git a/Emby.Server.Implementations/QuickConnect/QuickConnectManager.cs b/Emby.Server.Implementations/QuickConnect/QuickConnectManager.cs
index 8d704f32b7..263556e9d7 100644
--- a/Emby.Server.Implementations/QuickConnect/QuickConnectManager.cs
+++ b/Emby.Server.Implementations/QuickConnect/QuickConnectManager.cs
@@ -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");
}
}
///
- public QuickConnectResult Activate()
+ public void Activate()
{
- SetEnabled(QuickConnectState.Active);
-
DateActivated = DateTime.Now;
-
- return new QuickConnectResult();
+ SetEnabled(QuickConnectState.Active);
}
///
@@ -149,19 +146,6 @@ namespace Emby.Server.Implementations.QuickConnect
return result;
}
- public List GetCurrentRequests()
- {
- return GetCurrentRequestsInternal().Select(x => (QuickConnectResultDto)x).ToList();
- }
-
- ///
- public List GetCurrentRequestsInternal()
- {
- ExpireRequests();
- AssertActive();
- return _currentRequests.Values.ToList();
- }
-
///
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);
}
- ///
- /// Expire quick connect requests that are over the time limit. If is true, all requests are unconditionally expired.
- ///
- /// If true, all requests will be expired.
- private void ExpireRequests(bool expireAll = false)
+ ///
+ 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;
}
}
}
diff --git a/MediaBrowser.Api/QuickConnect/QuickConnectService.cs b/MediaBrowser.Api/QuickConnect/QuickConnectService.cs
index 60d6ac4147..9047a1e957 100644
--- a/MediaBrowser.Api/QuickConnect/QuickConnectService.cs
+++ b/MediaBrowser.Api/QuickConnect/QuickConnectService.cs
@@ -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>
- {
- }
-
[Route("/QuickConnect/Authorize", "POST", Summary = "Authorizes a pending quick connect request")]
[Authenticated]
- public class Authorize : IReturn
+ public class Authorize : IReturn
{
- [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
+ public class Activate : IReturn
{
+
}
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();
- }
-
- 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();
-
- if (string.IsNullOrEmpty(result.Error))
- {
- Logger.LogInformation("{name} temporarily activated quick connect", name);
- }
+ string name = _authContext.GetAuthorizationInfo(Request).User.Username;
- return result;
- }
+ Logger.LogInformation("{name} temporarily activated quick connect", name);
+ _quickConnect.Activate();
- 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;
}
}
diff --git a/MediaBrowser.Controller/QuickConnect/IQuickConnect.cs b/MediaBrowser.Controller/QuickConnect/IQuickConnect.cs
index d31d0e5097..10ec9e6cb5 100644
--- a/MediaBrowser.Controller/QuickConnect/IQuickConnect.cs
+++ b/MediaBrowser.Controller/QuickConnect/IQuickConnect.cs
@@ -38,8 +38,7 @@ namespace MediaBrowser.Controller.QuickConnect
///
/// Temporarily activates quick connect for a short amount of time.
///
- /// A quick connect result object indicating success.
- QuickConnectResult Activate();
+ void Activate();
///
/// Changes the status of quick connect.
@@ -61,26 +60,20 @@ namespace MediaBrowser.Controller.QuickConnect
/// Quick connect result.
QuickConnectResult CheckRequestStatus(string secret);
- ///
- /// Returns all current quick connect requests as DTOs. Does not include sensitive information.
- ///
- /// List of all quick connect results.
- List GetCurrentRequests();
-
- ///
- /// Returns all current quick connect requests (including sensitive information).
- ///
- /// List of all quick connect results.
- List GetCurrentRequestsInternal();
-
///
/// Authorizes a quick connect request to connect as the calling user.
///
/// HTTP request object.
- /// Identifying code for the request..
+ /// Identifying code for the request.
/// A boolean indicating if the authorization completed successfully.
bool AuthorizeRequest(IRequest request, string code);
+ ///
+ /// Expire quick connect requests that are over the time limit. If is true, all requests are unconditionally expired.
+ ///
+ /// If true, all requests will be expired.
+ public void ExpireRequests(bool expireAll = false);
+
///
/// Deletes all quick connect access tokens for the provided user.
///
diff --git a/MediaBrowser.Model/QuickConnect/QuickConnectResultDto.cs b/MediaBrowser.Model/QuickConnect/QuickConnectResultDto.cs
deleted file mode 100644
index 26084caf1e..0000000000
--- a/MediaBrowser.Model/QuickConnect/QuickConnectResultDto.cs
+++ /dev/null
@@ -1,41 +0,0 @@
-using System;
-
-namespace MediaBrowser.Model.QuickConnect
-{
- ///
- /// Stores the non-sensitive results of an incoming quick connect request.
- ///
- public class QuickConnectResultDto
- {
- ///
- /// Gets a value indicating whether this request is authorized.
- ///
- public bool Authenticated { get; private set; }
-
- ///
- /// Gets the user facing code used so the user can quickly differentiate this request from others.
- ///
- public string? Code { get; private set; }
-
- ///
- /// Gets the device friendly name.
- ///
- public string? FriendlyName { get; private set; }
-
- ///
- /// Cast an internal quick connect result to a DTO by removing all sensitive properties.
- ///
- /// QuickConnectResult object to cast.
- public static implicit operator QuickConnectResultDto(QuickConnectResult result)
- {
- QuickConnectResultDto resultDto = new QuickConnectResultDto
- {
- Authenticated = result.Authenticated,
- Code = result.Code,
- FriendlyName = result.FriendlyName,
- };
-
- return resultDto;
- }
- }
-}