2
0

ApiKeyService.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Globalization;
  3. using MediaBrowser.Controller;
  4. using MediaBrowser.Controller.Configuration;
  5. using MediaBrowser.Controller.Net;
  6. using MediaBrowser.Controller.Security;
  7. using MediaBrowser.Controller.Session;
  8. using MediaBrowser.Model.Services;
  9. using Microsoft.Extensions.Logging;
  10. namespace MediaBrowser.Api.Sessions
  11. {
  12. [Route("/Auth/Keys", "GET")]
  13. [Authenticated(Roles = "Admin")]
  14. public class GetKeys
  15. {
  16. }
  17. [Route("/Auth/Keys/{Key}", "DELETE")]
  18. [Authenticated(Roles = "Admin")]
  19. public class RevokeKey
  20. {
  21. [ApiMember(Name = "Key", Description = "Authentication key", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
  22. public string Key { get; set; }
  23. }
  24. [Route("/Auth/Keys", "POST")]
  25. [Authenticated(Roles = "Admin")]
  26. public class CreateKey
  27. {
  28. [ApiMember(Name = "App", Description = "Name of the app using the authentication key", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  29. public string App { get; set; }
  30. }
  31. public class ApiKeyService : BaseApiService
  32. {
  33. private readonly ISessionManager _sessionManager;
  34. private readonly IAuthenticationRepository _authRepo;
  35. private readonly IServerApplicationHost _appHost;
  36. public ApiKeyService(
  37. ILogger<ApiKeyService> logger,
  38. IServerConfigurationManager serverConfigurationManager,
  39. IHttpResultFactory httpResultFactory,
  40. ISessionManager sessionManager,
  41. IServerApplicationHost appHost,
  42. IAuthenticationRepository authRepo)
  43. : base(logger, serverConfigurationManager, httpResultFactory)
  44. {
  45. _sessionManager = sessionManager;
  46. _authRepo = authRepo;
  47. _appHost = appHost;
  48. }
  49. public void Delete(RevokeKey request)
  50. {
  51. _sessionManager.RevokeToken(request.Key);
  52. }
  53. public void Post(CreateKey request)
  54. {
  55. _authRepo.Create(new AuthenticationInfo
  56. {
  57. AppName = request.App,
  58. AccessToken = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture),
  59. DateCreated = DateTime.UtcNow,
  60. DeviceId = _appHost.SystemId,
  61. DeviceName = _appHost.FriendlyName,
  62. AppVersion = _appHost.ApplicationVersionString
  63. });
  64. }
  65. public object Get(GetKeys request)
  66. {
  67. var result = _authRepo.Get(new AuthenticationInfoQuery
  68. {
  69. HasUser = false
  70. });
  71. return result;
  72. }
  73. }
  74. }