2020-03-19 19:11:15 +09:00
|
|
|
using System.Linq;
|
2020-06-28 17:55:27 +09:00
|
|
|
using System.Threading.Tasks;
|
2020-11-17 13:46:23 +01:00
|
|
|
using BTCPayServer.Abstractions.Constants;
|
2022-02-24 09:00:44 +01:00
|
|
|
using BTCPayServer.Abstractions.Extensions;
|
2020-03-19 19:11:15 +09:00
|
|
|
using BTCPayServer.Client;
|
2020-03-02 16:50:28 +01:00
|
|
|
using BTCPayServer.Client.Models;
|
|
|
|
using BTCPayServer.Data;
|
2020-02-24 18:43:28 +01:00
|
|
|
using BTCPayServer.Security;
|
2022-01-14 13:05:23 +09:00
|
|
|
using BTCPayServer.Security.Greenfield;
|
2020-02-24 18:43:28 +01:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2020-06-30 08:26:19 +02:00
|
|
|
using Microsoft.AspNetCore.Cors;
|
2020-03-02 16:50:28 +01:00
|
|
|
using Microsoft.AspNetCore.Identity;
|
2020-02-24 18:43:28 +01:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-03-03 20:30:54 +09:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2020-03-27 14:17:31 +09:00
|
|
|
using NBitcoin;
|
2020-06-28 17:55:27 +09:00
|
|
|
using NBitcoin.DataEncoders;
|
2020-02-24 18:43:28 +01:00
|
|
|
|
2022-01-14 13:05:23 +09:00
|
|
|
namespace BTCPayServer.Controllers.Greenfield
|
2020-02-24 18:43:28 +01:00
|
|
|
{
|
|
|
|
[ApiController]
|
2020-03-27 13:13:40 +09:00
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.GreenfieldAPIKeys)]
|
2020-06-30 08:26:19 +02:00
|
|
|
[EnableCors(CorsPolicies.All)]
|
2022-01-07 12:17:59 +09:00
|
|
|
public class GreenfieldApiKeysController : ControllerBase
|
2020-02-24 18:43:28 +01:00
|
|
|
{
|
|
|
|
private readonly APIKeyRepository _apiKeyRepository;
|
2020-03-02 16:50:28 +01:00
|
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
2020-02-24 18:43:28 +01:00
|
|
|
|
2022-01-07 12:17:59 +09:00
|
|
|
public GreenfieldApiKeysController(APIKeyRepository apiKeyRepository, UserManager<ApplicationUser> userManager)
|
2020-02-24 18:43:28 +01:00
|
|
|
{
|
|
|
|
_apiKeyRepository = apiKeyRepository;
|
2020-03-02 16:50:28 +01:00
|
|
|
_userManager = userManager;
|
2020-02-24 18:43:28 +01:00
|
|
|
}
|
2020-06-28 17:55:27 +09:00
|
|
|
|
2020-02-24 18:43:28 +01:00
|
|
|
[HttpGet("~/api/v1/api-keys/current")]
|
2021-12-23 05:32:08 +01:00
|
|
|
public async Task<IActionResult> GetKey()
|
2020-02-24 18:43:28 +01:00
|
|
|
{
|
2020-03-23 21:18:02 +01:00
|
|
|
if (!ControllerContext.HttpContext.GetAPIKey(out var apiKey))
|
|
|
|
{
|
2021-12-23 05:32:08 +01:00
|
|
|
return
|
|
|
|
this.CreateAPIError(404, "api-key-not-found", "The api key was not present.");
|
2020-03-23 21:18:02 +01:00
|
|
|
}
|
2020-02-24 18:43:28 +01:00
|
|
|
var data = await _apiKeyRepository.GetKey(apiKey);
|
2020-03-02 16:50:28 +01:00
|
|
|
return Ok(FromModel(data));
|
|
|
|
}
|
2020-03-18 20:08:09 +09:00
|
|
|
|
2020-03-27 14:17:31 +09:00
|
|
|
[HttpPost("~/api/v1/api-keys")]
|
|
|
|
[Authorize(Policy = Policies.Unrestricted, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
2023-02-24 16:19:03 +09:00
|
|
|
public Task<IActionResult> CreateAPIKey(CreateApiKeyRequest request)
|
|
|
|
{
|
|
|
|
return CreateUserAPIKey(_userManager.GetUserId(User), request);
|
|
|
|
}
|
|
|
|
|
2023-03-03 21:24:27 +09:00
|
|
|
[HttpPost("~/api/v1/users/{idOrEmail}/api-keys")]
|
2023-02-24 16:19:03 +09:00
|
|
|
[Authorize(Policy = Policies.CanManageUsers, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
2023-03-03 21:24:27 +09:00
|
|
|
public async Task<IActionResult> CreateUserAPIKey(string idOrEmail, CreateApiKeyRequest request)
|
2020-03-27 14:17:31 +09:00
|
|
|
{
|
2021-12-23 05:32:08 +01:00
|
|
|
request ??= new CreateApiKeyRequest();
|
2020-07-16 10:26:04 +02:00
|
|
|
request.Permissions ??= System.Array.Empty<Permission>();
|
2023-03-03 21:24:27 +09:00
|
|
|
|
|
|
|
var userId = (await _userManager.FindByIdOrEmail(idOrEmail))?.Id;
|
|
|
|
if (userId is null)
|
|
|
|
return this.UserNotFound();
|
2020-03-27 14:17:31 +09:00
|
|
|
var key = new APIKeyData()
|
|
|
|
{
|
|
|
|
Id = Encoders.Hex.EncodeData(RandomUtils.GetBytes(20)),
|
|
|
|
Type = APIKeyType.Permanent,
|
2023-02-24 16:19:03 +09:00
|
|
|
UserId = userId,
|
2020-03-27 14:17:31 +09:00
|
|
|
Label = request.Label
|
|
|
|
};
|
2020-04-02 08:59:20 +02:00
|
|
|
key.SetBlob(new APIKeyBlob()
|
|
|
|
{
|
|
|
|
Permissions = request.Permissions.Select(p => p.ToString()).Distinct().ToArray()
|
|
|
|
});
|
2023-03-03 21:24:27 +09:00
|
|
|
await _apiKeyRepository.CreateKey(key);
|
2020-03-27 14:17:31 +09:00
|
|
|
return Ok(FromModel(key));
|
|
|
|
}
|
|
|
|
|
2020-03-02 16:50:28 +01:00
|
|
|
[HttpDelete("~/api/v1/api-keys/current")]
|
2020-03-27 14:46:51 +09:00
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.GreenfieldAPIKeys)]
|
|
|
|
public Task<IActionResult> RevokeCurrentKey()
|
2020-03-02 16:50:28 +01:00
|
|
|
{
|
2020-03-23 21:18:02 +01:00
|
|
|
if (!ControllerContext.HttpContext.GetAPIKey(out var apiKey))
|
|
|
|
{
|
2020-03-27 14:46:51 +09:00
|
|
|
// Should be impossible (we force apikey auth)
|
|
|
|
return Task.FromResult<IActionResult>(BadRequest());
|
2020-03-23 21:18:02 +01:00
|
|
|
}
|
2023-02-24 16:19:03 +09:00
|
|
|
return RevokeAPIKey(apiKey);
|
2020-03-02 16:50:28 +01:00
|
|
|
}
|
2020-03-27 14:46:51 +09:00
|
|
|
[HttpDelete("~/api/v1/api-keys/{apikey}", Order = 1)]
|
|
|
|
[Authorize(Policy = Policies.Unrestricted, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
2023-02-24 16:19:03 +09:00
|
|
|
public Task<IActionResult> RevokeAPIKey(string apikey)
|
|
|
|
{
|
|
|
|
return RevokeAPIKey(_userManager.GetUserId(User), apikey);
|
|
|
|
}
|
|
|
|
|
2023-03-03 21:24:27 +09:00
|
|
|
[HttpDelete("~/api/v1/users/{idOrEmail}/api-keys/{apikey}", Order = 1)]
|
2023-02-24 16:19:03 +09:00
|
|
|
[Authorize(Policy = Policies.CanManageUsers, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
2023-03-03 21:24:27 +09:00
|
|
|
public async Task<IActionResult> RevokeAPIKey(string idOrEmail, string apikey)
|
2020-03-27 14:46:51 +09:00
|
|
|
{
|
2023-03-03 21:24:27 +09:00
|
|
|
var userId = (await _userManager.FindByIdOrEmail(idOrEmail))?.Id;
|
|
|
|
if (userId is null)
|
|
|
|
return this.UserNotFound();
|
2021-12-16 15:04:06 +01:00
|
|
|
if (!string.IsNullOrEmpty(apikey) &&
|
2023-02-24 16:19:03 +09:00
|
|
|
await _apiKeyRepository.Remove(apikey, userId))
|
2020-03-27 14:46:51 +09:00
|
|
|
return Ok();
|
|
|
|
else
|
2021-12-16 15:04:06 +01:00
|
|
|
return this.CreateAPIError("apikey-not-found", "This apikey does not exists");
|
2020-03-27 14:46:51 +09:00
|
|
|
}
|
|
|
|
|
2023-02-24 16:19:03 +09:00
|
|
|
|
2020-03-02 16:50:28 +01:00
|
|
|
private static ApiKeyData FromModel(APIKeyData data)
|
|
|
|
{
|
|
|
|
return new ApiKeyData()
|
|
|
|
{
|
2020-04-02 08:59:20 +02:00
|
|
|
Permissions = Permission.ToPermissions(data.GetBlob().Permissions).ToArray(),
|
2020-03-02 16:50:28 +01:00
|
|
|
ApiKey = data.Id,
|
2020-03-20 20:00:05 +09:00
|
|
|
Label = data.Label ?? string.Empty
|
2020-03-02 16:50:28 +01:00
|
|
|
};
|
2020-02-24 18:43:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|