diff --git a/src/Core/KeyManagement/Sends/ISendPasswordHasher.cs b/src/Core/KeyManagement/Sends/ISendPasswordHasher.cs new file mode 100644 index 0000000000..d41cd5d699 --- /dev/null +++ b/src/Core/KeyManagement/Sends/ISendPasswordHasher.cs @@ -0,0 +1,7 @@ +namespace Bit.Core.KeyManagement.Sends; + +public interface ISendPasswordHasher +{ + bool VerifyPasswordHash(string sendPasswordHash, string userSubmittedPasswordHash); + string HashPasswordHash(string clientHashedPassword); +} diff --git a/src/Core/KeyManagement/Sends/SendPasswordHasher.cs b/src/Core/KeyManagement/Sends/SendPasswordHasher.cs new file mode 100644 index 0000000000..3bc8a12612 --- /dev/null +++ b/src/Core/KeyManagement/Sends/SendPasswordHasher.cs @@ -0,0 +1,29 @@ +using Bit.Core.Entities; +using Microsoft.AspNetCore.Identity; + +namespace Bit.Core.KeyManagement.Sends; + +public class SendPasswordHasher(IPasswordHasher passwordHasher) : ISendPasswordHasher +{ + /// + /// Verifies an existing send password hash against a new user submitted password hash. + /// + public bool VerifyPasswordHash(string sendPasswordHash, string userSubmittedPasswordHash) + { + if (string.IsNullOrWhiteSpace(sendPasswordHash) || string.IsNullOrWhiteSpace(userSubmittedPasswordHash)) + { + return false; + } + var passwordResult = passwordHasher.VerifyHashedPassword(new User(), sendPasswordHash, userSubmittedPasswordHash); + + return passwordResult is PasswordVerificationResult.Success or PasswordVerificationResult.SuccessRehashNeeded; + } + + /// + /// Accepts a client hashed send password and returns a server hashed password. + /// + public string HashPasswordHash(string clientHashedPassword) + { + return passwordHasher.HashPassword(new User(), clientHashedPassword); + } +} diff --git a/src/Core/KeyManagement/Sends/SendPasswordServiceCollectionExtensions.cs b/src/Core/KeyManagement/Sends/SendPasswordServiceCollectionExtensions.cs new file mode 100644 index 0000000000..f502c0de06 --- /dev/null +++ b/src/Core/KeyManagement/Sends/SendPasswordServiceCollectionExtensions.cs @@ -0,0 +1,17 @@ +using Bit.Core.Auth.PasswordValidation; +using Bit.Core.Entities; +using Bit.Core.KeyManagement.Sends; +using Microsoft.AspNetCore.Identity; +using Microsoft.Extensions.DependencyInjection.Extensions; + +namespace Microsoft.Extensions.DependencyInjection; + +public static class SendPasswordServiceCollectionExtensions +{ + public static void AddSendPasswordServices(this IServiceCollection services) + { + services.TryAddScoped, PasswordHasher>(); + services.Configure(options => options.IterationCount = PasswordValidationConstants.PasswordHasherKdfIterations); + services.TryAddScoped(); + } +}