2020-06-10 06:57:31 -07:00
|
|
|
using System.ComponentModel.DataAnnotations;
|
2021-04-01 14:08:22 -07:00
|
|
|
using System.Threading.Tasks;
|
2020-06-10 06:57:31 -07: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 07:27:54 -07:00
|
|
|
[Route("Auth")]
|
2020-06-10 06:57:31 -07:00
|
|
|
public class ApiKeyController : BaseJellyfinApiController
|
|
|
|
{
|
2021-04-01 14:08:22 -07:00
|
|
|
private readonly IAuthenticationManager _authenticationManager;
|
2020-06-10 06:57:31 -07:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="ApiKeyController"/> class.
|
|
|
|
/// </summary>
|
2021-04-01 14:08:22 -07:00
|
|
|
/// <param name="authenticationManager">Instance of <see cref="IAuthenticationManager"/> interface.</param>
|
|
|
|
public ApiKeyController(IAuthenticationManager authenticationManager)
|
2020-06-10 06:57:31 -07:00
|
|
|
{
|
2021-04-01 14:08:22 -07:00
|
|
|
_authenticationManager = authenticationManager;
|
2020-06-10 06:57:31 -07: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 14:08:22 -07:00
|
|
|
public async Task<ActionResult<QueryResult<AuthenticationInfo>>> GetKeys()
|
2020-06-10 06:57:31 -07:00
|
|
|
{
|
2021-04-01 14:08:22 -07:00
|
|
|
var keys = await _authenticationManager.GetApiKeys();
|
2020-06-10 06:57:31 -07:00
|
|
|
|
2021-04-01 14:08:22 -07:00
|
|
|
return new QueryResult<AuthenticationInfo>
|
|
|
|
{
|
|
|
|
Items = keys,
|
|
|
|
TotalRecordCount = keys.Count
|
|
|
|
};
|
2020-06-10 06:57:31 -07: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 14:08:22 -07:00
|
|
|
public async Task<ActionResult> CreateKey([FromQuery, Required] string app)
|
2020-06-10 06:57:31 -07:00
|
|
|
{
|
2021-04-01 14:08:22 -07:00
|
|
|
await _authenticationManager.CreateApiKey(app).ConfigureAwait(false);
|
|
|
|
|
2020-06-10 06:57:31 -07: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 15:26:58 -07:00
|
|
|
public async Task<ActionResult> RevokeKey([FromRoute, Required] string key)
|
2020-06-10 06:57:31 -07:00
|
|
|
{
|
2021-04-01 14:08:22 -07:00
|
|
|
await _authenticationManager.DeleteApiKey(key).ConfigureAwait(false);
|
|
|
|
|
2020-06-10 06:57:31 -07:00
|
|
|
return NoContent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|