2020-06-29 04:44:35 +02:00
|
|
|
using System;
|
2020-03-27 04:58:45 +01:00
|
|
|
using System.Collections.Generic;
|
2020-08-19 14:46:45 +02:00
|
|
|
using System.Linq;
|
2020-03-27 04:58:45 +01:00
|
|
|
using System.Security.Claims;
|
|
|
|
using System.Text;
|
|
|
|
using System.Text.Encodings.Web;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using BTCPayServer.Client;
|
|
|
|
using BTCPayServer.Data;
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
2021-04-28 06:14:15 +02:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2020-03-27 04:58:45 +01:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Security.GreenField
|
|
|
|
{
|
2020-03-27 05:06:41 +01:00
|
|
|
public class BasicAuthenticationHandler : AuthenticationHandler<GreenFieldAuthenticationOptions>
|
2020-03-27 04:58:45 +01:00
|
|
|
{
|
|
|
|
private readonly IOptionsMonitor<IdentityOptions> _identityOptions;
|
|
|
|
private readonly SignInManager<ApplicationUser> _signInManager;
|
|
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
|
|
|
|
2020-03-27 05:06:41 +01:00
|
|
|
public BasicAuthenticationHandler(
|
2020-03-27 04:58:45 +01:00
|
|
|
IOptionsMonitor<IdentityOptions> identityOptions,
|
|
|
|
IOptionsMonitor<GreenFieldAuthenticationOptions> options,
|
|
|
|
ILoggerFactory logger,
|
|
|
|
UrlEncoder encoder,
|
|
|
|
ISystemClock clock,
|
|
|
|
SignInManager<ApplicationUser> signInManager,
|
|
|
|
UserManager<ApplicationUser> userManager) : base(options, logger, encoder, clock)
|
|
|
|
{
|
|
|
|
_identityOptions = identityOptions;
|
|
|
|
_signInManager = signInManager;
|
|
|
|
_userManager = userManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
|
|
|
|
{
|
|
|
|
string authHeader = Context.Request.Headers["Authorization"];
|
|
|
|
|
2020-03-27 05:06:41 +01:00
|
|
|
if (authHeader == null || !authHeader.StartsWith("Basic ", StringComparison.OrdinalIgnoreCase))
|
|
|
|
return AuthenticateResult.NoResult();
|
2020-07-13 08:35:13 +02:00
|
|
|
string password;
|
|
|
|
string username;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var encodedUsernamePassword =
|
|
|
|
authHeader.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries)[1]?.Trim();
|
|
|
|
var decodedUsernamePassword =
|
|
|
|
Encoding.UTF8.GetString(Convert.FromBase64String(encodedUsernamePassword)).Split(':');
|
|
|
|
username = decodedUsernamePassword[0];
|
|
|
|
password = decodedUsernamePassword[1];
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
return AuthenticateResult.Fail(
|
|
|
|
"Basic authentication header was not in a correct format. (username:password encoded in base64)");
|
|
|
|
}
|
2020-03-27 04:58:45 +01:00
|
|
|
|
|
|
|
var result = await _signInManager.PasswordSignInAsync(username, password, true, true);
|
2020-03-27 05:06:41 +01:00
|
|
|
if (!result.Succeeded)
|
|
|
|
return AuthenticateResult.Fail(result.ToString());
|
2020-03-27 04:58:45 +01:00
|
|
|
|
2021-04-28 06:14:15 +02:00
|
|
|
var user = await _userManager.Users
|
|
|
|
.Include(applicationUser => applicationUser.Fido2Credentials)
|
|
|
|
.FirstOrDefaultAsync(applicationUser =>
|
|
|
|
applicationUser.NormalizedUserName == _userManager.NormalizeName(username));
|
|
|
|
|
2021-04-28 09:22:09 +02:00
|
|
|
if (user.Fido2Credentials.Any())
|
2021-04-28 06:14:15 +02:00
|
|
|
{
|
|
|
|
return AuthenticateResult.Fail("Cannot use Basic authentication with multi-factor is enabled.");
|
|
|
|
}
|
2020-03-27 04:58:45 +01:00
|
|
|
var claims = new List<Claim>()
|
|
|
|
{
|
|
|
|
new Claim(_identityOptions.CurrentValue.ClaimsIdentity.UserIdClaimType, user.Id),
|
|
|
|
new Claim(GreenFieldConstants.ClaimTypes.Permission,
|
|
|
|
Permission.Create(Policies.Unrestricted).ToString())
|
|
|
|
};
|
2020-08-19 14:46:45 +02:00
|
|
|
claims.AddRange((await _userManager.GetRolesAsync(user)).Select(s => new Claim(_identityOptions.CurrentValue.ClaimsIdentity.RoleClaimType, s)));
|
2020-03-27 04:58:45 +01:00
|
|
|
|
|
|
|
return AuthenticateResult.Success(new AuthenticationTicket(
|
|
|
|
new ClaimsPrincipal(new ClaimsIdentity(claims, GreenFieldConstants.AuthenticationType)),
|
|
|
|
GreenFieldConstants.AuthenticationType));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|