-
Notifications
You must be signed in to change notification settings - Fork 1
/
parseCode.ts
52 lines (45 loc) · 1.38 KB
/
parseCode.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { TwilioMessage } from './mfa';
export default function parseCode(message: TwilioMessage): string | null {
// Add case-insenstitive (`i`) modifier to regex. (we can't lowercase the message body before matching incase some codes are alpha case-sensitive)
// Make sure the code is in capture group 1
const regex = [
// Bill.com + PayPal
/code: (\d+)/i,
// Bill.com + SVB
/code is (\d+)/i,
// Bill Pay (payee activation)
/activation code for.*is (\d+)/,
// Twilio + BitClout + Clubhouse
/code is: (\d+)/i,
// SendGrid
/code for SendGrid: (\d+)/i,
// Stripe
/code is: (\d{3}-\d{3})/i,
// Namecheap
/code - (\d+)/i,
// Google
/(G-\d+) is your Google/i,
// TikTok
/\[TikTok\] (\d+) is your verification/i,
// generic — from '443-98' short code
/(\d+) is your verification code/i,
// Authy
/manually enter: (\d+)/i,
// URLs. This is below the codes since we want to prioritize codes over URLs
/(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b[-a-zA-Z0-9()@:%_\+.~#?&//=]*)/i,
// last ditch resorts
/code:? ?(\d+)/i,
/(\d+) is your/i,
/(\d{4,8})/,
/(\d+)/,
];
let code: string;
regex.forEach((regex) => {
if (code) return; // skip if a code has already been found
const output = message.Body.match(regex);
if (output && output[1]) {
code = output[1]; // first capture group
}
});
return code ? code : null;
}