Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
106 lines (93 loc) · 3.1 KB
/
Copy pathProgram.cs
File metadata and controls
106 lines (93 loc) · 3.1 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System.Text.RegularExpressions;
using Bandwidth.Standard.Api;
using Bandwidth.Standard.Client;
using Bandwidth.Standard.Model;
//Setting up environment variables
string? BW_ACCOUNT_ID = Environment.GetEnvironmentVariable("BW_ACCOUNT_ID");
string? BW_CLIENT_ID = Environment.GetEnvironmentVariable("BW_CLIENT_ID");
string? BW_CLIENT_SECRET = Environment.GetEnvironmentVariable("BW_CLIENT_SECRET");
string? BW_NUMBER = Environment.GetEnvironmentVariable("BW_NUMBER");
string? BW_VOICE_APPLICATION_ID = Environment.GetEnvironmentVariable("BW_VOICE_APPLICATION_ID");
string? BW_MESSAGING_APPLICATION_ID = Environment.GetEnvironmentVariable("BW_MESSAGING_APPLICATION_ID");
if (BW_ACCOUNT_ID == null || BW_CLIENT_ID == null || BW_CLIENT_SECRET == null || BW_NUMBER == null
|| BW_VOICE_APPLICATION_ID == null || BW_MESSAGING_APPLICATION_ID == null)
{
Console.WriteLine("Please set the environmental variables defined in the README");
Environment.Exit(1);
return;
}
Configuration configuration = new Configuration();
configuration.OAuthClientId = BW_CLIENT_ID;
configuration.OAuthClientSecret = BW_CLIENT_SECRET;
const string SCOPE = "sample";
string Prompt(string text)
{
Console.Write(text);
string? line = Console.ReadLine();
if (line == null)
{
Environment.Exit(1);
}
return line.Trim();
}
string phoneNumber = Prompt("Enter the phone number to send the code to (E.164, e.g. +15554443333): ");
if (!Regex.IsMatch(phoneNumber, @"^\+[1-9]\d{1,14}$"))
{
Console.Error.WriteLine("Phone number must be in E.164 format, e.g. +15554443333");
Environment.Exit(1);
}
string method = Prompt("Send the code by messaging or voice? [messaging]: ").ToLower();
if (method == "")
{
method = "messaging";
}
if (method != "messaging" && method != "voice")
{
Console.Error.WriteLine("Method must be messaging or voice");
Environment.Exit(1);
}
MFAApi mfaApi = new MFAApi(configuration);
CodeRequest codeRequest = new CodeRequest(
to: phoneNumber,
from: BW_NUMBER,
applicationId: method == "voice" ? BW_VOICE_APPLICATION_ID : BW_MESSAGING_APPLICATION_ID,
scope: SCOPE,
message: "Your temporary {NAME} {SCOPE} code is {CODE}",
digits: 6
);
Console.WriteLine($"Sending a code by {method} to {phoneNumber}");
try
{
if (method == "voice")
{
await mfaApi.GenerateVoiceCodeAsync(BW_ACCOUNT_ID, codeRequest);
}
else
{
await mfaApi.GenerateMessagingCodeAsync(BW_ACCOUNT_ID, codeRequest);
}
}
catch (Exception e)
{
Console.Error.WriteLine($"Failed to send code: {e.Message}");
Environment.Exit(1);
}
string code = Prompt("Enter the code you received: ");
bool valid = false;
try
{
VerifyCodeRequest verifyRequest = new VerifyCodeRequest(
to: phoneNumber,
scope: SCOPE,
expirationTimeInMinutes: 3,
code: code
);
valid = (await mfaApi.VerifyCodeAsync(BW_ACCOUNT_ID, verifyRequest)).Valid;
}
catch (Exception e)
{
Console.Error.WriteLine($"Failed to verify code: {e.Message}");
Environment.Exit(1);
}
Console.WriteLine(valid ? "Code verified" : "Code not verified");
Environment.Exit(valid ? 0 : 1);