Re-add support for API keys

This commit is contained in:
Patrick Barron 2021-06-18 18:26:58 -04:00
parent 0292936c65
commit 336ba2879f
6 changed files with 22 additions and 10 deletions

View File

@ -71,7 +71,7 @@ namespace Jellyfin.Api.Controllers
[HttpDelete("Keys/{key}")] [HttpDelete("Keys/{key}")]
[Authorize(Policy = Policies.RequiresElevation)] [Authorize(Policy = Policies.RequiresElevation)]
[ProducesResponseType(StatusCodes.Status204NoContent)] [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); await _authenticationManager.DeleteApiKey(key).ConfigureAwait(false);

View File

@ -1,6 +1,7 @@
using System; using System;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
using System.Globalization;
namespace Jellyfin.Data.Entities.Security namespace Jellyfin.Data.Entities.Security
{ {
@ -17,7 +18,7 @@ namespace Jellyfin.Data.Entities.Security
{ {
Name = name; Name = name;
AccessToken = Guid.NewGuid(); AccessToken = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);
DateCreated = DateTime.UtcNow; DateCreated = DateTime.UtcNow;
} }
@ -50,6 +51,6 @@ namespace Jellyfin.Data.Entities.Security
/// <summary> /// <summary>
/// Gets or sets the access token. /// Gets or sets the access token.
/// </summary> /// </summary>
public Guid AccessToken { get; set; } public string AccessToken { get; set; }
} }
} }

View File

@ -1,6 +1,4 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Globalization;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Jellyfin.Data.Entities.Security; using Jellyfin.Data.Entities.Security;
@ -43,7 +41,7 @@ namespace Jellyfin.Server.Implementations.Security
.Select(key => new AuthenticationInfo .Select(key => new AuthenticationInfo
{ {
AppName = key.Name, AppName = key.Name,
AccessToken = key.AccessToken.ToString("N", CultureInfo.InvariantCulture), AccessToken = key.AccessToken,
DateCreated = key.DateCreated, DateCreated = key.DateCreated,
DeviceId = string.Empty, DeviceId = string.Empty,
DeviceName = string.Empty, DeviceName = string.Empty,
@ -52,7 +50,7 @@ namespace Jellyfin.Server.Implementations.Security
} }
/// <inheritdoc /> /// <inheritdoc />
public async Task DeleteApiKey(Guid accessToken) public async Task DeleteApiKey(string accessToken)
{ {
await using var dbContext = _dbProvider.CreateContext(); await using var dbContext = _dbProvider.CreateContext();

View File

@ -187,6 +187,19 @@ namespace Jellyfin.Server.Implementations.Security
await dbContext.SaveChangesAsync().ConfigureAwait(false); 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; return authInfo;
} }

View File

@ -61,7 +61,7 @@ namespace Jellyfin.Server.Migrations.Routines
{ {
dbContext.ApiKeys.Add(new ApiKey(row[3].ToString()) dbContext.ApiKeys.Add(new ApiKey(row[3].ToString())
{ {
AccessToken = row[1].ToGuid(), AccessToken = row[1].ToString(),
DateCreated = row[9].ToDateTime(), DateCreated = row[9].ToDateTime(),
DateLastActivity = row[10].ToDateTime() DateLastActivity = row[10].ToDateTime()
}); });

View File

@ -29,6 +29,6 @@ namespace MediaBrowser.Controller.Security
/// </summary> /// </summary>
/// <param name="accessToken">The access token.</param> /// <param name="accessToken">The access token.</param>
/// <returns>A task representing the deletion of the API key.</returns> /// <returns>A task representing the deletion of the API key.</returns>
Task DeleteApiKey(Guid accessToken); Task DeleteApiKey(string accessToken);
} }
} }