using System; using System.Linq; using System.Threading.Tasks; using isnd.Data; using isnd.Data.ApiKeys; using isnd.Entities; using isnd.Interfaces; using Microsoft.AspNetCore.DataProtection; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; namespace isnd; public class ApiKeyProvider : IApiKeyProvider { private readonly IsndSettings isndSettings; private readonly IDataProtector protector; private readonly ApplicationDbContext dbContext; public ApiKeyProvider( ApplicationDbContext dbContext, IDataProtectionProvider dataProtectionProvider, IOptions isndSettingsOptions) { this.dbContext = dbContext; isndSettings = isndSettingsOptions.Value; protector = dataProtectionProvider.CreateProtector(isndSettings.ProtectionTitle); } public async Task CreateApiKeyAsync(CreateModel model) { var newKey = new ApiKey{ UserId = model.UserId, CreationDate = DateTime.Now, Name = model.Name, ValidityPeriodInDays = model.ValidityPeriodInDays }; _ = dbContext.ApiKeys.Add(newKey); _ = await dbContext.SaveChangesAsync(); return newKey; } public IQueryable GetUserKeys(string identityName) { return dbContext.ApiKeys.Include(k => k.User).Where(k => k.User.UserName == identityName); } }