1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-07 12:13:45 +00:00

PM-20532 - Add KM new SendPasswordService

This commit is contained in:
Jared Snider
2025-05-28 17:48:04 -04:00
parent 31ff7971c2
commit cf39a16a42
2 changed files with 47 additions and 0 deletions

View File

@@ -168,6 +168,7 @@ import {
MasterPasswordServiceAbstraction,
} from "@bitwarden/common/key-management/master-password/abstractions/master-password.service.abstraction";
import { MasterPasswordService } from "@bitwarden/common/key-management/master-password/services/master-password.service";
import { SendPasswordService } from "@bitwarden/common/key-management/sends/send-password.service";
import {
DefaultVaultTimeoutService,
DefaultVaultTimeoutSettingsService,
@@ -1433,6 +1434,11 @@ const safeProviders: SafeProvider[] = [
useExisting: NoopViewCacheService,
deps: [],
}),
safeProvider({
provide: SendPasswordService,
useClass: SendPasswordService,
deps: [CryptoFunctionServiceAbstraction],
}),
safeProvider({
provide: LoginComponentService,
useClass: DefaultLoginComponentService,

View File

@@ -0,0 +1,41 @@
import { Opaque } from "type-fest";
import { Utils } from "../../platform/misc/utils";
import { SEND_KDF_ITERATIONS } from "../../tools/send/send-kdf";
import { CryptoFunctionService } from "../crypto/abstractions/crypto-function.service";
// TODO: remove this comment:
// Code taken from send access.component.ts load method.
// TODO: add test file for this service.
/**
* Represents an opaque hashed send password as a base64 encoded string.
*/
export type SendHashedPassword = Opaque<string, "SendHashedPassword">;
/**
* Service for managing passwords for sends.
*/
export class SendPasswordService {
constructor(private cryptoFunctionService: CryptoFunctionService) {}
async hashPassword(password: string, keyMaterialUrlB64: string): Promise<SendHashedPassword> {
if (!password || !keyMaterialUrlB64) {
throw new Error("Password and key material URL base64 string are required.");
}
// Convert the key material URL base64 string to an array.
const keyMaterialArray = Utils.fromUrlB64ToArray(keyMaterialUrlB64);
// Derive a password hash using the key material.
const passwordHash = await this.cryptoFunctionService.pbkdf2(
password,
keyMaterialArray,
"sha256",
SEND_KDF_ITERATIONS,
);
// Convert the password hash to a base64 string and return as proper type
return Utils.fromBufferToB64(passwordHash) as SendHashedPassword;
}
}