< Summary - Jellyfin

Information
Class: Jellyfin.Api.Controllers.ApiKeyController
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Controllers/ApiKeyController.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 27
Coverable lines: 27
Total lines: 96
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 5/16/2026 - 12:15:55 AM Line coverage: 0% (0/12) Total lines: 7710/29/2026 - 12:17:11 AM Line coverage: 0% (0/27) Total lines: 96

Coverage delta

Coverage delta 1 -1

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
GetKeys()100%210%
CreateKey()100%210%
RevokeKey()100%210%
ToDto(...)100%210%

File(s)

/srv/git/jellyfin/Jellyfin.Api/Controllers/ApiKeyController.cs

#LineLine coverage
 1using System.ComponentModel.DataAnnotations;
 2using System.Linq;
 3using System.Threading.Tasks;
 4using Jellyfin.Api.Constants;
 5using Jellyfin.Api.Models.ApiKeyDtos;
 6using MediaBrowser.Common.Api;
 7using MediaBrowser.Controller.Security;
 8using MediaBrowser.Model.Querying;
 9using Microsoft.AspNetCore.Authorization;
 10using Microsoft.AspNetCore.Http;
 11using Microsoft.AspNetCore.Mvc;
 12
 13namespace Jellyfin.Api.Controllers;
 14
 15/// <summary>
 16/// Authentication controller.
 17/// </summary>
 18[Route("Auth")]
 19[Tags("Authentication")]
 20public 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>
 028    public ApiKeyController(IAuthenticationManager authenticationManager)
 29    {
 030        _authenticationManager = authenticationManager;
 031    }
 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    {
 043        var keys = await _authenticationManager.GetApiKeys().ConfigureAwait(false);
 44
 045        return new QueryResult<AuthenticationInfoDto>(keys.Select(ToDto).ToList());
 046    }
 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    {
 059        var key = await _authenticationManager.CreateApiKey(app).ConfigureAwait(false);
 60
 061        return ToDto(key);
 062    }
 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    {
 075        await _authenticationManager.DeleteApiKey(key).ConfigureAwait(false);
 76
 077        return NoContent();
 078    }
 79
 80    private static AuthenticationInfoDto ToDto(AuthenticationInfo info)
 081        => new()
 082        {
 083            Id = info.Id,
 084            AccessToken = info.AccessToken,
 085            DeviceId = info.DeviceId,
 086            AppName = info.AppName,
 087            AppVersion = info.AppVersion,
 088            DeviceName = info.DeviceName,
 089            UserId = info.UserId,
 090            IsActive = info.IsActive,
 091            DateCreated = info.DateCreated,
 092            DateRevoked = info.DateRevoked,
 093            DateLastActivity = info.DateLastActivity,
 094            UserName = info.UserName
 095        };
 96}