2020-02-24 18:43:28 +01:00
|
|
|
using System.Threading.Tasks;
|
2020-03-19 11:11:15 +01:00
|
|
|
using System.Linq;
|
|
|
|
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;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
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 04:55:21 +01:00
|
|
|
using BTCPayServer.Security.GreenField;
|
2020-03-27 06:17:31 +01:00
|
|
|
using NBitcoin.DataEncoders;
|
|
|
|
using NBitcoin;
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
[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)]
|
|
|
|
public async Task<ActionResult<ApiKeyData>> CreateKey(CreateApiKeyRequest request)
|
|
|
|
{
|
|
|
|
if (request is null)
|
|
|
|
return BadRequest();
|
|
|
|
var key = new APIKeyData()
|
|
|
|
{
|
|
|
|
Id = Encoders.Hex.EncodeData(RandomUtils.GetBytes(20)),
|
|
|
|
Type = APIKeyType.Permanent,
|
|
|
|
UserId = _userManager.GetUserId(User),
|
|
|
|
Label = request.Label
|
|
|
|
};
|
|
|
|
key.Permissions = string.Join(";", request.Permissions.Select(p => p.ToString()).Distinct().ToArray());
|
|
|
|
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 05:13:40 +01:00
|
|
|
[Authorize(Policy = Policies.Unrestricted, AuthenticationSchemes = AuthenticationSchemes.GreenfieldAPIKeys)]
|
2020-03-27 06:17:31 +01:00
|
|
|
public async Task<IActionResult> RevokeKey()
|
2020-03-02 16:50:28 +01:00
|
|
|
{
|
2020-03-23 21:18:02 +01:00
|
|
|
if (!ControllerContext.HttpContext.GetAPIKey(out var apiKey))
|
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
2020-03-02 16:50:28 +01:00
|
|
|
await _apiKeyRepository.Remove(apiKey, _userManager.GetUserId(User));
|
|
|
|
return Ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ApiKeyData FromModel(APIKeyData data)
|
|
|
|
{
|
|
|
|
return new ApiKeyData()
|
|
|
|
{
|
2020-03-20 06:01:51 +01:00
|
|
|
Permissions = Permission.ToPermissions(data.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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|