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