-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MNTOR-3435 - Convert Breaches.js to Typescript (#4876)
* Migrate utils/hibp.js to TypeScript This removes the `Breach` type in functions/universal/breaches, which was created when first introducing TypeScript and the flow of data was still unclear, but by now had overlap with other types and no clear provenance. Instead, there are now three breach-related types, that represent where the data came from: - HibpGetBreachesResponse: this is an array of breach elements as returned from the HIBP API, unprocessed. Properties are in PascalCase, so are a breach's data classes. - BreachRow: this is a breach's data as stored in our database, along with some data we added to it, such as a favicon URL. Properties are snake_case, and data classes are lowercased and kebab-cased by the formatDataClassesArray function. - HibpLikeDbBreach: this is a breach's data fetched from the database, but stored in an object meant to look like the ones in HibpGetBreachesResponse. In other words, it contains the same data as BreachRow (including lowercased, kebab-cased data classes), but on PascalCase properties. The latter is somewhat of a historical artefact, because we used to try to load breaches from our database, then if our database didn't contain any breaches yet, fetch them live from the HIBP API and continue working with that. We no longer do that: now, even after fetching them from the HIBP API, we do a new query to get them from the database and process them into HibpLikeDbBreach, so that we can assume a consisent data structure everywhere we work with breaches. * MNTOR-3435 - breaches js to ts * remove old typedef * explicitly type breach as any * update path * update breaches path * Fix mistaken conflict resolutions * remove recency code * update fxa path link * remove comments --------- Co-authored-by: Vincent <git@vincenttunru.com>
- Loading branch information
Showing
6 changed files
with
170 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import { getUserEmails } from "../db/tables/emailAddresses.js"; | ||
import { | ||
getBreachesForEmail, | ||
getFilteredBreaches, | ||
HibpLikeDbBreach, | ||
} from "./hibp"; | ||
import { getSha1 } from "./fxa"; | ||
import { captureMessage } from "@sentry/node"; | ||
import { EmailAddressRow, SubscriberRow } from "knex/types/tables"; | ||
|
||
export type BundledVerifiedEmails = { | ||
email: string; | ||
breaches: HibpLikeDbBreach[]; | ||
id: number; | ||
primary: boolean; | ||
verified: boolean; | ||
hasNewBreaches?: number; | ||
}; | ||
|
||
export type AllEmailsAndBreaches = { | ||
unverifiedEmails: EmailAddressRow[]; | ||
verifiedEmails: BundledVerifiedEmails[]; | ||
}; | ||
|
||
type userType = | ||
| ({ | ||
email_addresses: Array<{ | ||
id: EmailAddressRow["id"]; | ||
email: EmailAddressRow["email"]; | ||
}>; | ||
} & SubscriberRow) | ||
| undefined; | ||
|
||
async function getAllEmailsAndBreaches( | ||
user: userType, | ||
allBreaches: HibpLikeDbBreach[], | ||
): Promise<AllEmailsAndBreaches> { | ||
const verifiedEmails: BundledVerifiedEmails[] = []; | ||
const unverifiedEmails: EmailAddressRow[] = []; | ||
|
||
if (!user) { | ||
const errMsg = "getAllEmailsAndBreaches: subscriber cannot be undefined"; | ||
console.error(errMsg); | ||
captureMessage(errMsg); | ||
|
||
return { verifiedEmails, unverifiedEmails }; | ||
} | ||
if (!allBreaches || allBreaches.length === 0) { | ||
const errMsg = | ||
"getAllEmailsAndBreaches: allBreaches object cannot be empty"; | ||
console.error(errMsg); | ||
captureMessage(errMsg); | ||
|
||
return { verifiedEmails, unverifiedEmails }; | ||
} | ||
|
||
const monitoredEmails = await getUserEmails(user.id); | ||
verifiedEmails.push( | ||
await bundleVerifiedEmails({ | ||
user, | ||
email: user.primary_email, | ||
recordId: user.id, | ||
recordVerified: user.primary_verified, | ||
allBreaches, | ||
}), | ||
); | ||
for (const email of monitoredEmails) { | ||
if (email.verified) { | ||
verifiedEmails.push( | ||
await bundleVerifiedEmails({ | ||
user, | ||
email: user.primary_email, | ||
recordId: email.id, | ||
recordVerified: email.verified, | ||
allBreaches, | ||
}), | ||
); | ||
} else { | ||
unverifiedEmails.push(email); | ||
} | ||
} | ||
|
||
// get new breaches since last shown | ||
for (const emailEntry of verifiedEmails) { | ||
const newBreachesForEmail = emailEntry.breaches.filter( | ||
(breach) => breach.AddedDate >= user.breaches_last_shown, | ||
); | ||
|
||
for (const newBreachForEmail of newBreachesForEmail) { | ||
newBreachForEmail.NewBreach = true; // add "NewBreach" property to the new breach. | ||
emailEntry.hasNewBreaches = newBreachesForEmail.length; // add the number of new breaches to the email | ||
} | ||
} | ||
|
||
return { verifiedEmails, unverifiedEmails }; | ||
} | ||
|
||
function addRecencyIndex(foundBreaches: HibpLikeDbBreach[]) { | ||
const annotatedBreaches: HibpLikeDbBreach[] = []; | ||
// slice() the array to make a copy so before reversing so we don't | ||
// reverse foundBreaches in-place | ||
const oldestToNewestFoundBreaches = foundBreaches.slice().reverse(); | ||
oldestToNewestFoundBreaches.forEach((annotatingBreach, index) => { | ||
const foundBreach = foundBreaches.find( | ||
(foundBreach) => foundBreach.Name === annotatingBreach.Name, | ||
); | ||
annotatedBreaches.push(Object.assign({ recencyIndex: index }, foundBreach)); | ||
}); | ||
return annotatedBreaches.reverse(); | ||
} | ||
|
||
type options = { | ||
user: userType; | ||
email: string; | ||
recordId: number; | ||
recordVerified: boolean; | ||
allBreaches: HibpLikeDbBreach[]; | ||
}; | ||
async function bundleVerifiedEmails( | ||
options: options, | ||
): Promise<BundledVerifiedEmails> { | ||
const { user, email, recordId, recordVerified, allBreaches } = options; | ||
const lowerCaseEmailSha = getSha1(email.toLowerCase()); | ||
|
||
// find all breaches relevant to the current email | ||
const foundBreaches = await getBreachesForEmail( | ||
lowerCaseEmailSha, | ||
allBreaches, | ||
true, | ||
false, | ||
); | ||
|
||
// TODO: remove after migration MNTOR-978 | ||
// adding index to breaches based on recency | ||
const foundBreachesWithRecency = addRecencyIndex(foundBreaches); | ||
|
||
if (!user) { | ||
const errMsg = "breachResolutionV2: subscriber cannot be undefined"; | ||
console.error(errMsg); | ||
captureMessage(errMsg); | ||
|
||
// @ts-ignore: function will be deprecated | ||
return { verifiedEmails, unverifiedEmails }; | ||
} | ||
|
||
// filter out irrelevant breaches based on HIBP | ||
const filteredAnnotatedFoundBreaches = getFilteredBreaches( | ||
foundBreachesWithRecency, | ||
); | ||
|
||
const emailEntry: BundledVerifiedEmails = { | ||
email: email, | ||
breaches: filteredAnnotatedFoundBreaches, | ||
primary: email === user.primary_email, | ||
id: recordId, | ||
verified: recordVerified, | ||
}; | ||
|
||
return emailEntry; | ||
} | ||
|
||
export { getAllEmailsAndBreaches }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters