using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Jellyfin.Data.Entities.Security;
using MediaBrowser.Controller.Security;
using Microsoft.EntityFrameworkCore;
namespace Jellyfin.Server.Implementations.Security
{
///
public class AuthenticationManager : IAuthenticationManager
{
private readonly IDbContextFactory _dbProvider;
///
/// Initializes a new instance of the class.
///
/// The database provider.
public AuthenticationManager(IDbContextFactory dbProvider)
{
_dbProvider = dbProvider;
}
///
public async Task CreateApiKey(string name)
{
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
await using (dbContext.ConfigureAwait(false))
{
dbContext.ApiKeys.Add(new ApiKey(name));
await dbContext.SaveChangesAsync().ConfigureAwait(false);
}
}
///
public async Task> 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);
}
}
///
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);
}
}
}
}