| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Linq; |
| | | 3 | | using System.Threading.Tasks; |
| | | 4 | | using Jellyfin.Database.Implementations; |
| | | 5 | | using Jellyfin.Database.Implementations.Entities.Security; |
| | | 6 | | using MediaBrowser.Controller.Security; |
| | | 7 | | using Microsoft.EntityFrameworkCore; |
| | | 8 | | |
| | | 9 | | namespace Jellyfin.Server.Implementations.Security |
| | | 10 | | { |
| | | 11 | | /// <inheritdoc /> |
| | | 12 | | public class AuthenticationManager : IAuthenticationManager |
| | | 13 | | { |
| | | 14 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Initializes a new instance of the <see cref="AuthenticationManager"/> class. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="dbProvider">The database provider.</param> |
| | | 20 | | public AuthenticationManager(IDbContextFactory<JellyfinDbContext> dbProvider) |
| | | 21 | | { |
| | 0 | 22 | | _dbProvider = dbProvider; |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public async Task CreateApiKey(string name) |
| | | 27 | | { |
| | | 28 | | var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); |
| | | 29 | | await using (dbContext.ConfigureAwait(false)) |
| | | 30 | | { |
| | | 31 | | dbContext.ApiKeys.Add(new ApiKey(name)); |
| | | 32 | | |
| | | 33 | | await dbContext.SaveChangesAsync().ConfigureAwait(false); |
| | | 34 | | } |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | | 38 | | public async Task<IReadOnlyList<AuthenticationInfo>> GetApiKeys() |
| | | 39 | | { |
| | | 40 | | var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); |
| | | 41 | | await using (dbContext.ConfigureAwait(false)) |
| | | 42 | | { |
| | | 43 | | return await dbContext.ApiKeys |
| | | 44 | | .Select(key => new AuthenticationInfo |
| | | 45 | | { |
| | | 46 | | AppName = key.Name, |
| | | 47 | | AccessToken = key.AccessToken, |
| | | 48 | | DateCreated = key.DateCreated, |
| | | 49 | | DeviceId = string.Empty, |
| | | 50 | | DeviceName = string.Empty, |
| | | 51 | | AppVersion = string.Empty |
| | | 52 | | }).ToListAsync().ConfigureAwait(false); |
| | | 53 | | } |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | | 57 | | public async Task DeleteApiKey(string accessToken) |
| | | 58 | | { |
| | | 59 | | var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); |
| | | 60 | | await using (dbContext.ConfigureAwait(false)) |
| | | 61 | | { |
| | | 62 | | await dbContext.ApiKeys |
| | | 63 | | .Where(apiKey => apiKey.AccessToken == accessToken) |
| | | 64 | | .ExecuteDeleteAsync() |
| | | 65 | | .ConfigureAwait(false); |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | } |
| | | 69 | | } |