2
0

ApiKeyController.cs 2.9 KB

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