1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-16 16:23:44 +00:00

Libs account serialization improvements

This commit is contained in:
Matt Gibson
2022-11-16 19:11:28 -05:00
parent 717afaf069
commit 41c60fd2a9
2 changed files with 20 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
/* eslint-disable no-useless-escape */ /* eslint-disable no-useless-escape */
import { getHostname, parse } from "tldts"; import { getHostname, parse } from "tldts";
import { Merge } from "type-fest";
import { CryptoService } from "../abstractions/crypto.service"; import { CryptoService } from "../abstractions/crypto.service";
import { EncryptService } from "../abstractions/encrypt.service"; import { EncryptService } from "../abstractions/encrypt.service";
@@ -55,6 +56,10 @@ export class Utils {
} }
static fromB64ToArray(str: string): Uint8Array { static fromB64ToArray(str: string): Uint8Array {
if (str == null) {
return null;
}
if (Utils.isNode) { if (Utils.isNode) {
return new Uint8Array(Buffer.from(str, "base64")); return new Uint8Array(Buffer.from(str, "base64"));
} else { } else {
@@ -108,6 +113,9 @@ export class Utils {
} }
static fromBufferToB64(buffer: ArrayBuffer): string { static fromBufferToB64(buffer: ArrayBuffer): string {
if (buffer == null) {
return null;
}
if (Utils.isNode) { if (Utils.isNode) {
return Buffer.from(buffer).toString("base64"); return Buffer.from(buffer).toString("base64");
} else { } else {
@@ -429,7 +437,7 @@ export class Utils {
* @param map * @param map
* @returns * @returns
*/ */
static mapToRecord<K extends string | number | symbol, V>(map: Map<K, V>): Record<string, V> { static mapToRecord<K extends string | number, V>(map: Map<K, V>): Record<string, V> {
return map == null ? null : Object.fromEntries(map); return map == null ? null : Object.fromEntries(map);
} }
@@ -457,6 +465,14 @@ export class Utils {
} }
} }
/** Applies Object.assign, but converts the type nicely using Type-Fest Merge<Destination, Source> */
static merge<Destination, Source>(
destination: Destination,
source: Source
): Merge<Destination, Source> {
return Object.assign(destination, source) as unknown as Merge<Destination, Source>;
}
private static isMobile(win: Window) { private static isMobile(win: Window) {
let mobile = false; let mobile = false;
((a) => { ((a) => {

View File

@@ -1,4 +1,4 @@
import { Except, Jsonify } from "type-fest"; import { Jsonify } from "type-fest";
import { AuthenticationStatus } from "../../enums/authenticationStatus"; import { AuthenticationStatus } from "../../enums/authenticationStatus";
import { KdfType } from "../../enums/kdfType"; import { KdfType } from "../../enums/kdfType";
@@ -40,7 +40,7 @@ export class EncryptionPair<TEncrypted, TDecrypted> {
} }
static fromJSON<TEncrypted, TDecrypted>( static fromJSON<TEncrypted, TDecrypted>(
obj: Jsonify<EncryptionPair<Jsonify<TEncrypted>, Jsonify<TDecrypted>>>, obj: { encrypted?: Jsonify<TEncrypted>; decrypted?: string | Jsonify<TDecrypted> },
decryptedFromJson?: (decObj: Jsonify<TDecrypted> | string) => TDecrypted, decryptedFromJson?: (decObj: Jsonify<TDecrypted> | string) => TDecrypted,
encryptedFromJson?: (encObj: Jsonify<TEncrypted>) => TEncrypted encryptedFromJson?: (encObj: Jsonify<TEncrypted>) => TEncrypted
) { ) {
@@ -123,7 +123,7 @@ export class AccountKeys {
apiKeyClientSecret?: string; apiKeyClientSecret?: string;
toJSON() { toJSON() {
return Object.assign(this as Except<AccountKeys, "publicKey">, { return Utils.merge(this, {
publicKey: Utils.fromBufferToByteString(this.publicKey), publicKey: Utils.fromBufferToByteString(this.publicKey),
}); });
} }