Testing the push method (TODO coherence between return code a physical result)
This commit is contained in:
48
src/isnd/Services/ApiKeyProvider.cs
Normal file
48
src/isnd/Services/ApiKeyProvider.cs
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user