1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-07 19:50:32 -05:00

PM-20532 - Add KM SendPasswordHasher

This commit is contained in:
Jared Snider 2025-05-30 17:24:17 -04:00
parent 61a531c9bc
commit b16ae11d9c
No known key found for this signature in database
GPG Key ID: A149DDD612516286
3 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,7 @@
namespace Bit.Core.KeyManagement.Sends;
public interface ISendPasswordHasher
{
bool VerifyPasswordHash(string sendPasswordHash, string userSubmittedPasswordHash);
string HashPasswordHash(string clientHashedPassword);
}

View File

@ -0,0 +1,29 @@
using Bit.Core.Entities;
using Microsoft.AspNetCore.Identity;
namespace Bit.Core.KeyManagement.Sends;
public class SendPasswordHasher(IPasswordHasher<User> passwordHasher) : ISendPasswordHasher
{
/// <summary>
/// Verifies an existing send password hash against a new user submitted password hash.
/// </summary>
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;
}
/// <summary>
/// Accepts a client hashed send password and returns a server hashed password.
/// </summary>
public string HashPasswordHash(string clientHashedPassword)
{
return passwordHasher.HashPassword(new User(), clientHashedPassword);
}
}

View File

@ -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<IPasswordHasher<User>, PasswordHasher<User>>();
services.Configure<PasswordHasherOptions>(options => options.IterationCount = PasswordValidationConstants.PasswordHasherKdfIterations);
services.TryAddScoped<ISendPasswordHasher, SendPasswordHasher>();
}
}