Testing the push method (TODO coherence between return code a physical result)
parent
e69462999e
commit
89e1b5a235
@ -1,47 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace isn
|
|
||||||
{
|
|
||||||
public interface IDataProtector
|
|
||||||
{
|
|
||||||
string Protect(string data);
|
|
||||||
string UnProtect(string data);
|
|
||||||
}
|
|
||||||
|
|
||||||
public class DefaultDataProtector : IDataProtector
|
|
||||||
{
|
|
||||||
private byte delta = 145;
|
|
||||||
|
|
||||||
public DefaultDataProtector()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public string Protect(string data)
|
|
||||||
{
|
|
||||||
List<Byte> protd = new List<byte>();
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
foreach (byte c in Encoding.UTF8.GetBytes(data))
|
|
||||||
{
|
|
||||||
protd.Add((byte) (c ^ delta));
|
|
||||||
}
|
|
||||||
return System.Convert.ToBase64String(protd.ToArray());
|
|
||||||
}
|
|
||||||
|
|
||||||
public string UnProtect(string data)
|
|
||||||
{
|
|
||||||
if (data==null) return null;
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
List<byte> unps = new List<byte>();
|
|
||||||
|
|
||||||
foreach (byte c in System.Convert.FromBase64CharArray(data.ToCharArray(),0,data.Length))
|
|
||||||
{
|
|
||||||
unps.Add((byte) (c ^ delta));
|
|
||||||
}
|
|
||||||
return Encoding.UTF8.GetString(unps.ToArray());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,13 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using isnd.Data.ApiKeys;
|
||||||
|
|
||||||
|
|
||||||
|
namespace isnd.Interfaces
|
||||||
|
{
|
||||||
|
public interface IApiKeyProvider
|
||||||
|
{
|
||||||
|
Task<ApiKey> CreateApiKeyAsync(CreateModel model);
|
||||||
|
IQueryable<ApiKey> GetUserKeys(string identityName);
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue