Skip to content

Commit

Permalink
chore(core): add disposable phone validation (#3598)
Browse files Browse the repository at this point in the history
  • Loading branch information
dolcalmi authored Nov 24, 2023
1 parent 2eee388 commit 9b3fa32
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 53 deletions.
1 change: 1 addition & 0 deletions core/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@google-cloud/storage": "^7.6.0",
"@grpc/grpc-js": "^1.9.10",
"@grpc/proto-loader": "^0.7.10",
"@ip1sms/disposable-phone-numbers": "^2.1.600",
"@opentelemetry/api": "^1.7.0",
"@opentelemetry/core": "^1.18.1",
"@opentelemetry/exporter-trace-otlp-http": "^0.45.1",
Expand Down
12 changes: 11 additions & 1 deletion core/api/src/services/twilio-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import twilio from "twilio"

import { isAxiosError } from "axios"
import disposablePhoneList from "@ip1sms/disposable-phone-numbers"

import { wrapAsyncFunctionsToRunInSpan } from "./tracing"

Expand Down Expand Up @@ -49,13 +49,18 @@ export const TwilioClient = (): IPhoneProviderService => {
channel: ChannelType
}): Promise<true | PhoneProviderServiceError> => {
try {
if (isDisposablePhoneNumber(to)) {
return new InvalidTypePhoneProviderError()
}

const lookup = await client.lookups.v2.phoneNumbers(to).fetch({
fields: "line_type_intelligence",
})
// https://www.twilio.com/docs/lookup/v2-api/line-type-intelligence#type-property-values
if (lookup.lineTypeIntelligence.type === "nonFixedVoip") {
return new InvalidTypePhoneProviderError()
}

await verify.verifications.create({ to, channel })
} catch (err) {
baseLogger.error({ err }, "impossible to send text")
Expand Down Expand Up @@ -226,3 +231,8 @@ export const isPhoneCodeValid = async ({

return TwilioClient().validateVerify({ to: phone, code })
}

export const isDisposablePhoneNumber = (phone: PhoneNumber) => {
const phoneNumber = phone.replace(/[^0-9]/, "")
return phoneNumber in disposablePhoneList
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { isDisposablePhoneNumber } from "@/services/twilio-service"

describe("Twilio Service", () => {
describe("isDisposablePhoneNumber", () => {
const amounts = [
{ phone: "+34689682259", expected: true },
{ phone: "34689682259", expected: true },
{ phone: "+34699682259", expected: false },
{ phone: "34699682259", expected: false },
]
test.each(amounts)("phone $phone is disposable: $expected", ({ phone, expected }) => {
const result = isDisposablePhoneNumber(phone as PhoneNumber)
expect(result).toEqual(expected)
})
})
})
Loading

0 comments on commit 9b3fa32

Please sign in to comment.