48 lines
1.4 KiB
C#
48 lines
1.4 KiB
C#
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);
|
|
}
|
|
} |