mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 16:23:44 +00:00
user public key apis
This commit is contained in:
@@ -71,6 +71,7 @@ import { TwoFactorProviderResponse } from '../models/response/twoFactorProviderR
|
|||||||
import { TwoFactorRecoverResponse } from '../models/response/twoFactorRescoverResponse';
|
import { TwoFactorRecoverResponse } from '../models/response/twoFactorRescoverResponse';
|
||||||
import { TwoFactorU2fResponse } from '../models/response/twoFactorU2fResponse';
|
import { TwoFactorU2fResponse } from '../models/response/twoFactorU2fResponse';
|
||||||
import { TwoFactorYubiKeyResponse } from '../models/response/twoFactorYubiKeyResponse';
|
import { TwoFactorYubiKeyResponse } from '../models/response/twoFactorYubiKeyResponse';
|
||||||
|
import { UserKeyResponse } from '../models/response/userKeyResponse';
|
||||||
|
|
||||||
export abstract class ApiService {
|
export abstract class ApiService {
|
||||||
urlsSet: boolean;
|
urlsSet: boolean;
|
||||||
@@ -195,5 +196,7 @@ export abstract class ApiService {
|
|||||||
getEventsOrganizationUser: (organizationId: string, id: string,
|
getEventsOrganizationUser: (organizationId: string, id: string,
|
||||||
start: string, end: string, token: string) => Promise<ListResponse<EventResponse>>;
|
start: string, end: string, token: string) => Promise<ListResponse<EventResponse>>;
|
||||||
|
|
||||||
|
getUserPublicKey: (id: string) => Promise<UserKeyResponse>;
|
||||||
|
|
||||||
fetch: (request: Request) => Promise<Response>;
|
fetch: (request: Request) => Promise<Response>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ export abstract class CryptoService {
|
|||||||
makeEncKey: (key: SymmetricCryptoKey) => Promise<[SymmetricCryptoKey, CipherString]>;
|
makeEncKey: (key: SymmetricCryptoKey) => Promise<[SymmetricCryptoKey, CipherString]>;
|
||||||
encrypt: (plainValue: string | ArrayBuffer, key?: SymmetricCryptoKey) => Promise<CipherString>;
|
encrypt: (plainValue: string | ArrayBuffer, key?: SymmetricCryptoKey) => Promise<CipherString>;
|
||||||
encryptToBytes: (plainValue: ArrayBuffer, key?: SymmetricCryptoKey) => Promise<ArrayBuffer>;
|
encryptToBytes: (plainValue: ArrayBuffer, key?: SymmetricCryptoKey) => Promise<ArrayBuffer>;
|
||||||
|
rsaEncrypt: (data: ArrayBuffer, publicKey?: ArrayBuffer, key?: SymmetricCryptoKey) => Promise<CipherString>;
|
||||||
decryptToUtf8: (cipherString: CipherString, key?: SymmetricCryptoKey) => Promise<string>;
|
decryptToUtf8: (cipherString: CipherString, key?: SymmetricCryptoKey) => Promise<string>;
|
||||||
decryptFromBytes: (encBuf: ArrayBuffer, key: SymmetricCryptoKey) => Promise<ArrayBuffer>;
|
decryptFromBytes: (encBuf: ArrayBuffer, key: SymmetricCryptoKey) => Promise<ArrayBuffer>;
|
||||||
randomNumber: (min: number, max: number) => Promise<number>;
|
randomNumber: (min: number, max: number) => Promise<number>;
|
||||||
|
|||||||
9
src/models/response/userKeyResponse.ts
Normal file
9
src/models/response/userKeyResponse.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
export class UserKeyResponse {
|
||||||
|
userId: string;
|
||||||
|
publicKey: string;
|
||||||
|
|
||||||
|
constructor(response: any) {
|
||||||
|
this.userId = response.UserId;
|
||||||
|
this.publicKey = response.PublicKey;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -78,6 +78,7 @@ import { TwoFactorProviderResponse } from '../models/response/twoFactorProviderR
|
|||||||
import { TwoFactorRecoverResponse } from '../models/response/twoFactorRescoverResponse';
|
import { TwoFactorRecoverResponse } from '../models/response/twoFactorRescoverResponse';
|
||||||
import { TwoFactorU2fResponse } from '../models/response/twoFactorU2fResponse';
|
import { TwoFactorU2fResponse } from '../models/response/twoFactorU2fResponse';
|
||||||
import { TwoFactorYubiKeyResponse } from '../models/response/twoFactorYubiKeyResponse';
|
import { TwoFactorYubiKeyResponse } from '../models/response/twoFactorYubiKeyResponse';
|
||||||
|
import { UserKeyResponse } from '../models/response/userKeyResponse';
|
||||||
|
|
||||||
export class ApiService implements ApiServiceAbstraction {
|
export class ApiService implements ApiServiceAbstraction {
|
||||||
urlsSet: boolean = false;
|
urlsSet: boolean = false;
|
||||||
@@ -649,6 +650,13 @@ export class ApiService implements ApiServiceAbstraction {
|
|||||||
return new ListResponse(r, EventResponse);
|
return new ListResponse(r, EventResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// User APIs
|
||||||
|
|
||||||
|
async getUserPublicKey(id: string): Promise<UserKeyResponse> {
|
||||||
|
const r = await this.send('GET', '/users/' + id + '/public-key', null, true, true);
|
||||||
|
return new UserKeyResponse(r);
|
||||||
|
}
|
||||||
|
|
||||||
// Helpers
|
// Helpers
|
||||||
|
|
||||||
fetch(request: Request): Promise<Response> {
|
fetch(request: Request): Promise<Response> {
|
||||||
|
|||||||
@@ -356,6 +356,25 @@ export class CryptoService implements CryptoServiceAbstraction {
|
|||||||
return encBytes.buffer;
|
return encBytes.buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async rsaEncrypt(data: ArrayBuffer, publicKey?: ArrayBuffer, key?: SymmetricCryptoKey): Promise<CipherString> {
|
||||||
|
if (publicKey == null) {
|
||||||
|
publicKey = await this.getPublicKey();
|
||||||
|
}
|
||||||
|
if (publicKey == null) {
|
||||||
|
throw new Error('Public key unavailable.');
|
||||||
|
}
|
||||||
|
|
||||||
|
let type = EncryptionType.Rsa2048_OaepSha1_B64;
|
||||||
|
const encBytes = await this.cryptoFunctionService.rsaEncrypt(data, publicKey, 'sha1');
|
||||||
|
let mac: string = null;
|
||||||
|
if (key != null && key.macKey != null) {
|
||||||
|
type = EncryptionType.Rsa2048_OaepSha1_HmacSha256_B64;
|
||||||
|
const macBytes = await this.cryptoFunctionService.hmac(encBytes, key.macKey, 'sha256');
|
||||||
|
mac = Utils.fromBufferToB64(macBytes);
|
||||||
|
}
|
||||||
|
return new CipherString(type, Utils.fromBufferToB64(encBytes), null, mac);
|
||||||
|
}
|
||||||
|
|
||||||
async decrypt(cipherString: CipherString, key?: SymmetricCryptoKey): Promise<ArrayBuffer> {
|
async decrypt(cipherString: CipherString, key?: SymmetricCryptoKey): Promise<ArrayBuffer> {
|
||||||
const iv = Utils.fromB64ToArray(cipherString.iv).buffer;
|
const iv = Utils.fromB64ToArray(cipherString.iv).buffer;
|
||||||
const data = Utils.fromB64ToArray(cipherString.data).buffer;
|
const data = Utils.fromB64ToArray(cipherString.data).buffer;
|
||||||
@@ -530,25 +549,6 @@ export class CryptoService implements CryptoServiceAbstraction {
|
|||||||
return await this.cryptoFunctionService.aesDecrypt(data, iv, theKey.encKey);
|
return await this.cryptoFunctionService.aesDecrypt(data, iv, theKey.encKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async rsaEncrypt(data: ArrayBuffer, publicKey?: ArrayBuffer, key?: SymmetricCryptoKey) {
|
|
||||||
if (publicKey == null) {
|
|
||||||
publicKey = await this.getPublicKey();
|
|
||||||
}
|
|
||||||
if (publicKey == null) {
|
|
||||||
throw new Error('Public key unavailable.');
|
|
||||||
}
|
|
||||||
|
|
||||||
let type = EncryptionType.Rsa2048_OaepSha1_B64;
|
|
||||||
const encBytes = await this.cryptoFunctionService.rsaEncrypt(data, publicKey, 'sha1');
|
|
||||||
let mac: string = null;
|
|
||||||
if (key != null && key.macKey != null) {
|
|
||||||
type = EncryptionType.Rsa2048_OaepSha1_HmacSha256_B64;
|
|
||||||
const macBytes = await this.cryptoFunctionService.hmac(encBytes, key.macKey, 'sha256');
|
|
||||||
mac = Utils.fromBufferToB64(macBytes);
|
|
||||||
}
|
|
||||||
return new CipherString(type, Utils.fromBufferToB64(encBytes), null, mac);
|
|
||||||
}
|
|
||||||
|
|
||||||
private async rsaDecrypt(encValue: string): Promise<ArrayBuffer> {
|
private async rsaDecrypt(encValue: string): Promise<ArrayBuffer> {
|
||||||
const headerPieces = encValue.split('.');
|
const headerPieces = encValue.split('.');
|
||||||
let encType: EncryptionType = null;
|
let encType: EncryptionType = null;
|
||||||
|
|||||||
Reference in New Issue
Block a user