aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Api/Controllers/ApiKeyController.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Jellyfin.Api/Controllers/ApiKeyController.cs')
-rw-r--r--Jellyfin.Api/Controllers/ApiKeyController.cs134
1 files changed, 56 insertions, 78 deletions
diff --git a/Jellyfin.Api/Controllers/ApiKeyController.cs b/Jellyfin.Api/Controllers/ApiKeyController.cs
index 8c43d786a..991f8cbf2 100644
--- a/Jellyfin.Api/Controllers/ApiKeyController.cs
+++ b/Jellyfin.Api/Controllers/ApiKeyController.cs
@@ -1,97 +1,75 @@
-using System;
using System.ComponentModel.DataAnnotations;
-using System.Globalization;
+using System.Threading.Tasks;
using Jellyfin.Api.Constants;
-using MediaBrowser.Controller;
using MediaBrowser.Controller.Security;
-using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Querying;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
-namespace Jellyfin.Api.Controllers
+namespace Jellyfin.Api.Controllers;
+
+/// <summary>
+/// Authentication controller.
+/// </summary>
+[Route("Auth")]
+public class ApiKeyController : BaseJellyfinApiController
{
+ private readonly IAuthenticationManager _authenticationManager;
+
/// <summary>
- /// Authentication controller.
+ /// Initializes a new instance of the <see cref="ApiKeyController"/> class.
/// </summary>
- [Route("Auth")]
- public class ApiKeyController : BaseJellyfinApiController
+ /// <param name="authenticationManager">Instance of <see cref="IAuthenticationManager"/> interface.</param>
+ public ApiKeyController(IAuthenticationManager authenticationManager)
{
- private readonly ISessionManager _sessionManager;
- private readonly IServerApplicationHost _appHost;
- private readonly IAuthenticationRepository _authRepo;
+ _authenticationManager = authenticationManager;
+ }
- /// <summary>
- /// Initializes a new instance of the <see cref="ApiKeyController"/> class.
- /// </summary>
- /// <param name="sessionManager">Instance of <see cref="ISessionManager"/> interface.</param>
- /// <param name="appHost">Instance of <see cref="IServerApplicationHost"/> interface.</param>
- /// <param name="authRepo">Instance of <see cref="IAuthenticationRepository"/> interface.</param>
- public ApiKeyController(
- ISessionManager sessionManager,
- IServerApplicationHost appHost,
- IAuthenticationRepository authRepo)
- {
- _sessionManager = sessionManager;
- _appHost = appHost;
- _authRepo = authRepo;
- }
+ /// <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);
- /// <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 ActionResult<QueryResult<AuthenticationInfo>> GetKeys()
- {
- var result = _authRepo.Get(new AuthenticationInfoQuery
- {
- HasUser = false
- });
+ return new QueryResult<AuthenticationInfo>(keys);
+ }
- return result;
- }
+ /// <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);
+
+ return NoContent();
+ }
- /// <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 ActionResult CreateKey([FromQuery, Required] string app)
- {
- _authRepo.Create(new AuthenticationInfo
- {
- AppName = app,
- AccessToken = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture),
- DateCreated = DateTime.UtcNow,
- DeviceId = _appHost.SystemId,
- DeviceName = _appHost.FriendlyName,
- AppVersion = _appHost.ApplicationVersionString
- });
- 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)]
+ public async Task<ActionResult> RevokeKey([FromRoute, Required] string key)
+ {
+ await _authenticationManager.DeleteApiKey(key).ConfigureAwait(false);
- /// <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 ActionResult RevokeKey([FromRoute, Required] string key)
- {
- _sessionManager.RevokeToken(key);
- return NoContent();
- }
+ return NoContent();
}
}