aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Barron <barronpm@gmail.com>2021-06-18 18:26:58 -0400
committerPatrick Barron <barronpm@gmail.com>2021-06-18 18:26:58 -0400
commit336ba2879f325a4efd52bc7737ce94f40369bfeb (patch)
tree56f159b85dde61ab4e1b1ca75caedde4f86301fd
parent0292936c659b25464c1bc1e1b80711f873a1a7cd (diff)
Re-add support for API keys
-rw-r--r--Jellyfin.Api/Controllers/ApiKeyController.cs2
-rw-r--r--Jellyfin.Data/Entities/Security/ApiKey.cs5
-rw-r--r--Jellyfin.Server.Implementations/Security/AuthenticationManager.cs8
-rw-r--r--Jellyfin.Server.Implementations/Security/AuthorizationContext.cs13
-rw-r--r--Jellyfin.Server/Migrations/Routines/MigrateAuthenticationDb.cs2
-rw-r--r--MediaBrowser.Controller/Security/IAuthenticationManager.cs2
6 files changed, 22 insertions, 10 deletions
diff --git a/Jellyfin.Api/Controllers/ApiKeyController.cs b/Jellyfin.Api/Controllers/ApiKeyController.cs
index 96efde5fb..720b22b1d 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.cs
index 5c9ac5d5b..31d865d01 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.cs
index ab76e2302..b79e46469 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.cs
index e589fae30..9a073c477 100644
--- a/Jellyfin.Server.Implementations/Security/AuthorizationContext.cs
+++ b/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;
}
diff --git a/Jellyfin.Server/Migrations/Routines/MigrateAuthenticationDb.cs b/Jellyfin.Server/Migrations/Routines/MigrateAuthenticationDb.cs
index 10afc52a1..9bcf245d3 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.cs
index 46d0c6622..29621b73e 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);
}
}