1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-19 07:35:48 +02:00
bitwarden-browser/src/services/audit.service.ts

42 lines
1.6 KiB
TypeScript
Raw Normal View History

2018-07-08 05:48:58 +02:00
import { ApiService } from '../abstractions/api.service';
import { AuditService as AuditServiceAbstraction } from '../abstractions/audit.service';
import { CryptoFunctionService } from '../abstractions/cryptoFunction.service';
2018-06-28 17:57:29 +02:00
import { Utils } from '../misc/utils';
2018-06-28 17:57:29 +02:00
import { BreachAccountResponse } from '../models/response/breachAccountResponse';
2019-01-17 16:46:24 +01:00
import { ErrorResponse } from '../models/response/errorResponse';
2018-06-28 17:57:29 +02:00
const PwnedPasswordsApi = 'https://api.pwnedpasswords.com/range/';
export class AuditService implements AuditServiceAbstraction {
2018-07-08 05:48:58 +02:00
constructor(private cryptoFunctionService: CryptoFunctionService, private apiService: ApiService) { }
async passwordLeaked(password: string): Promise<number> {
const hashBytes = await this.cryptoFunctionService.hash(password, 'sha1');
const hash = Utils.fromBufferToHex(hashBytes).toUpperCase();
2018-02-28 16:58:34 +01:00
const hashStart = hash.substr(0, 5);
const hashEnding = hash.substr(5);
2019-01-17 16:46:24 +01:00
const response = await fetch(PwnedPasswordsApi + hashStart);
const leakedHashes = await response.text();
2018-02-28 16:58:34 +01:00
const match = leakedHashes.split(/\r?\n/).find((v) => {
return v.split(':')[0] === hashEnding;
});
2018-02-28 16:58:34 +01:00
return match != null ? parseInt(match.split(':')[1], 10) : 0;
}
2018-06-28 17:57:29 +02:00
2018-06-29 14:20:28 +02:00
async breachedAccounts(username: string): Promise<BreachAccountResponse[]> {
2019-01-17 16:46:24 +01:00
try {
return await this.apiService.getHibpBreach(username);
} catch (e) {
const error = e as ErrorResponse;
if (error.statusCode === 404) {
return [];
}
2018-06-28 17:57:29 +02:00
throw new Error();
}
}
}