Merge pull request #2382 from dkanada/refactor

Refactor some API services
pull/2422/head
Bond-009 5 years ago committed by GitHub
commit 3bc0ce070d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

@ -5,7 +5,7 @@ using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Session;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Api.Session
namespace MediaBrowser.Api.Sessions
{
/// <summary>
/// Class SessionInfoWebSocketListener

@ -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
{
/// <summary>
/// Class GetSessions.
@ -24,10 +21,10 @@ namespace MediaBrowser.Api.Session
[Authenticated]
public class GetSessions : IReturn<SessionInfo[]>
{
[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<NameIdPair[]>
@ -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; }
}
/// <summary>
/// Class SessionsService.
/// </summary>
public class SessionsService : BaseApiService
public class SessionService : BaseApiService
{
/// <summary>
/// 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<SessionsService> logger,
public SessionService(
ILogger<SessionService> 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;
}
/// <summary>
/// Gets the specified request.
/// </summary>
Loading…
Cancel
Save