-
Notifications
You must be signed in to change notification settings - Fork 5
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
5 changed files
with
147 additions
and
127 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,51 @@ | ||
using BTCPayApp.Core.Contracts; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace BTCPayApp.Desktop; | ||
|
||
public class DesktopDataDirectoryProvider : IDataDirectoryProvider | ||
{ | ||
private readonly IConfiguration _configuration; | ||
private readonly ILogger<DesktopDataDirectoryProvider> _logger; | ||
|
||
public DesktopDataDirectoryProvider(IConfiguration configuration, ILogger<DesktopDataDirectoryProvider> logger) | ||
{ | ||
_configuration = configuration; | ||
_logger = logger; | ||
} | ||
|
||
private string? _result = null; | ||
public virtual Task<string> GetAppDataDirectory() | ||
{ | ||
if (_result != null) | ||
return Task.FromResult(_result); | ||
var def = "BTCPayApp"; | ||
var dirName = _configuration.GetValue("BTCPAYAPP_DIRNAME", def); | ||
_result = GetDirectory(dirName?? def); | ||
_logger.LogInformation($"Using data directory: {_result}"); | ||
return Task.FromResult(_result); | ||
} | ||
|
||
private string GetDirectory(string appDirectory) | ||
{ | ||
var environmentVariable1 = _configuration.GetValue<string>("HOME"); | ||
var environmentVariable2 = _configuration.GetValue<string>("APPDATA"); | ||
string str; | ||
if (!string.IsNullOrEmpty(environmentVariable1) && string.IsNullOrEmpty(environmentVariable2)) | ||
str = Path.Combine(environmentVariable1, "." + appDirectory.ToLowerInvariant()); | ||
else if (!string.IsNullOrEmpty(environmentVariable2)) | ||
{ | ||
str = Path.Combine(environmentVariable2, appDirectory); | ||
} | ||
else | ||
{ | ||
throw new DirectoryNotFoundException( | ||
"Could not find suitable datadir environment variables HOME or APPDATA are not set"); | ||
} | ||
|
||
if (!Directory.Exists(str)) | ||
Directory.CreateDirectory(str); | ||
return str; | ||
} | ||
} |
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,66 @@ | ||
using System.Text.Json; | ||
using BTCPayApp.Core.Contracts; | ||
using Microsoft.AspNetCore.DataProtection; | ||
|
||
namespace BTCPayApp.Desktop; | ||
|
||
public class DesktopSecureConfigProvider: ISecureConfigProvider | ||
{ | ||
private readonly IDataProtector _dataProtector; | ||
|
||
public DesktopSecureConfigProvider(IDataDirectoryProvider directoryProvider, IDataProtectionProvider dataProtectionProvider) | ||
{ | ||
_dataProtector = dataProtectionProvider.CreateProtector("SecureConfig"); | ||
_configDir = directoryProvider.GetAppDataDirectory().ContinueWith(task => | ||
{ | ||
var res = Path.Combine(task.Result, "config"); | ||
Directory.CreateDirectory(res); | ||
return res; | ||
}); | ||
} | ||
|
||
private readonly Task<string> _configDir; | ||
|
||
public async Task<T?> Get<T>(string key) | ||
{ | ||
var dir = Path.Combine(await _configDir, key); | ||
if (!File.Exists(dir)) | ||
{ | ||
return default; | ||
} | ||
var raw = await File.ReadAllTextAsync(dir); | ||
var json = await ReadFromRaw(raw); | ||
return JsonSerializer.Deserialize<T>(json); | ||
} | ||
|
||
|
||
public async Task Set<T>(string key, T? value) | ||
{ | ||
var dir = Path.Combine(await _configDir, key); | ||
if (value is null) | ||
{ | ||
if (File.Exists(dir)) | ||
{ | ||
File.Delete(dir); | ||
} | ||
} | ||
else | ||
{ | ||
var raw = JsonSerializer.Serialize(value); | ||
await File.WriteAllTextAsync(dir, await WriteFromRaw(raw)); | ||
} | ||
} | ||
|
||
public async Task<IEnumerable<string>> List(string prefix) | ||
{ | ||
var dir = await _configDir; | ||
if (!Directory.Exists(dir)) | ||
{ | ||
return Array.Empty<string>(); | ||
} | ||
return Directory.GetFiles(dir, $"{prefix}*").Select(Path.GetFileName).Where(p => p?.StartsWith(prefix) is true)!; | ||
} | ||
|
||
protected Task<string> ReadFromRaw(string str) => Task.FromResult(_dataProtector.Unprotect(str)); | ||
protected Task<string> WriteFromRaw(string str) => Task.FromResult(_dataProtector.Protect(str)); | ||
} |
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
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,27 @@ | ||
using Plugin.Fingerprint.Abstractions; | ||
|
||
namespace BTCPayApp.Desktop; | ||
|
||
public class StubFingerprintProvider: IFingerprint | ||
{ | ||
public Task<FingerprintAvailability> GetAvailabilityAsync(bool allowAlternativeAuthentication = false) | ||
{ | ||
return Task.FromResult(FingerprintAvailability.NoImplementation); | ||
} | ||
|
||
public Task<bool> IsAvailableAsync(bool allowAlternativeAuthentication = false) | ||
{ | ||
return Task.FromResult(false); | ||
} | ||
|
||
public Task<FingerprintAuthenticationResult> AuthenticateAsync(AuthenticationRequestConfiguration authRequestConfig, | ||
CancellationToken cancellationToken = new CancellationToken()) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public Task<AuthenticationType> GetAuthenticationTypeAsync() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
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