2017-09-13 08:47:34 +02:00
|
|
|
|
using BTCPayServer.Authentication;
|
|
|
|
|
using BTCPayServer.Filters;
|
|
|
|
|
using BTCPayServer.Models;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using NBitcoin.DataEncoders;
|
|
|
|
|
using NBitpayClient;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers
|
|
|
|
|
{
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public class AccessTokenController : Controller
|
|
|
|
|
{
|
|
|
|
|
TokenRepository _TokenRepository;
|
|
|
|
|
public AccessTokenController(TokenRepository tokenRepository)
|
|
|
|
|
{
|
|
|
|
|
_TokenRepository = tokenRepository ?? throw new ArgumentNullException(nameof(tokenRepository));
|
|
|
|
|
}
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("tokens")]
|
|
|
|
|
public async Task<GetTokensResponse> Tokens()
|
|
|
|
|
{
|
|
|
|
|
var tokens = await _TokenRepository.GetTokens(this.GetBitIdentity().SIN);
|
|
|
|
|
return new GetTokensResponse(tokens);
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("tokens")]
|
|
|
|
|
public async Task<DataWrapper<List<PairingCodeResponse>>> Tokens([FromBody] TokenRequest request)
|
|
|
|
|
{
|
|
|
|
|
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");
|
|
|
|
|
if (string.IsNullOrEmpty(request.Facade))
|
|
|
|
|
throw new BitpayHttpException(400, "'facade' 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,
|
|
|
|
|
Facade = request.Facade,
|
|
|
|
|
Label = request.Label
|
|
|
|
|
});
|
2017-10-11 05:20:44 +02:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var sin = this.GetBitIdentity(false)?.SIN ?? request.Id;
|
|
|
|
|
if (string.IsNullOrEmpty(request.Id) || !NBitpayClient.Extensions.BitIdExtensions.ValidateSIN(request.Id))
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
PairingCode = pairingEntity.Id,
|
|
|
|
|
PairingExpiration = pairingEntity.Expiration,
|
|
|
|
|
DateCreated = pairingEntity.CreatedTime,
|
|
|
|
|
Facade = pairingEntity.Facade,
|
|
|
|
|
Token = pairingEntity.TokenValue,
|
|
|
|
|
Label = pairingEntity.Label
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return DataWrapper.Create(pairingCodes);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
}
|