2
0

ApiKeyController.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.ComponentModel.DataAnnotations;
  3. using System.Threading.Tasks;
  4. using Jellyfin.Api.Constants;
  5. using MediaBrowser.Controller.Security;
  6. using MediaBrowser.Model.Querying;
  7. using Microsoft.AspNetCore.Authorization;
  8. using Microsoft.AspNetCore.Http;
  9. using Microsoft.AspNetCore.Mvc;
  10. namespace Jellyfin.Api.Controllers
  11. {
  12. /// <summary>
  13. /// Authentication controller.
  14. /// </summary>
  15. [Route("Auth")]
  16. public class ApiKeyController : BaseJellyfinApiController
  17. {
  18. private readonly IAuthenticationManager _authenticationManager;
  19. /// <summary>
  20. /// Initializes a new instance of the <see cref="ApiKeyController"/> class.
  21. /// </summary>
  22. /// <param name="authenticationManager">Instance of <see cref="IAuthenticationManager"/> interface.</param>
  23. public ApiKeyController(IAuthenticationManager authenticationManager)
  24. {
  25. _authenticationManager = authenticationManager;
  26. }
  27. /// <summary>
  28. /// Get all keys.
  29. /// </summary>
  30. /// <response code="200">Api keys retrieved.</response>
  31. /// <returns>A <see cref="QueryResult{AuthenticationInfo}"/> with all keys.</returns>
  32. [HttpGet("Keys")]
  33. [Authorize(Policy = Policies.RequiresElevation)]
  34. [ProducesResponseType(StatusCodes.Status200OK)]
  35. public async Task<ActionResult<QueryResult<AuthenticationInfo>>> GetKeys()
  36. {
  37. var keys = await _authenticationManager.GetApiKeys();
  38. return new QueryResult<AuthenticationInfo>
  39. {
  40. Items = keys,
  41. TotalRecordCount = keys.Count
  42. };
  43. }
  44. /// <summary>
  45. /// Create a new api key.
  46. /// </summary>
  47. /// <param name="app">Name of the app using the authentication key.</param>
  48. /// <response code="204">Api key created.</response>
  49. /// <returns>A <see cref="NoContentResult"/>.</returns>
  50. [HttpPost("Keys")]
  51. [Authorize(Policy = Policies.RequiresElevation)]
  52. [ProducesResponseType(StatusCodes.Status204NoContent)]
  53. public async Task<ActionResult> CreateKey([FromQuery, Required] string app)
  54. {
  55. await _authenticationManager.CreateApiKey(app).ConfigureAwait(false);
  56. return NoContent();
  57. }
  58. /// <summary>
  59. /// Remove an api key.
  60. /// </summary>
  61. /// <param name="key">The access token to delete.</param>
  62. /// <response code="204">Api key deleted.</response>
  63. /// <returns>A <see cref="NoContentResult"/>.</returns>
  64. [HttpDelete("Keys/{key}")]
  65. [Authorize(Policy = Policies.RequiresElevation)]
  66. [ProducesResponseType(StatusCodes.Status204NoContent)]
  67. public async Task<ActionResult> RevokeKey([FromRoute, Required] string key)
  68. {
  69. await _authenticationManager.DeleteApiKey(key).ConfigureAwait(false);
  70. return NoContent();
  71. }
  72. }
  73. }