The Free Software Media System - Server Backend & API
Re-add support for API keys
Patrick Barron336ba2879f32
diff --git a/Jellyfin.Api/Controllers/ApiKeyController.cs b/Jellyfin.Api/Controllers/ApiKeyController.csindex 96efde5fbd..720b22b1d6 100644--- a/Jellyfin.Api/Controllers/ApiKeyController.cs+++ b/Jellyfin.Api/Controllers/ApiKeyController.cs@@ -71,7 +71,7 @@ namespace Jellyfin.Api.Controllers[HttpDelete("Keys/{key}")][Authorize(Policy = Policies.RequiresElevation)][ProducesResponseType(StatusCodes.Status204NoContent)]- public async Task<ActionResult> RevokeKey([FromRoute, Required] Guid key)+ public async Task<ActionResult> RevokeKey([FromRoute, Required] string key){await _authenticationManager.DeleteApiKey(key).ConfigureAwait(false);diff --git a/Jellyfin.Data/Entities/Security/ApiKey.cs b/Jellyfin.Data/Entities/Security/ApiKey.csindex 5c9ac5d5b8..31d865d018 100644--- a/Jellyfin.Data/Entities/Security/ApiKey.cs+++ b/Jellyfin.Data/Entities/Security/ApiKey.cs@@ -1,6 +1,7 @@using System;using System.ComponentModel.DataAnnotations;using System.ComponentModel.DataAnnotations.Schema;+using System.Globalization;namespace Jellyfin.Data.Entities.Security{@@ -17,7 +18,7 @@ namespace Jellyfin.Data.Entities.Security{Name = name;- AccessToken = Guid.NewGuid();+ AccessToken = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);DateCreated = DateTime.UtcNow;}@@ -50,6 +51,6 @@ namespace Jellyfin.Data.Entities.Security/// <summary>/// Gets or sets the access token./// </summary>- public Guid AccessToken { get; set; }+ public string AccessToken { get; set; }}}diff --git a/Jellyfin.Server.Implementations/Security/AuthenticationManager.cs b/Jellyfin.Server.Implementations/Security/AuthenticationManager.csindex ab76e2302f..b79e46469c 100644--- a/Jellyfin.Server.Implementations/Security/AuthenticationManager.cs+++ b/Jellyfin.Server.Implementations/Security/AuthenticationManager.cs@@ -1,6 +1,4 @@-using System;-using System.Collections.Generic;-using System.Globalization;+using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using Jellyfin.Data.Entities.Security;@@ -43,7 +41,7 @@ namespace Jellyfin.Server.Implementations.Security.Select(key => new AuthenticationInfo{AppName = key.Name,- AccessToken = key.AccessToken.ToString("N", CultureInfo.InvariantCulture),+ AccessToken = key.AccessToken,DateCreated = key.DateCreated,DeviceId = string.Empty,DeviceName = string.Empty,@@ -52,7 +50,7 @@ namespace Jellyfin.Server.Implementations.Security}/// <inheritdoc />- public async Task DeleteApiKey(Guid accessToken)+ public async Task DeleteApiKey(string accessToken){await using var dbContext = _dbProvider.CreateContext();diff --git a/Jellyfin.Server.Implementations/Security/AuthorizationContext.cs b/Jellyfin.Server.Implementations/Security/AuthorizationContext.csindex e589fae301..9a073c4770 100644--- a/Jellyfin.Server.Implementations/Security/AuthorizationContext.cs+++ b/Jellyfin.Server.Implementations/Security/AuthorizationContext.cs@@ -187,6 +187,19 @@ namespace Jellyfin.Server.Implementations.Securityawait dbContext.SaveChangesAsync().ConfigureAwait(false);}}+ else+ {+ var key = await dbContext.ApiKeys.FirstOrDefaultAsync(apiKey => apiKey.AccessToken == token).ConfigureAwait(false);+ if (key != null)+ {+ authInfo.IsAuthenticated = true;+ authInfo.Client = key.Name;+ authInfo.Token = key.AccessToken;+ authInfo.DeviceId = string.Empty;+ authInfo.Device = string.Empty;+ authInfo.Version = string.Empty;+ }+ }return authInfo;}diff --git a/Jellyfin.Server/Migrations/Routines/MigrateAuthenticationDb.cs b/Jellyfin.Server/Migrations/Routines/MigrateAuthenticationDb.csindex 10afc52a19..9bcf245d38 100644--- a/Jellyfin.Server/Migrations/Routines/MigrateAuthenticationDb.cs+++ b/Jellyfin.Server/Migrations/Routines/MigrateAuthenticationDb.cs@@ -61,7 +61,7 @@ namespace Jellyfin.Server.Migrations.Routines{dbContext.ApiKeys.Add(new ApiKey(row[3].ToString()){- AccessToken = row[1].ToGuid(),+ AccessToken = row[1].ToString(),DateCreated = row[9].ToDateTime(),DateLastActivity = row[10].ToDateTime()});diff --git a/MediaBrowser.Controller/Security/IAuthenticationManager.cs b/MediaBrowser.Controller/Security/IAuthenticationManager.csindex 46d0c66224..29621b73e7 100644--- a/MediaBrowser.Controller/Security/IAuthenticationManager.cs+++ b/MediaBrowser.Controller/Security/IAuthenticationManager.cs@@ -29,6 +29,6 @@ namespace MediaBrowser.Controller.Security/// </summary>/// <param name="accessToken">The access token.</param>/// <returns>A task representing the deletion of the API key.</returns>- Task DeleteApiKey(Guid accessToken);+ Task DeleteApiKey(string accessToken);}}