jellyfin/Jellyfin.Api/Controllers/ApiKeyController.cs

77 lines
2.6 KiB
C#
Raw Normal View History

2020-06-10 13:57:31 +00:00
using System.ComponentModel.DataAnnotations;
2021-04-01 21:08:22 +00:00
using System.Threading.Tasks;
2020-06-10 13:57:31 +00:00
using Jellyfin.Api.Constants;
using MediaBrowser.Common.Api;
2020-06-10 13:57:31 +00:00
using MediaBrowser.Controller.Security;
using MediaBrowser.Model.Querying;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
2023-01-31 11:18:10 +00:00
namespace Jellyfin.Api.Controllers;
/// <summary>
/// Authentication controller.
/// </summary>
[Route("Auth")]
public class ApiKeyController : BaseJellyfinApiController
2020-06-10 13:57:31 +00:00
{
2023-01-31 11:18:10 +00:00
private readonly IAuthenticationManager _authenticationManager;
2020-06-10 13:57:31 +00:00
/// <summary>
2023-01-31 11:18:10 +00:00
/// Initializes a new instance of the <see cref="ApiKeyController"/> class.
2020-06-10 13:57:31 +00:00
/// </summary>
2023-01-31 11:18:10 +00:00
/// <param name="authenticationManager">Instance of <see cref="IAuthenticationManager"/> interface.</param>
public ApiKeyController(IAuthenticationManager authenticationManager)
2020-06-10 13:57:31 +00:00
{
2023-01-31 11:18:10 +00:00
_authenticationManager = authenticationManager;
}
2020-06-10 13:57:31 +00:00
2023-01-31 11:18:10 +00:00
/// <summary>
/// Get all keys.
/// </summary>
/// <response code="200">Api keys retrieved.</response>
/// <returns>A <see cref="QueryResult{AuthenticationInfo}"/> with all keys.</returns>
[HttpGet("Keys")]
[Authorize(Policy = Policies.RequiresElevation)]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult<QueryResult<AuthenticationInfo>>> GetKeys()
{
var keys = await _authenticationManager.GetApiKeys().ConfigureAwait(false);
2020-06-10 13:57:31 +00:00
2023-01-31 11:18:10 +00:00
return new QueryResult<AuthenticationInfo>(keys);
}
2020-06-10 13:57:31 +00:00
2023-01-31 11:18:10 +00:00
/// <summary>
/// Create a new api key.
/// </summary>
/// <param name="app">Name of the app using the authentication key.</param>
/// <response code="204">Api key created.</response>
/// <returns>A <see cref="NoContentResult"/>.</returns>
[HttpPost("Keys")]
[Authorize(Policy = Policies.RequiresElevation)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<ActionResult> CreateKey([FromQuery, Required] string app)
{
await _authenticationManager.CreateApiKey(app).ConfigureAwait(false);
2021-04-01 21:08:22 +00:00
2023-01-31 11:18:10 +00:00
return NoContent();
}
2020-06-10 13:57:31 +00:00
2023-01-31 11:18:10 +00:00
/// <summary>
/// Remove an api key.
/// </summary>
/// <param name="key">The access token to delete.</param>
/// <response code="204">Api key deleted.</response>
/// <returns>A <see cref="NoContentResult"/>.</returns>
[HttpDelete("Keys/{key}")]
[Authorize(Policy = Policies.RequiresElevation)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<ActionResult> RevokeKey([FromRoute, Required] string key)
{
await _authenticationManager.DeleteApiKey(key).ConfigureAwait(false);
2021-04-01 21:08:22 +00:00
2023-01-31 11:18:10 +00:00
return NoContent();
2020-06-10 13:57:31 +00:00
}
}