< 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: 3
Coverable lines: 3
Total lines: 76
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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%

File(s)

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

#LineLine coverage
 1using System.ComponentModel.DataAnnotations;
 2using System.Threading.Tasks;
 3using Jellyfin.Api.Constants;
 4using MediaBrowser.Common.Api;
 5using MediaBrowser.Controller.Security;
 6using MediaBrowser.Model.Querying;
 7using Microsoft.AspNetCore.Authorization;
 8using Microsoft.AspNetCore.Http;
 9using Microsoft.AspNetCore.Mvc;
 10
 11namespace Jellyfin.Api.Controllers;
 12
 13/// <summary>
 14/// Authentication controller.
 15/// </summary>
 16[Route("Auth")]
 17public 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>
 025    public ApiKeyController(IAuthenticationManager authenticationManager)
 26    {
 027        _authenticationManager = authenticationManager;
 028    }
 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}