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.Controller.Security;
|
|
|
|
using MediaBrowser.Model.Querying;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace Jellyfin.Api.Controllers
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Authentication controller.
|
|
|
|
/// </summary>
|
2020-08-04 14:27:54 +00:00
|
|
|
[Route("Auth")]
|
2020-06-10 13:57:31 +00:00
|
|
|
public class ApiKeyController : BaseJellyfinApiController
|
|
|
|
{
|
2021-04-01 21:08:22 +00:00
|
|
|
private readonly IAuthenticationManager _authenticationManager;
|
2020-06-10 13:57:31 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="ApiKeyController"/> class.
|
|
|
|
/// </summary>
|
2021-04-01 21:08:22 +00:00
|
|
|
/// <param name="authenticationManager">Instance of <see cref="IAuthenticationManager"/> interface.</param>
|
|
|
|
public ApiKeyController(IAuthenticationManager authenticationManager)
|
2020-06-10 13:57:31 +00:00
|
|
|
{
|
2021-04-01 21:08:22 +00:00
|
|
|
_authenticationManager = authenticationManager;
|
2020-06-10 13:57:31 +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)]
|
2021-04-01 21:08:22 +00:00
|
|
|
public async Task<ActionResult<QueryResult<AuthenticationInfo>>> GetKeys()
|
2020-06-10 13:57:31 +00:00
|
|
|
{
|
2021-04-01 21:08:22 +00:00
|
|
|
var keys = await _authenticationManager.GetApiKeys();
|
2020-06-10 13:57:31 +00:00
|
|
|
|
2022-01-20 15:46:17 +00:00
|
|
|
return new QueryResult<AuthenticationInfo>(keys);
|
2020-06-10 13:57:31 +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)]
|
2021-04-01 21:08:22 +00:00
|
|
|
public async Task<ActionResult> CreateKey([FromQuery, Required] string app)
|
2020-06-10 13:57:31 +00:00
|
|
|
{
|
2021-04-01 21:08:22 +00:00
|
|
|
await _authenticationManager.CreateApiKey(app).ConfigureAwait(false);
|
|
|
|
|
2020-06-10 13:57:31 +00:00
|
|
|
return NoContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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)]
|
2021-06-18 22:26:58 +00:00
|
|
|
public async Task<ActionResult> RevokeKey([FromRoute, Required] string key)
|
2020-06-10 13:57:31 +00:00
|
|
|
{
|
2021-04-01 21:08:22 +00:00
|
|
|
await _authenticationManager.DeleteApiKey(key).ConfigureAwait(false);
|
|
|
|
|
2020-06-10 13:57:31 +00:00
|
|
|
return NoContent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|