Testing the push method (TODO coherence between return code a physical result)

This commit is contained in:
2024-03-09 21:43:56 +00:00
parent e69462999e
commit 89e1b5a235
10 changed files with 191 additions and 78 deletions

View File

@ -0,0 +1,48 @@
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<IsndSettings> isndSettingsOptions)
{
this.dbContext = dbContext;
isndSettings = isndSettingsOptions.Value;
protector = dataProtectionProvider.CreateProtector(isndSettings.ProtectionTitle);
}
public async Task<ApiKey> 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<ApiKey> GetUserKeys(string identityName)
{
return dbContext.ApiKeys.Include(k => k.User).Where(k => k.User.UserName == identityName);
}
}