2
0
Эх сурвалжийг харах

Re-add support for API keys

Patrick Barron 4 жил өмнө
parent
commit
336ba2879f

+ 1 - 1
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);
 

+ 3 - 2
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; }
     }
 }

+ 3 - 5
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();
 

+ 13 - 0
Jellyfin.Server.Implementations/Security/AuthorizationContext.cs

@@ -187,6 +187,19 @@ namespace Jellyfin.Server.Implementations.Security
                     await 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;
         }

+ 1 - 1
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()
                         });

+ 1 - 1
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);
     }
 }