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:
parent
61a531c9bc
commit
b16ae11d9c
7
src/Core/KeyManagement/Sends/ISendPasswordHasher.cs
Normal file
7
src/Core/KeyManagement/Sends/ISendPasswordHasher.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace Bit.Core.KeyManagement.Sends;
|
||||
|
||||
public interface ISendPasswordHasher
|
||||
{
|
||||
bool VerifyPasswordHash(string sendPasswordHash, string userSubmittedPasswordHash);
|
||||
string HashPasswordHash(string clientHashedPassword);
|
||||
}
|
29
src/Core/KeyManagement/Sends/SendPasswordHasher.cs
Normal file
29
src/Core/KeyManagement/Sends/SendPasswordHasher.cs
Normal 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);
|
||||
}
|
||||
}
|
@ -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>();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user