using System;
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();
return new QueryResult
{
Items = keys,
TotalRecordCount = keys.Count
};
}
///
/// 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] Guid key)
{
await _authenticationManager.DeleteApiKey(key).ConfigureAwait(false);
return NoContent();
}
}
}