mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-10-26 16:45:09 +00:00
* Return the created API key from POST /Auth/Keys * Keep the other API key fields in the DTO
81 lines
2.9 KiB
C#
81 lines
2.9 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Jellyfin.Database.Implementations;
|
|
using Jellyfin.Database.Implementations.Entities.Security;
|
|
using MediaBrowser.Controller.Security;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Jellyfin.Server.Implementations.Security
|
|
{
|
|
/// <inheritdoc />
|
|
public class AuthenticationManager : IAuthenticationManager
|
|
{
|
|
private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="AuthenticationManager"/> class.
|
|
/// </summary>
|
|
/// <param name="dbProvider">The database provider.</param>
|
|
public AuthenticationManager(IDbContextFactory<JellyfinDbContext> dbProvider)
|
|
{
|
|
_dbProvider = dbProvider;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<AuthenticationInfo> CreateApiKey(string name)
|
|
{
|
|
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
|
|
await using (dbContext.ConfigureAwait(false))
|
|
{
|
|
var apiKey = new ApiKey(name);
|
|
dbContext.ApiKeys.Add(apiKey);
|
|
|
|
await dbContext.SaveChangesAsync().ConfigureAwait(false);
|
|
|
|
return new AuthenticationInfo
|
|
{
|
|
AppName = apiKey.Name,
|
|
AccessToken = apiKey.AccessToken,
|
|
DateCreated = apiKey.DateCreated,
|
|
DeviceId = string.Empty,
|
|
DeviceName = string.Empty,
|
|
AppVersion = string.Empty
|
|
};
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IReadOnlyList<AuthenticationInfo>> GetApiKeys()
|
|
{
|
|
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
|
|
await using (dbContext.ConfigureAwait(false))
|
|
{
|
|
return await dbContext.ApiKeys
|
|
.Select(key => new AuthenticationInfo
|
|
{
|
|
AppName = key.Name,
|
|
AccessToken = key.AccessToken,
|
|
DateCreated = key.DateCreated,
|
|
DeviceId = string.Empty,
|
|
DeviceName = string.Empty,
|
|
AppVersion = string.Empty
|
|
}).ToListAsync().ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task DeleteApiKey(string accessToken)
|
|
{
|
|
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
|
|
await using (dbContext.ConfigureAwait(false))
|
|
{
|
|
await dbContext.ApiKeys
|
|
.Where(apiKey => apiKey.AccessToken == accessToken)
|
|
.ExecuteDeleteAsync()
|
|
.ConfigureAwait(false);
|
|
}
|
|
}
|
|
}
|
|
}
|