-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
75 lines (64 loc) · 2.17 KB
/
index.js
File metadata and controls
75 lines (64 loc) · 2.17 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { MFAApi, Configuration } from 'bandwidth-sdk';
import prompts from 'prompts';
const BW_ACCOUNT_ID = process.env.BW_ACCOUNT_ID;
const BW_VOICE_APPLICATION_ID = process.env.BW_VOICE_APPLICATION_ID;
const BW_MESSAGING_APPLICATION_ID = process.env.BW_MESSAGING_APPLICATION_ID;
const BW_NUMBER = process.env.BW_NUMBER;
const BW_USERNAME = process.env.BW_USERNAME;
const BW_PASSWORD = process.env.BW_PASSWORD;
if([
BW_ACCOUNT_ID,
BW_VOICE_APPLICATION_ID,
BW_MESSAGING_APPLICATION_ID,
BW_NUMBER,
BW_USERNAME,
BW_PASSWORD
].some(item => item === undefined)) {
throw new Error('Please set the environment variables defined in the README');
}
const config = new Configuration({
username: BW_USERNAME,
password: BW_PASSWORD
});
const mfaApi = new MFAApi(config);
const main = async () => {
const inputs = await prompts([
{
name: 'recipientPhoneNumber',
type: 'text',
message: 'Enter phone number in E164 format (+15554443333): '
},
{
name: 'deliveryMethod',
type: 'text',
message: 'Enter MFA request method (voice/messaging). Default is messaging: ',
}
]);
const codeRequestBody = {
to: inputs.recipientPhoneNumber,
from: BW_NUMBER,
applicationId: inputs.deliveryMethod === 'voice' ? BW_VOICE_APPLICATION_ID : BW_MESSAGING_APPLICATION_ID,
scope: 'scope',
message: 'Your temporary {NAME} {SCOPE} code is {CODE}',
digits: 6
};
if (inputs.deliveryMethod === 'voice') {
await mfaApi.generateVoiceCode(BW_ACCOUNT_ID, codeRequestBody);
} else {
await mfaApi.generateMessagingCode(BW_ACCOUNT_ID, codeRequestBody);
}
const code = await prompts({
name: 'value',
type: 'text',
message: 'Please enter your received code: '
});
const verifyRequestBody = {
to: inputs.recipientPhoneNumber,
scope: 'scope',
code: code.value,
expirationTimeInMinutes: 3
};
const { data } = await mfaApi.verifyCode(BW_ACCOUNT_ID, verifyRequestBody);
data.valid ? console.log('Code verified') : console.log('Code not verified');
}
main();