| | | 1 | | using System.ComponentModel.DataAnnotations; |
| | | 2 | | using System.Linq; |
| | | 3 | | using System.Threading.Tasks; |
| | | 4 | | using Jellyfin.Api.Constants; |
| | | 5 | | using Jellyfin.Api.Models.ApiKeyDtos; |
| | | 6 | | using MediaBrowser.Common.Api; |
| | | 7 | | using MediaBrowser.Controller.Security; |
| | | 8 | | using MediaBrowser.Model.Querying; |
| | | 9 | | using Microsoft.AspNetCore.Authorization; |
| | | 10 | | using Microsoft.AspNetCore.Http; |
| | | 11 | | using Microsoft.AspNetCore.Mvc; |
| | | 12 | | |
| | | 13 | | namespace Jellyfin.Api.Controllers; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Authentication controller. |
| | | 17 | | /// </summary> |
| | | 18 | | [Route("Auth")] |
| | | 19 | | [Tags("Authentication")] |
| | | 20 | | public class ApiKeyController : BaseJellyfinApiController |
| | | 21 | | { |
| | | 22 | | private readonly IAuthenticationManager _authenticationManager; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="ApiKeyController"/> class. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="authenticationManager">Instance of <see cref="IAuthenticationManager"/> interface.</param> |
| | 0 | 28 | | public ApiKeyController(IAuthenticationManager authenticationManager) |
| | | 29 | | { |
| | 0 | 30 | | _authenticationManager = authenticationManager; |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Get all keys. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <response code="200">Api keys retrieved.</response> |
| | | 37 | | /// <returns>A <see cref="QueryResult{AuthenticationInfoDto}"/> with all keys.</returns> |
| | | 38 | | [HttpGet("Keys")] |
| | | 39 | | [Authorize(Policy = Policies.RequiresElevation)] |
| | | 40 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 41 | | public async Task<ActionResult<QueryResult<AuthenticationInfoDto>>> GetKeys() |
| | | 42 | | { |
| | 0 | 43 | | var keys = await _authenticationManager.GetApiKeys().ConfigureAwait(false); |
| | | 44 | | |
| | 0 | 45 | | return new QueryResult<AuthenticationInfoDto>(keys.Select(ToDto).ToList()); |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Create a new api key. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="app">Name of the app using the authentication key.</param> |
| | | 52 | | /// <response code="200">Api key created.</response> |
| | | 53 | | /// <returns>The created api key.</returns> |
| | | 54 | | [HttpPost("Keys")] |
| | | 55 | | [Authorize(Policy = Policies.RequiresElevation)] |
| | | 56 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 57 | | public async Task<ActionResult<AuthenticationInfoDto>> CreateKey([FromQuery, Required] string app) |
| | | 58 | | { |
| | 0 | 59 | | var key = await _authenticationManager.CreateApiKey(app).ConfigureAwait(false); |
| | | 60 | | |
| | 0 | 61 | | return ToDto(key); |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Remove an api key. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <param name="key">The access token to delete.</param> |
| | | 68 | | /// <response code="204">Api key deleted.</response> |
| | | 69 | | /// <returns>A <see cref="NoContentResult"/>.</returns> |
| | | 70 | | [HttpDelete("Keys/{key}")] |
| | | 71 | | [Authorize(Policy = Policies.RequiresElevation)] |
| | | 72 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 73 | | public async Task<ActionResult> RevokeKey([FromRoute, Required] string key) |
| | | 74 | | { |
| | 0 | 75 | | await _authenticationManager.DeleteApiKey(key).ConfigureAwait(false); |
| | | 76 | | |
| | 0 | 77 | | return NoContent(); |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | private static AuthenticationInfoDto ToDto(AuthenticationInfo info) |
| | 0 | 81 | | => new() |
| | 0 | 82 | | { |
| | 0 | 83 | | Id = info.Id, |
| | 0 | 84 | | AccessToken = info.AccessToken, |
| | 0 | 85 | | DeviceId = info.DeviceId, |
| | 0 | 86 | | AppName = info.AppName, |
| | 0 | 87 | | AppVersion = info.AppVersion, |
| | 0 | 88 | | DeviceName = info.DeviceName, |
| | 0 | 89 | | UserId = info.UserId, |
| | 0 | 90 | | IsActive = info.IsActive, |
| | 0 | 91 | | DateCreated = info.DateCreated, |
| | 0 | 92 | | DateRevoked = info.DateRevoked, |
| | 0 | 93 | | DateLastActivity = info.DateLastActivity, |
| | 0 | 94 | | UserName = info.UserName |
| | 0 | 95 | | }; |
| | | 96 | | } |