| | | 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 | | { |
| | 2 | 22 | | _dbProvider = dbProvider; |
| | 2 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public async Task<AuthenticationInfo> CreateApiKey(string name) |
| | | 27 | | { |
| | 3 | 28 | | var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); |
| | 3 | 29 | | await using (dbContext.ConfigureAwait(false)) |
| | | 30 | | { |
| | 3 | 31 | | var apiKey = new ApiKey(name); |
| | 3 | 32 | | dbContext.ApiKeys.Add(apiKey); |
| | | 33 | | |
| | 3 | 34 | | await dbContext.SaveChangesAsync().ConfigureAwait(false); |
| | | 35 | | |
| | 3 | 36 | | return new AuthenticationInfo |
| | 3 | 37 | | { |
| | 3 | 38 | | AppName = apiKey.Name, |
| | 3 | 39 | | AccessToken = apiKey.AccessToken, |
| | 3 | 40 | | DateCreated = apiKey.DateCreated, |
| | 3 | 41 | | DeviceId = string.Empty, |
| | 3 | 42 | | DeviceName = string.Empty, |
| | 3 | 43 | | AppVersion = string.Empty |
| | 3 | 44 | | }; |
| | | 45 | | } |
| | 3 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | | 49 | | public async Task<IReadOnlyList<AuthenticationInfo>> GetApiKeys() |
| | | 50 | | { |
| | 1 | 51 | | var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); |
| | 1 | 52 | | await using (dbContext.ConfigureAwait(false)) |
| | | 53 | | { |
| | 1 | 54 | | return await dbContext.ApiKeys |
| | 1 | 55 | | .Select(key => new AuthenticationInfo |
| | 1 | 56 | | { |
| | 1 | 57 | | AppName = key.Name, |
| | 1 | 58 | | AccessToken = key.AccessToken, |
| | 1 | 59 | | DateCreated = key.DateCreated, |
| | 1 | 60 | | DeviceId = string.Empty, |
| | 1 | 61 | | DeviceName = string.Empty, |
| | 1 | 62 | | AppVersion = string.Empty |
| | 1 | 63 | | }).ToListAsync().ConfigureAwait(false); |
| | | 64 | | } |
| | 1 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <inheritdoc /> |
| | | 68 | | public async Task DeleteApiKey(string accessToken) |
| | | 69 | | { |
| | 0 | 70 | | var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); |
| | 0 | 71 | | await using (dbContext.ConfigureAwait(false)) |
| | | 72 | | { |
| | 0 | 73 | | await dbContext.ApiKeys |
| | 0 | 74 | | .Where(apiKey => apiKey.AccessToken == accessToken) |
| | 0 | 75 | | .ExecuteDeleteAsync() |
| | 0 | 76 | | .ConfigureAwait(false); |
| | | 77 | | } |
| | 0 | 78 | | } |
| | | 79 | | } |
| | | 80 | | } |