-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
564 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System.Net; | ||
using BTCPayApp.Core.Auth; | ||
using BTCPayApp.VSS; | ||
using VSSProto; | ||
|
||
namespace BTCPayApp.Core.Backup; | ||
|
||
public class AccountAwareVssClient : IVSSAPI | ||
{ | ||
private readonly IVSSAPI _inner; | ||
private readonly IAccountManager _accountManager; | ||
|
||
public AccountAwareVssClient(IVSSAPI inner, IAccountManager accountManager) | ||
{ | ||
_inner = inner; | ||
_accountManager = accountManager; | ||
} | ||
|
||
private async Task<T> Wrap<T>(Func<Task<T>> func) | ||
{ | ||
var retry = false; | ||
attemptAgain: | ||
try | ||
{ | ||
return await func(); | ||
} | ||
catch (HttpRequestException ex) when (ex.StatusCode is HttpStatusCode.Unauthorized && !retry) | ||
{ | ||
await _accountManager.RefreshAccess(); | ||
retry = true; | ||
goto attemptAgain; | ||
} | ||
} | ||
|
||
public async Task<GetObjectResponse> GetObjectAsync(GetObjectRequest request, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return await Wrap(async () => await _inner.GetObjectAsync(request, cancellationToken)); | ||
} | ||
|
||
public async Task<PutObjectResponse> PutObjectAsync(PutObjectRequest request, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return await Wrap(async () => await _inner.PutObjectAsync(request, cancellationToken)); | ||
} | ||
|
||
public async Task<DeleteObjectResponse> DeleteObjectAsync(DeleteObjectRequest request, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return await Wrap(async () => await _inner.DeleteObjectAsync(request, cancellationToken)); | ||
} | ||
|
||
public async Task<ListKeyVersionsResponse> ListKeyVersionsAsync(ListKeyVersionsRequest request, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return await Wrap(async () => await _inner.ListKeyVersionsAsync(request, cancellationToken)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using Microsoft.AspNetCore.DataProtection; | ||
|
||
namespace BTCPayApp.Core.Backup; | ||
|
||
public class SingleKeyDataProtector : IDataProtector | ||
{ | ||
private readonly byte[] _key; | ||
|
||
public SingleKeyDataProtector(byte[] key) | ||
{ | ||
if (key.Length != 32) // AES-256 key size | ||
{ | ||
throw new ArgumentException("Key length must be 32 bytes."); | ||
} | ||
|
||
_key = key; | ||
} | ||
|
||
public IDataProtector CreateProtector(string purpose) | ||
{ | ||
using var hmac = new HMACSHA256(_key); | ||
var purposeBytes = Encoding.UTF8.GetBytes(purpose); | ||
var key = hmac.ComputeHash(purposeBytes).Take(32).ToArray(); | ||
return new SingleKeyDataProtector(key); | ||
} | ||
|
||
public byte[] Protect(byte[] plaintext) | ||
{ | ||
using var aes = Aes.Create(); | ||
aes.Key = _key; | ||
aes.GenerateIV(); | ||
|
||
var iv = aes.IV; | ||
var encrypted = aes.EncryptCbc(plaintext, iv); | ||
|
||
return iv.Concat(encrypted).ToArray(); | ||
} | ||
|
||
public byte[] Unprotect(byte[] protectedData) | ||
{ | ||
using var aes = Aes.Create(); | ||
aes.Key = _key; | ||
|
||
if(protectedData.Length == 0) | ||
{ | ||
return protectedData; | ||
} | ||
var iv = protectedData.Take(16).ToArray(); | ||
var cipherText = protectedData.Skip(16).ToArray(); | ||
|
||
return aes.DecryptCbc(cipherText, iv); | ||
} | ||
|
||
} |
Oops, something went wrong.