2020-06-29 04:44:35 +02:00
|
|
|
using System;
|
2020-06-28 10:55:27 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading.Tasks;
|
2020-11-17 13:46:23 +01:00
|
|
|
using BTCPayServer.Abstractions.Constants;
|
2020-06-28 10:55:27 +02:00
|
|
|
using BTCPayServer.Filters;
|
2017-09-13 08:47:34 +02:00
|
|
|
using BTCPayServer.Models;
|
2019-10-18 17:54:20 +02:00
|
|
|
using BTCPayServer.Security.Bitpay;
|
2017-09-13 08:47:34 +02:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers
|
|
|
|
{
|
2020-11-17 13:46:23 +01:00
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Bitpay)]
|
2019-02-02 07:19:22 +01:00
|
|
|
[BitpayAPIConstraint()]
|
2017-10-27 10:53:04 +02:00
|
|
|
public class AccessTokenController : Controller
|
|
|
|
{
|
2020-06-29 05:07:48 +02:00
|
|
|
readonly TokenRepository _TokenRepository;
|
2017-10-27 10:53:04 +02:00
|
|
|
public AccessTokenController(TokenRepository tokenRepository)
|
|
|
|
{
|
|
|
|
_TokenRepository = tokenRepository ?? throw new ArgumentNullException(nameof(tokenRepository));
|
|
|
|
}
|
|
|
|
[HttpGet]
|
|
|
|
[Route("tokens")]
|
|
|
|
public async Task<GetTokensResponse> Tokens()
|
|
|
|
{
|
2018-04-27 19:09:24 +02:00
|
|
|
var tokens = await _TokenRepository.GetTokens(this.User.GetSIN());
|
2017-10-27 10:53:04 +02:00
|
|
|
return new GetTokensResponse(tokens);
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
[HttpPost]
|
|
|
|
[Route("tokens")]
|
2018-06-06 07:46:41 +02:00
|
|
|
[AllowAnonymous]
|
2017-10-27 10:53:04 +02:00
|
|
|
public async Task<DataWrapper<List<PairingCodeResponse>>> Tokens([FromBody] TokenRequest request)
|
|
|
|
{
|
2019-03-10 07:10:30 +01:00
|
|
|
if (request == null)
|
|
|
|
throw new BitpayHttpException(400, "The request body is missing");
|
2017-10-27 10:53:04 +02:00
|
|
|
PairingCodeEntity pairingEntity = null;
|
|
|
|
if (string.IsNullOrEmpty(request.PairingCode))
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(request.Id) || !NBitpayClient.Extensions.BitIdExtensions.ValidateSIN(request.Id))
|
|
|
|
throw new BitpayHttpException(400, "'id' property is required");
|
2017-09-13 08:47:34 +02:00
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
var pairingCode = await _TokenRepository.CreatePairingCodeAsync();
|
|
|
|
await _TokenRepository.PairWithSINAsync(pairingCode, request.Id);
|
|
|
|
pairingEntity = await _TokenRepository.UpdatePairingCode(new PairingCodeEntity()
|
|
|
|
{
|
|
|
|
Id = pairingCode,
|
|
|
|
Label = request.Label
|
|
|
|
});
|
2017-10-11 05:20:44 +02:00
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-27 19:09:24 +02:00
|
|
|
var sin = this.User.GetSIN() ?? request.Id;
|
2018-06-06 07:46:41 +02:00
|
|
|
if (string.IsNullOrEmpty(sin) || !NBitpayClient.Extensions.BitIdExtensions.ValidateSIN(sin))
|
2017-10-27 10:53:04 +02:00
|
|
|
throw new BitpayHttpException(400, "'id' property is required, alternatively, use BitId");
|
2017-10-11 05:20:44 +02:00
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
pairingEntity = await _TokenRepository.GetPairingAsync(request.PairingCode);
|
|
|
|
if (pairingEntity == null)
|
|
|
|
throw new BitpayHttpException(404, "The specified pairingCode is not found");
|
|
|
|
pairingEntity.SIN = sin;
|
2017-10-13 11:06:46 +02:00
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
if (string.IsNullOrEmpty(pairingEntity.Label) && !string.IsNullOrEmpty(request.Label))
|
|
|
|
{
|
|
|
|
pairingEntity.Label = request.Label;
|
|
|
|
await _TokenRepository.UpdatePairingCode(pairingEntity);
|
|
|
|
}
|
2017-10-18 11:43:52 +02:00
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
var result = await _TokenRepository.PairWithSINAsync(request.PairingCode, sin);
|
|
|
|
if (result != PairingResult.Complete && result != PairingResult.Partial)
|
|
|
|
throw new BitpayHttpException(400, $"Error while pairing ({result})");
|
2017-10-11 05:20:44 +02:00
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var pairingCodes = new List<PairingCodeResponse>
|
|
|
|
{
|
|
|
|
new PairingCodeResponse()
|
|
|
|
{
|
2018-05-26 11:26:02 +02:00
|
|
|
Policies = new Newtonsoft.Json.Linq.JArray(),
|
2017-10-27 10:53:04 +02:00
|
|
|
PairingCode = pairingEntity.Id,
|
|
|
|
PairingExpiration = pairingEntity.Expiration,
|
|
|
|
DateCreated = pairingEntity.CreatedTime,
|
2019-04-11 16:54:58 +02:00
|
|
|
Facade = "merchant",
|
2017-10-27 10:53:04 +02:00
|
|
|
Token = pairingEntity.TokenValue,
|
|
|
|
Label = pairingEntity.Label
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return DataWrapper.Create(pairingCodes);
|
|
|
|
}
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
}
|