From c2733ac0dc50e0447a6906a634d27ec9f111d23a Mon Sep 17 00:00:00 2001 From: dkanada Date: Thu, 6 Feb 2020 00:26:21 +0900 Subject: [PATCH 1/2] split api keys into their own service --- MediaBrowser.Api/Session/ApiKeysService.cs | 85 +++++++++++++++++++++ MediaBrowser.Api/Session/SessionsService.cs | 66 +--------------- 2 files changed, 88 insertions(+), 63 deletions(-) create mode 100644 MediaBrowser.Api/Session/ApiKeysService.cs diff --git a/MediaBrowser.Api/Session/ApiKeysService.cs b/MediaBrowser.Api/Session/ApiKeysService.cs new file mode 100644 index 0000000000..45d7ff4215 --- /dev/null +++ b/MediaBrowser.Api/Session/ApiKeysService.cs @@ -0,0 +1,85 @@ +using System; +using System.Globalization; +using MediaBrowser.Controller; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Net; +using MediaBrowser.Controller.Security; +using MediaBrowser.Controller.Session; +using MediaBrowser.Model.Services; +using Microsoft.Extensions.Logging; + +namespace MediaBrowser.Api.Session +{ + [Route("/Auth/Keys", "GET")] + [Authenticated(Roles = "Admin")] + public class GetApiKeys + { + } + + [Route("/Auth/Keys/{Key}", "DELETE")] + [Authenticated(Roles = "Admin")] + public class RevokeKey + { + [ApiMember(Name = "Key", Description = "Authentication key", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")] + public string Key { get; set; } + } + + [Route("/Auth/Keys", "POST")] + [Authenticated(Roles = "Admin")] + public class CreateKey + { + [ApiMember(Name = "App", Description = "Name of the app using the authentication key", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")] + public string App { get; set; } + } + + public class ApiKeysService : BaseApiService + { + private readonly ISessionManager _sessionManager; + + private readonly IAuthenticationRepository _authRepo; + + private readonly IServerApplicationHost _appHost; + + public ApiKeysService( + ILogger logger, + IServerConfigurationManager serverConfigurationManager, + IHttpResultFactory httpResultFactory, + ISessionManager sessionManager, + IServerApplicationHost appHost, + IAuthenticationRepository authRepo) + : base(logger, serverConfigurationManager, httpResultFactory) + { + _sessionManager = sessionManager; + _authRepo = authRepo; + _appHost = appHost; + } + + public void Delete(RevokeKey request) + { + _sessionManager.RevokeToken(request.Key); + } + + public void Post(CreateKey request) + { + _authRepo.Create(new AuthenticationInfo + { + AppName = request.App, + AccessToken = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture), + DateCreated = DateTime.UtcNow, + DeviceId = _appHost.SystemId, + DeviceName = _appHost.FriendlyName, + AppVersion = _appHost.ApplicationVersionString + }); + } + + public object Get(GetApiKeys request) + { + var result = _authRepo.Get(new AuthenticationInfoQuery + { + HasUser = false + }); + + return result; + } + } +} diff --git a/MediaBrowser.Api/Session/SessionsService.cs b/MediaBrowser.Api/Session/SessionsService.cs index 700861c554..01727b238b 100644 --- a/MediaBrowser.Api/Session/SessionsService.cs +++ b/MediaBrowser.Api/Session/SessionsService.cs @@ -1,14 +1,11 @@ using System; -using System.Globalization; using System.Linq; using System.Threading; using System.Threading.Tasks; -using MediaBrowser.Controller; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Devices; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Net; -using MediaBrowser.Controller.Security; using MediaBrowser.Controller.Session; using MediaBrowser.Model.Dto; using MediaBrowser.Model.Services; @@ -24,10 +21,10 @@ namespace MediaBrowser.Api.Session [Authenticated] public class GetSessions : IReturn { - [ApiMember(Name = "ControllableByUserId", Description = "Optional. Filter by sessions that a given user is allowed to remote control.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")] + [ApiMember(Name = "ControllableByUserId", Description = "Filter by sessions that a given user is allowed to remote control.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")] public Guid ControllableByUserId { get; set; } - [ApiMember(Name = "DeviceId", Description = "Optional. Filter by device id.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")] + [ApiMember(Name = "DeviceId", Description = "Filter by device Id.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")] public string DeviceId { get; set; } public int? ActiveWithinSeconds { get; set; } @@ -182,7 +179,7 @@ namespace MediaBrowser.Api.Session [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")] public string Id { get; set; } - [ApiMember(Name = "UserId", Description = "UserId Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")] + [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")] public string UserId { get; set; } } @@ -236,12 +233,6 @@ namespace MediaBrowser.Api.Session { } - [Route("/Auth/Keys", "GET")] - [Authenticated(Roles = "Admin")] - public class GetApiKeys - { - } - [Route("/Auth/Providers", "GET")] [Authenticated(Roles = "Admin")] public class GetAuthProviders : IReturn @@ -254,22 +245,6 @@ namespace MediaBrowser.Api.Session { } - [Route("/Auth/Keys/{Key}", "DELETE")] - [Authenticated(Roles = "Admin")] - public class RevokeKey - { - [ApiMember(Name = "Key", Description = "Auth Key", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")] - public string Key { get; set; } - } - - [Route("/Auth/Keys", "POST")] - [Authenticated(Roles = "Admin")] - public class CreateKey - { - [ApiMember(Name = "App", Description = "App", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")] - public string App { get; set; } - } - /// /// Class SessionsService. /// @@ -282,20 +257,16 @@ namespace MediaBrowser.Api.Session private readonly IUserManager _userManager; private readonly IAuthorizationContext _authContext; - private readonly IAuthenticationRepository _authRepo; private readonly IDeviceManager _deviceManager; private readonly ISessionContext _sessionContext; - private readonly IServerApplicationHost _appHost; public SessionsService( ILogger logger, IServerConfigurationManager serverConfigurationManager, IHttpResultFactory httpResultFactory, ISessionManager sessionManager, - IServerApplicationHost appHost, IUserManager userManager, IAuthorizationContext authContext, - IAuthenticationRepository authRepo, IDeviceManager deviceManager, ISessionContext sessionContext) : base(logger, serverConfigurationManager, httpResultFactory) @@ -303,10 +274,8 @@ namespace MediaBrowser.Api.Session _sessionManager = sessionManager; _userManager = userManager; _authContext = authContext; - _authRepo = authRepo; _deviceManager = deviceManager; _sessionContext = sessionContext; - _appHost = appHost; } public object Get(GetAuthProviders request) @@ -319,25 +288,6 @@ namespace MediaBrowser.Api.Session return _userManager.GetPasswordResetProviders(); } - public void Delete(RevokeKey request) - { - _sessionManager.RevokeToken(request.Key); - - } - - public void Post(CreateKey request) - { - _authRepo.Create(new AuthenticationInfo - { - AppName = request.App, - AccessToken = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture), - DateCreated = DateTime.UtcNow, - DeviceId = _appHost.SystemId, - DeviceName = _appHost.FriendlyName, - AppVersion = _appHost.ApplicationVersionString - }); - } - public void Post(ReportSessionEnded request) { var auth = _authContext.GetAuthorizationInfo(Request); @@ -345,16 +295,6 @@ namespace MediaBrowser.Api.Session _sessionManager.Logout(auth.Token); } - public object Get(GetApiKeys request) - { - var result = _authRepo.Get(new AuthenticationInfoQuery - { - HasUser = false - }); - - return result; - } - /// /// Gets the specified request. /// From 1cb51a8ac79c8cc2b2bde3e4279b18765a5851e6 Mon Sep 17 00:00:00 2001 From: dkanada Date: Thu, 6 Feb 2020 00:47:50 +0900 Subject: [PATCH 2/2] rename session folder --- .../ApiKeysService.cs => Sessions/ApiKeyService.cs} | 12 ++++++------ .../SessionInfoWebSocketListener.cs | 2 +- .../SessionService.cs} | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) rename MediaBrowser.Api/{Session/ApiKeysService.cs => Sessions/ApiKeyService.cs} (91%) rename MediaBrowser.Api/{Session => Sessions}/SessionInfoWebSocketListener.cs (98%) rename MediaBrowser.Api/{Session/SessionsService.cs => Sessions/SessionService.cs} (99%) diff --git a/MediaBrowser.Api/Session/ApiKeysService.cs b/MediaBrowser.Api/Sessions/ApiKeyService.cs similarity index 91% rename from MediaBrowser.Api/Session/ApiKeysService.cs rename to MediaBrowser.Api/Sessions/ApiKeyService.cs index 45d7ff4215..5102ce0a7c 100644 --- a/MediaBrowser.Api/Session/ApiKeysService.cs +++ b/MediaBrowser.Api/Sessions/ApiKeyService.cs @@ -8,11 +8,11 @@ using MediaBrowser.Controller.Session; using MediaBrowser.Model.Services; using Microsoft.Extensions.Logging; -namespace MediaBrowser.Api.Session +namespace MediaBrowser.Api.Sessions { [Route("/Auth/Keys", "GET")] [Authenticated(Roles = "Admin")] - public class GetApiKeys + public class GetKeys { } @@ -32,7 +32,7 @@ namespace MediaBrowser.Api.Session public string App { get; set; } } - public class ApiKeysService : BaseApiService + public class ApiKeyService : BaseApiService { private readonly ISessionManager _sessionManager; @@ -40,8 +40,8 @@ namespace MediaBrowser.Api.Session private readonly IServerApplicationHost _appHost; - public ApiKeysService( - ILogger logger, + public ApiKeyService( + ILogger logger, IServerConfigurationManager serverConfigurationManager, IHttpResultFactory httpResultFactory, ISessionManager sessionManager, @@ -72,7 +72,7 @@ namespace MediaBrowser.Api.Session }); } - public object Get(GetApiKeys request) + public object Get(GetKeys request) { var result = _authRepo.Get(new AuthenticationInfoQuery { diff --git a/MediaBrowser.Api/Session/SessionInfoWebSocketListener.cs b/MediaBrowser.Api/Sessions/SessionInfoWebSocketListener.cs similarity index 98% rename from MediaBrowser.Api/Session/SessionInfoWebSocketListener.cs rename to MediaBrowser.Api/Sessions/SessionInfoWebSocketListener.cs index f1a6622fb5..051d09850a 100644 --- a/MediaBrowser.Api/Session/SessionInfoWebSocketListener.cs +++ b/MediaBrowser.Api/Sessions/SessionInfoWebSocketListener.cs @@ -5,7 +5,7 @@ using MediaBrowser.Controller.Net; using MediaBrowser.Controller.Session; using Microsoft.Extensions.Logging; -namespace MediaBrowser.Api.Session +namespace MediaBrowser.Api.Sessions { /// /// Class SessionInfoWebSocketListener diff --git a/MediaBrowser.Api/Session/SessionsService.cs b/MediaBrowser.Api/Sessions/SessionService.cs similarity index 99% rename from MediaBrowser.Api/Session/SessionsService.cs rename to MediaBrowser.Api/Sessions/SessionService.cs index 01727b238b..700da5f082 100644 --- a/MediaBrowser.Api/Session/SessionsService.cs +++ b/MediaBrowser.Api/Sessions/SessionService.cs @@ -12,7 +12,7 @@ using MediaBrowser.Model.Services; using MediaBrowser.Model.Session; using Microsoft.Extensions.Logging; -namespace MediaBrowser.Api.Session +namespace MediaBrowser.Api.Sessions { /// /// Class GetSessions @@ -248,7 +248,7 @@ namespace MediaBrowser.Api.Session /// /// Class SessionsService. /// - public class SessionsService : BaseApiService + public class SessionService : BaseApiService { /// /// The _session manager. @@ -260,8 +260,8 @@ namespace MediaBrowser.Api.Session private readonly IDeviceManager _deviceManager; private readonly ISessionContext _sessionContext; - public SessionsService( - ILogger logger, + public SessionService( + ILogger logger, IServerConfigurationManager serverConfigurationManager, IHttpResultFactory httpResultFactory, ISessionManager sessionManager,