mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-18 06:24:24 +01:00
This lets the authorize api key screen redirect to the defined url and provide it with the user id, permissions granted and the key. This also allows apps to match existing api keys generated for it specifically using the application identifier, and if matched, presented with a confirmation page before redirection.
85 lines
2.9 KiB
C#
85 lines
2.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using BTCPayServer.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace BTCPayServer.Security.GreenField
|
|
{
|
|
public class APIKeyRepository
|
|
{
|
|
private readonly ApplicationDbContextFactory _applicationDbContextFactory;
|
|
|
|
public APIKeyRepository(ApplicationDbContextFactory applicationDbContextFactory)
|
|
{
|
|
_applicationDbContextFactory = applicationDbContextFactory;
|
|
}
|
|
|
|
public async Task<APIKeyData> GetKey(string apiKey)
|
|
{
|
|
using (var context = _applicationDbContextFactory.CreateContext())
|
|
{
|
|
return await EntityFrameworkQueryableExtensions.SingleOrDefaultAsync(context.ApiKeys,
|
|
data => data.Id == apiKey && data.Type != APIKeyType.Legacy);
|
|
}
|
|
}
|
|
|
|
public async Task<List<APIKeyData>> GetKeys(APIKeyQuery query)
|
|
{
|
|
using (var context = _applicationDbContextFactory.CreateContext())
|
|
{
|
|
var queryable = context.ApiKeys.AsQueryable();
|
|
if (query != null)
|
|
{
|
|
if (query.UserId != null && query.UserId.Any())
|
|
{
|
|
queryable = queryable.Where(data => query.UserId.Contains(data.UserId));
|
|
}
|
|
|
|
if (query.ApplicationIdentifier != null && query.ApplicationIdentifier.Any())
|
|
{
|
|
queryable = queryable.Where(data =>
|
|
query.ApplicationIdentifier.Contains(data.ApplicationIdentifier));
|
|
}
|
|
}
|
|
|
|
return await queryable.ToListAsync();
|
|
}
|
|
}
|
|
|
|
public async Task CreateKey(APIKeyData key)
|
|
{
|
|
if (key.Type == APIKeyType.Legacy || !string.IsNullOrEmpty(key.StoreId) || string.IsNullOrEmpty(key.UserId))
|
|
{
|
|
throw new InvalidOperationException("cannot save a bitpay legacy api key with this repository");
|
|
}
|
|
|
|
using (var context = _applicationDbContextFactory.CreateContext())
|
|
{
|
|
await context.ApiKeys.AddAsync(key);
|
|
await context.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
public async Task<bool> Remove(string id, string getUserId)
|
|
{
|
|
using (var context = _applicationDbContextFactory.CreateContext())
|
|
{
|
|
var key = await EntityFrameworkQueryableExtensions.SingleOrDefaultAsync(context.ApiKeys,
|
|
data => data.Id == id && data.UserId == getUserId);
|
|
if (key == null)
|
|
return false;
|
|
context.ApiKeys.Remove(key);
|
|
await context.SaveChangesAsync();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public class APIKeyQuery
|
|
{
|
|
public string[] UserId { get; set; }
|
|
}
|
|
}
|
|
}
|