ApiKeyController.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.ComponentModel.DataAnnotations;
  3. using System.Globalization;
  4. using Jellyfin.Api.Constants;
  5. using MediaBrowser.Controller;
  6. using MediaBrowser.Controller.Security;
  7. using MediaBrowser.Controller.Session;
  8. using MediaBrowser.Model.Querying;
  9. using Microsoft.AspNetCore.Authorization;
  10. using Microsoft.AspNetCore.Http;
  11. using Microsoft.AspNetCore.Mvc;
  12. namespace Jellyfin.Api.Controllers
  13. {
  14. /// <summary>
  15. /// Authentication controller.
  16. /// </summary>
  17. [Route("Auth")]
  18. public class ApiKeyController : BaseJellyfinApiController
  19. {
  20. private readonly ISessionManager _sessionManager;
  21. private readonly IServerApplicationHost _appHost;
  22. private readonly IAuthenticationRepository _authRepo;
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="ApiKeyController"/> class.
  25. /// </summary>
  26. /// <param name="sessionManager">Instance of <see cref="ISessionManager"/> interface.</param>
  27. /// <param name="appHost">Instance of <see cref="IServerApplicationHost"/> interface.</param>
  28. /// <param name="authRepo">Instance of <see cref="IAuthenticationRepository"/> interface.</param>
  29. public ApiKeyController(
  30. ISessionManager sessionManager,
  31. IServerApplicationHost appHost,
  32. IAuthenticationRepository authRepo)
  33. {
  34. _sessionManager = sessionManager;
  35. _appHost = appHost;
  36. _authRepo = authRepo;
  37. }
  38. /// <summary>
  39. /// Get all keys.
  40. /// </summary>
  41. /// <response code="200">Api keys retrieved.</response>
  42. /// <returns>A <see cref="QueryResult{AuthenticationInfo}"/> with all keys.</returns>
  43. [HttpGet("Keys")]
  44. [Authorize(Policy = Policies.RequiresElevation)]
  45. [ProducesResponseType(StatusCodes.Status200OK)]
  46. public ActionResult<QueryResult<AuthenticationInfo>> GetKeys()
  47. {
  48. var result = _authRepo.Get(new AuthenticationInfoQuery
  49. {
  50. HasUser = false
  51. });
  52. return result;
  53. }
  54. /// <summary>
  55. /// Create a new api key.
  56. /// </summary>
  57. /// <param name="app">Name of the app using the authentication key.</param>
  58. /// <response code="204">Api key created.</response>
  59. /// <returns>A <see cref="NoContentResult"/>.</returns>
  60. [HttpPost("Keys")]
  61. [Authorize(Policy = Policies.RequiresElevation)]
  62. [ProducesResponseType(StatusCodes.Status204NoContent)]
  63. public ActionResult CreateKey([FromQuery, Required] string app)
  64. {
  65. _authRepo.Create(new AuthenticationInfo
  66. {
  67. AppName = app,
  68. AccessToken = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture),
  69. DateCreated = DateTime.UtcNow,
  70. DeviceId = _appHost.SystemId,
  71. DeviceName = _appHost.FriendlyName,
  72. AppVersion = _appHost.ApplicationVersionString
  73. });
  74. return NoContent();
  75. }
  76. /// <summary>
  77. /// Remove an api key.
  78. /// </summary>
  79. /// <param name="key">The access token to delete.</param>
  80. /// <response code="204">Api key deleted.</response>
  81. /// <returns>A <see cref="NoContentResult"/>.</returns>
  82. [HttpDelete("Keys/{key}")]
  83. [Authorize(Policy = Policies.RequiresElevation)]
  84. [ProducesResponseType(StatusCodes.Status204NoContent)]
  85. public ActionResult RevokeKey([FromRoute, Required] string key)
  86. {
  87. _sessionManager.RevokeToken(key);
  88. return NoContent();
  89. }
  90. }
  91. }