2018-07-08 05:48:58 +02:00
|
|
|
import { ApiService } from '../abstractions/api.service';
|
2018-02-28 21:15:10 +01:00
|
|
|
import { AuditService as AuditServiceAbstraction } from '../abstractions/audit.service';
|
2018-04-22 05:55:21 +02:00
|
|
|
import { CryptoFunctionService } from '../abstractions/cryptoFunction.service';
|
2018-06-28 17:57:29 +02:00
|
|
|
|
2018-04-22 05:55:21 +02:00
|
|
|
import { Utils } from '../misc/utils';
|
2018-02-28 16:52:35 +01:00
|
|
|
|
2018-06-28 17:57:29 +02:00
|
|
|
import { BreachAccountResponse } from '../models/response/breachAccountResponse';
|
|
|
|
|
2018-02-28 16:52:35 +01:00
|
|
|
const PwnedPasswordsApi = 'https://api.pwnedpasswords.com/range/';
|
2018-06-28 17:57:29 +02:00
|
|
|
const HibpBreachApi = 'https://haveibeenpwned.com/api/v2/breachedaccount/';
|
2018-02-28 16:52:35 +01:00
|
|
|
|
2018-02-28 21:15:10 +01:00
|
|
|
export class AuditService implements AuditServiceAbstraction {
|
2018-07-08 05:48:58 +02:00
|
|
|
constructor(private cryptoFunctionService: CryptoFunctionService, private apiService: ApiService) { }
|
2018-02-28 16:52:35 +01:00
|
|
|
|
|
|
|
async passwordLeaked(password: string): Promise<number> {
|
2018-04-22 05:55:21 +02:00
|
|
|
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);
|
2018-02-28 16:52:35 +01:00
|
|
|
|
2018-07-18 21:09:13 +02:00
|
|
|
const response = await fetch(new Request(PwnedPasswordsApi + hashStart));
|
2018-02-28 16:52:35 +01:00
|
|
|
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:52:35 +01:00
|
|
|
|
2018-02-28 16:58:34 +01:00
|
|
|
return match != null ? parseInt(match.split(':')[1], 10) : 0;
|
2018-02-28 16:52:35 +01:00
|
|
|
}
|
2018-06-28 17:57:29 +02:00
|
|
|
|
2018-06-29 14:20:28 +02:00
|
|
|
async breachedAccounts(username: string): Promise<BreachAccountResponse[]> {
|
2018-09-11 21:54:18 +02:00
|
|
|
const response = await fetch(new Request(HibpBreachApi + username));
|
2018-06-28 17:57:29 +02:00
|
|
|
if (response.status === 404) {
|
|
|
|
return [];
|
|
|
|
} else if (response.status !== 200) {
|
|
|
|
throw new Error();
|
|
|
|
}
|
|
|
|
const responseJson = await response.json();
|
2018-06-28 19:48:50 +02:00
|
|
|
return responseJson.map((a: any) => new BreachAccountResponse(a));
|
2018-06-28 17:57:29 +02:00
|
|
|
}
|
2018-02-28 16:52:35 +01:00
|
|
|
}
|