diff --git a/BtcTransmuter/Areas/Identity/Pages/Account/Login.cshtml.cs b/BtcTransmuter/Areas/Identity/Pages/Account/Login.cshtml.cs
index 93c9fab..ea13bef 100644
--- a/BtcTransmuter/Areas/Identity/Pages/Account/Login.cshtml.cs
+++ b/BtcTransmuter/Areas/Identity/Pages/Account/Login.cshtml.cs
@@ -137,7 +137,11 @@ public class GetCurrentUserResponse {
public bool EmailConfirmed { get; set; }
[JsonProperty("requiresEmailConfirmation")]
- public bool RequiresEmailConfirmation { get; set; }
+ public bool RequiresEmailConfirmation { get; set; }
+
+
+ [JsonProperty("roles")]
+ public string[] Roles { get; set; }
public override string ToString()
{
diff --git a/BtcTransmuter/Areas/Identity/Pages/Account/Manage/BTCPayAccountLink.cshtml.cs b/BtcTransmuter/Areas/Identity/Pages/Account/Manage/BTCPayAccountLink.cshtml.cs
index 6485084..f01966c 100644
--- a/BtcTransmuter/Areas/Identity/Pages/Account/Manage/BTCPayAccountLink.cshtml.cs
+++ b/BtcTransmuter/Areas/Identity/Pages/Account/Manage/BTCPayAccountLink.cshtml.cs
@@ -1,15 +1,10 @@
-using System;
-using System.ComponentModel.DataAnnotations;
-using System.Net.Http;
-using System.Net.Http.Headers;
+using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using BtcTransmuter.Data.Entities;
using BtcTransmuter.Data.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
-using Microsoft.Extensions.Logging;
-using Newtonsoft.Json;
namespace BtcTransmuter.Areas.Identity.Pages.Account.Manage
{
diff --git a/BtcTransmuter/Areas/Identity/Pages/Account/Manage/_ManageNav.cshtml b/BtcTransmuter/Areas/Identity/Pages/Account/Manage/_ManageNav.cshtml
index f657e31..31e09ae 100644
--- a/BtcTransmuter/Areas/Identity/Pages/Account/Manage/_ManageNav.cshtml
+++ b/BtcTransmuter/Areas/Identity/Pages/Account/Manage/_ManageNav.cshtml
@@ -8,13 +8,12 @@
- Profile
@if (!BtcTransmuterOptions.DisableInternalAuth)
- {
-
+ {
- Password
}
@if (BtcTransmuterOptions.BTCPayAuthServer != null)
{
- - BTCPay account link
+ - BTCPay account link
}
diff --git a/BtcTransmuter/BTCPayAuthService.cs b/BtcTransmuter/BTCPayAuthService.cs
index e69ad8f..b140daf 100644
--- a/BtcTransmuter/BTCPayAuthService.cs
+++ b/BtcTransmuter/BTCPayAuthService.cs
@@ -9,6 +9,7 @@
using BtcTransmuter.Data.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@@ -20,12 +21,16 @@ public class BTCPayAuthService
private readonly UserManager _userManager;
private readonly IBtcTransmuterOptions _btcTransmuterOptions;
private readonly IHttpClientFactory _httpClientFactory;
+ private readonly ILogger _logger;
- public BTCPayAuthService(UserManager userManager, IBtcTransmuterOptions btcTransmuterOptions, IHttpClientFactory httpClientFactory)
+ public BTCPayAuthService(
+ UserManager userManager, IBtcTransmuterOptions btcTransmuterOptions,
+ IHttpClientFactory httpClientFactory, ILogger logger)
{
_userManager = userManager;
_btcTransmuterOptions = btcTransmuterOptions;
_httpClientFactory = httpClientFactory;
+ _logger = logger;
}
public async Task LoginAndRegisterIfNeeded(string user, string pass)
@@ -40,6 +45,7 @@ public async Task LoginAndRegisterIfNeeded(string user, string pass)
{
return null;
}
+
var matchedUser = await FindUserByBTCPayUserId(response.Id);
if (matchedUser == null)
{
@@ -48,6 +54,7 @@ public async Task LoginAndRegisterIfNeeded(string user, string pass)
{
return null;
}
+
//create account
matchedUser = new User()
{
@@ -65,7 +72,8 @@ public async Task LoginAndRegisterIfNeeded(string user, string pass)
});
if ((await _userManager.CreateAsync(matchedUser)).Succeeded)
{
- if (await _userManager.Users.CountAsync() == 1)
+
+ if (response.Roles.Contains("ServerAdmin") || await _userManager.Users.CountAsync() == 1)
{
await _userManager.AddToRoleAsync(matchedUser, "Admin");
}
@@ -78,16 +86,26 @@ public async Task LoginAndRegisterIfNeeded(string user, string pass)
else
{
var tokenResponse = await CheckToken(matchedUser);
-
- if (!(tokenResponse?.ToString()?.Equals(response.ToString()) is true) && await GenerateKeyAndSet(user, pass, matchedUser))
+
+ if (!(tokenResponse?.ToString()?.Equals(response.ToString()) is true) &&
+ await GenerateKeyAndSet(user, pass, matchedUser))
{
await _userManager.UpdateAsync(matchedUser);
- }else if (!(tokenResponse?.ToString()?.Equals(response.ToString()) is true))
+ }
+ else if (!(tokenResponse?.ToString()?.Equals(response.ToString()) is true))
{
return null;
}
}
-
+ if (response.Roles.Contains("ServerAdmin"))
+ {
+ await _userManager.AddToRoleAsync(matchedUser, "Admin");
+ }
+ else if(!await _userManager.HasPasswordAsync(matchedUser))
+ {
+ await _userManager.RemoveFromRoleAsync(matchedUser, "Admin");
+ }
+
return matchedUser;
}
@@ -100,29 +118,33 @@ private async Task GenerateKeyAndSet(string user, string pass, User matche
}
var blob = matchedUser.Get();
- blob.BTCPayAuthDetails.AccessToken = key;
+ blob.BTCPayAuthDetails.AccessToken = key;
if (_btcTransmuterOptions.DisableInternalAuth || !await _userManager.HasPasswordAsync(matchedUser))
{
matchedUser.Email = user;
- matchedUser.UserName= user;
+ matchedUser.UserName = user;
}
+
matchedUser.Set(blob);
return true;
}
+
public async Task GenerateKey(string user, string pass)
{
var client = _httpClientFactory.CreateClient("BTCPayAuthServer");
- var request = new HttpRequestMessage(HttpMethod.Post, new Uri(_btcTransmuterOptions.BTCPayAuthServer, "/api/v1/api-keys"));
- request.Headers.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{user}:{pass}")));
+ var request = new HttpRequestMessage(HttpMethod.Post,
+ new Uri(_btcTransmuterOptions.BTCPayAuthServer, "/api/v1/api-keys"));
+ request.Headers.Authorization = new AuthenticationHeaderValue("Basic",
+ Convert.ToBase64String(Encoding.UTF8.GetBytes($"{user}:{pass}")));
request.Content = new StringContent(JsonConvert.SerializeObject(new
{
label = "transmuter login access token",
- permissions = new[]{ "btcpay.user.canmodifyprofile"}
+ permissions = new[] {"btcpay.user.canmodifyprofile"}
}), Encoding.UTF8, "application/json");
-
+
var response = await client.SendAsync(request);
- if(response.IsSuccessStatusCode)
+ if (response.IsSuccessStatusCode)
{
var accessTokenResponse =
JsonConvert.DeserializeObject((await response.Content.ReadAsStringAsync()));
@@ -131,6 +153,7 @@ public async Task GenerateKey(string user, string pass)
return null;
}
+
public async Task FindUserByBTCPayUserId(string userId)
{
return _userManager.Users.AsEnumerable().SingleOrDefault(user =>
@@ -143,18 +166,27 @@ public async Task BasicAuthLogin(string user, string pas
{
return null;
}
-
- var client = _httpClientFactory.CreateClient("BTCPayAuthServer");
- var fetchUserId = new Uri(_btcTransmuterOptions.BTCPayAuthServer, "api/v1/users/me");
- var request = new HttpRequestMessage(HttpMethod.Get, fetchUserId);
- request.Headers.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{user}:{pass}")));
- var response = await client.SendAsync(request);
- if (!response.IsSuccessStatusCode)
+
+ try
{
- return null;
+ var client = _httpClientFactory.CreateClient("BTCPayAuthServer");
+ var fetchUserId = new Uri(_btcTransmuterOptions.BTCPayAuthServer, "api/v1/users/me");
+ var request = new HttpRequestMessage(HttpMethod.Get, fetchUserId);
+ request.Headers.Authorization = new AuthenticationHeaderValue("Basic",
+ Convert.ToBase64String(Encoding.UTF8.GetBytes($"{user}:{pass}")));
+ var response = await client.SendAsync(request);
+ if (response.IsSuccessStatusCode)
+ {
+ return JsonConvert.DeserializeObject(
+ await response.Content.ReadAsStringAsync());
+ }
}
- return JsonConvert.DeserializeObject(
- await response.Content.ReadAsStringAsync());
+ catch (Exception e)
+ {
+ _logger.LogError(e, "error while attempting to authenticate with btcpay");
+ }
+
+ return null;
}
public async Task CheckToken(User user)
@@ -163,23 +195,25 @@ public async Task CheckToken(User user)
{
return null;
}
+
var blob = user.Get();
return await CheckToken(blob.BTCPayAuthDetails.AccessToken);
}
+
public async Task CheckToken(string token)
{
if (_btcTransmuterOptions.BTCPayAuthServer is null)
{
return null;
}
-
+
var client = _httpClientFactory.CreateClient("BTCPayAuthServer");
if (string.IsNullOrEmpty(token))
{
return null;
}
-
+
var fetchUserId = new Uri(_btcTransmuterOptions.BTCPayAuthServer, "api/v1/users/me");
var request = new HttpRequestMessage(HttpMethod.Get, fetchUserId);
request.Headers.Authorization = new AuthenticationHeaderValue("token", token);
@@ -188,10 +222,9 @@ public async Task CheckToken(string token)
{
return null;
}
+
return JsonConvert.DeserializeObject(
await response.Content.ReadAsStringAsync());
}
-
-
}
}
\ No newline at end of file
diff --git a/BtcTransmuter/Properties/launchSettings.json b/BtcTransmuter/Properties/launchSettings.json
index 1f24bc0..78adcf4 100644
--- a/BtcTransmuter/Properties/launchSettings.json
+++ b/BtcTransmuter/Properties/launchSettings.json
@@ -28,7 +28,8 @@
"NBXplorer_Uri": "http://127.0.0.1:32838/",
"NBXplorer_NetworkType":"Regtest",
"NBXplorer_UseDefaultCookie": "1",
- "BTCPayAuthServer": "https://localhost:14142"
+ "BTCPayAuthServer": "http://localhost:14142",
+ "DisableInternalAuth": "false"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
},