using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using Jellyfin.Api.Constants;
using MediaBrowser.Controller.Security;
using MediaBrowser.Model.Querying;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Api.Controllers;
///
/// Authentication controller.
///
[Route("Auth")]
public class ApiKeyController : BaseJellyfinApiController
{
private readonly IAuthenticationManager _authenticationManager;
///
/// Initializes a new instance of the class.
///
/// Instance of interface.
public ApiKeyController(IAuthenticationManager authenticationManager)
{
_authenticationManager = authenticationManager;
}
///
/// Get all keys.
///
/// Api keys retrieved.
/// A with all keys.
[HttpGet("Keys")]
[Authorize(Policy = Policies.RequiresElevation)]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task>> GetKeys()
{
var keys = await _authenticationManager.GetApiKeys().ConfigureAwait(false);
return new QueryResult(keys);
}
///
/// Create a new api key.
///
/// Name of the app using the authentication key.
/// Api key created.
/// A .
[HttpPost("Keys")]
[Authorize(Policy = Policies.RequiresElevation)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task CreateKey([FromQuery, Required] string app)
{
await _authenticationManager.CreateApiKey(app).ConfigureAwait(false);
return NoContent();
}
///
/// Remove an api key.
///
/// The access token to delete.
/// Api key deleted.
/// A .
[HttpDelete("Keys/{key}")]
[Authorize(Policy = Policies.RequiresElevation)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task RevokeKey([FromRoute, Required] string key)
{
await _authenticationManager.DeleteApiKey(key).ConfigureAwait(false);
return NoContent();
}
}