diff --git a/MediaBrowser.Api/Sessions/ApiKeyService.cs b/MediaBrowser.Api/Sessions/ApiKeyService.cs new file mode 100644 index 000000000..5102ce0a7 --- /dev/null +++ b/MediaBrowser.Api/Sessions/ApiKeyService.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.Sessions +{ + [Route("/Auth/Keys", "GET")] + [Authenticated(Roles = "Admin")] + public class GetKeys + { + } + + [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 ApiKeyService : BaseApiService + { + private readonly ISessionManager _sessionManager; + + private readonly IAuthenticationRepository _authRepo; + + private readonly IServerApplicationHost _appHost; + + public ApiKeyService( + 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(GetKeys request) + { + var result = _authRepo.Get(new AuthenticationInfoQuery + { + HasUser = false + }); + + return result; + } + } +} 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 f1a6622fb..051d09850 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 88% rename from MediaBrowser.Api/Session/SessionsService.cs rename to MediaBrowser.Api/Sessions/SessionService.cs index 054371321..020bb5042 100644 --- a/MediaBrowser.Api/Session/SessionsService.cs +++ b/MediaBrowser.Api/Sessions/SessionService.cs @@ -1,21 +1,18 @@ 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; using MediaBrowser.Model.Session; using Microsoft.Extensions.Logging; -namespace MediaBrowser.Api.Session +namespace MediaBrowser.Api.Sessions { /// /// Class GetSessions. @@ -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; } } @@ -247,12 +244,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 @@ -265,26 +256,10 @@ 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. /// - public class SessionsService : BaseApiService + public class SessionService : BaseApiService { /// /// The session manager. @@ -293,20 +268,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, + public SessionService( + ILogger logger, IServerConfigurationManager serverConfigurationManager, IHttpResultFactory httpResultFactory, ISessionManager sessionManager, - IServerApplicationHost appHost, IUserManager userManager, IAuthorizationContext authContext, - IAuthenticationRepository authRepo, IDeviceManager deviceManager, ISessionContext sessionContext) : base(logger, serverConfigurationManager, httpResultFactory) @@ -314,10 +285,8 @@ namespace MediaBrowser.Api.Session _sessionManager = sessionManager; _userManager = userManager; _authContext = authContext; - _authRepo = authRepo; _deviceManager = deviceManager; _sessionContext = sessionContext; - _appHost = appHost; } public object Get(GetAuthProviders request) @@ -330,25 +299,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); @@ -356,16 +306,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. ///