2020-03-19 11:11:15 +01:00
|
|
|
using System.Linq;
|
2020-06-28 10:55:27 +02:00
|
|
|
using System.Threading.Tasks;
|
2020-03-19 11:11:15 +01: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;
|
2020-06-28 10:55:27 +02: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;
|
2020-03-27 06:17:31 +01:00
|
|
|
using NBitcoin;
|
2020-06-28 10:55:27 +02:00
|
|
|
using NBitcoin.DataEncoders;
|
2020-02-24 18:43:28 +01:00
|
|
|
|
2020-03-27 04:58:45 +01:00
|
|
|
namespace BTCPayServer.Controllers.GreenField
|
2020-02-24 18:43:28 +01:00
|
|
|
{
|
|
|
|
[ApiController]
|
2020-03-27 05:13:40 +01:00
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.GreenfieldAPIKeys)]
|
2020-06-30 08:26:19 +02:00
|
|
|
[EnableCors(CorsPolicies.All)]
|
2020-02-24 18:43:28 +01:00
|
|
|
public class ApiKeysController : ControllerBase
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2020-03-02 16:50:28 +01:00
|
|
|
public ApiKeysController(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 10:55:27 +02:00
|
|
|
|
2020-02-24 18:43:28 +01:00
|
|
|
[HttpGet("~/api/v1/api-keys/current")]
|
|
|
|
public async Task<ActionResult<ApiKeyData>> GetKey()
|
|
|
|
{
|
2020-03-23 21:18:02 +01:00
|
|
|
if (!ControllerContext.HttpContext.GetAPIKey(out var apiKey))
|
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
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 12:08:09 +01:00
|
|
|
|
2020-03-27 06:17:31 +01:00
|
|
|
[HttpPost("~/api/v1/api-keys")]
|
|
|
|
[Authorize(Policy = Policies.Unrestricted, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
2020-06-27 07:05:56 +02:00
|
|
|
public async Task<IActionResult> CreateKey(CreateApiKeyRequest request)
|
2020-03-27 06:17:31 +01:00
|
|
|
{
|
|
|
|
if (request is null)
|
2020-06-08 16:40:58 +02:00
|
|
|
return NotFound();
|
2020-07-16 10:26:04 +02:00
|
|
|
request.Permissions ??= System.Array.Empty<Permission>();
|
2020-03-27 06:17:31 +01:00
|
|
|
var key = new APIKeyData()
|
|
|
|
{
|
|
|
|
Id = Encoders.Hex.EncodeData(RandomUtils.GetBytes(20)),
|
|
|
|
Type = APIKeyType.Permanent,
|
|
|
|
UserId = _userManager.GetUserId(User),
|
|
|
|
Label = request.Label
|
|
|
|
};
|
2020-04-02 08:59:20 +02:00
|
|
|
key.SetBlob(new APIKeyBlob()
|
|
|
|
{
|
|
|
|
Permissions = request.Permissions.Select(p => p.ToString()).Distinct().ToArray()
|
|
|
|
});
|
2020-03-27 06:17:31 +01:00
|
|
|
await _apiKeyRepository.CreateKey(key);
|
|
|
|
return Ok(FromModel(key));
|
|
|
|
}
|
|
|
|
|
2020-03-02 16:50:28 +01:00
|
|
|
[HttpDelete("~/api/v1/api-keys/current")]
|
2020-03-27 06:46:51 +01: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 06:46:51 +01:00
|
|
|
// Should be impossible (we force apikey auth)
|
|
|
|
return Task.FromResult<IActionResult>(BadRequest());
|
2020-03-23 21:18:02 +01:00
|
|
|
}
|
2020-03-27 06:46:51 +01:00
|
|
|
return RevokeKey(apiKey);
|
2020-03-02 16:50:28 +01:00
|
|
|
}
|
2020-03-27 06:46:51 +01:00
|
|
|
[HttpDelete("~/api/v1/api-keys/{apikey}", Order = 1)]
|
|
|
|
[Authorize(Policy = Policies.Unrestricted, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
public async Task<IActionResult> RevokeKey(string apikey)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(apikey))
|
2020-06-08 16:40:58 +02:00
|
|
|
return NotFound();
|
2020-03-27 06:46:51 +01:00
|
|
|
if (await _apiKeyRepository.Remove(apikey, _userManager.GetUserId(User)))
|
|
|
|
return Ok();
|
|
|
|
else
|
|
|
|
return NotFound();
|
|
|
|
}
|
|
|
|
|
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 12:00:05 +01:00
|
|
|
Label = data.Label ?? string.Empty
|
2020-03-02 16:50:28 +01:00
|
|
|
};
|
2020-02-24 18:43:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|