-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoFactorApplicationCommandExtension.cs
More file actions
337 lines (274 loc) · 18.3 KB
/
Copy pathTwoFactorApplicationCommandExtension.cs
File metadata and controls
337 lines (274 loc) · 18.3 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// This file is part of the DisCatSharp project.
//
// Copyright (c) 2021-2023 AITSYS
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using DisCatSharp.ApplicationCommands.Context;
using DisCatSharp.Entities;
using DisCatSharp.Enums;
using DisCatSharp.EventArgs;
using DisCatSharp.Extensions.TwoFactorCommands.Entities;
using DisCatSharp.Extensions.TwoFactorCommands.Enums;
using DisCatSharp.Interactivity.Extensions;
using TwoFactorAuthNet;
namespace DisCatSharp.Extensions.TwoFactorCommands.ApplicationCommands;
public static class TwoFactorApplicationCommandExtension
{
/// <summary>
/// <para>Enrolls the user via modal input into two factor.</para>
/// <para>This uses DisCatSharp.Interactivity.</para>
/// <para>To be used as first action for application commands.</para>
/// </summary>
/// <param name="ctx">The base context.</param>
/// <returns>A <see cref="TwoFactorResponse" />.</returns>
public static async Task<TwoFactorResponse> EnrollTwoFactorAsync(this BaseContext ctx)
{
var ext = ctx.Client.GetTwoFactor() ?? throw new InvalidOperationException("Two factor extension is not registered on this client.");
var secret = ext.TwoFactorClient.CreateSecret(160, CryptoSecureRequirement.RequireSecure);
var label = $"{ext.Configuration.ResponseConfiguration.AuthenticatorAccountPrefix}: {HttpUtility.UrlEncode(ctx.User.Username)}";
var otpauth = $"otpauth://totp/{label}?secret={secret}&issuer={ext.TwoFactorClient.Issuer}";
var qrBlockText = otpauth.ToBlockText();
DiscordInteractionModalBuilder builder = new("Register Two Factor");
builder.AddTextDisplayComponent(new DiscordTextDisplayComponent($"## Scan this QR code with your authenticator app:\n{qrBlockText.BlockCode()}\n## Can't scan it? Enter this secret key instead:\n{secret.BlockCode()}"));
builder.AddLabelComponent(new("Enter Code", "Enter the code given by your authenticator app.", new DiscordTextInputComponent(TextComponentStyle.Small, customId: "code", minLength: ext.Configuration.Digits, maxLength: ext.Configuration.Digits)));
await ctx.CreateModalResponseAsync(builder);
var response = new TwoFactorResponse
{
Client = ctx.Client
};
var inter = await ctx.Client.GetInteractivity().WaitForModalAsync(builder.CustomId, TimeSpan.FromSeconds(ext.Configuration.TwoFactorTimeout));
if (inter.TimedOut)
{
response.Result = TwoFactorResult.TimedOut;
return response;
}
response.ComponentInteraction = inter.Result;
if (ext.Configuration.ResponseConfiguration.ShowResponse)
await inter.Result.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().AsEphemeral().WithV2Components().AddComponents(new DiscordContainerComponent([new DiscordTextDisplayComponent("Two Factor"), new DiscordTextDisplayComponent("Checking..")], accentColor: DiscordColor.Blurple)));
var codeResult = inter.Result.Interaction.Data.ModalComponents
.OfType<DiscordLabelComponent>()
.Select(component => component.Component as DiscordTextInputComponent)
.FirstOrDefault(component => component?.CustomId == "code")
?.Value;
if (string.IsNullOrWhiteSpace(codeResult) || codeResult.Any(dig => !char.IsDigit(dig)))
{
if (ext.Configuration.ResponseConfiguration.ShowResponse)
await inter.Result.Interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().WithV2Components().AddComponents(new DiscordContainerComponent([new DiscordTextDisplayComponent("Two Factor"), new DiscordTextDisplayComponent(ext.Configuration.ResponseConfiguration.AuthenticationFailureMessage)], accentColor: DiscordColor.Red)));
response.Result = TwoFactorResult.InvalidCode;
return response;
}
var res = ext.TwoFactorClient.VerifyCode(secret, codeResult);
if (res)
{
ext.EnrollUser(ctx.User.Id, secret);
if (ext.Configuration.ResponseConfiguration.ShowResponse)
await inter.Result.Interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().WithV2Components().AddComponents(new DiscordContainerComponent([new DiscordTextDisplayComponent("Two Factor"), new DiscordTextDisplayComponent(ext.Configuration.ResponseConfiguration.AuthenticationEnrolledMessage)], accentColor: DiscordColor.Green)));
response.Result = TwoFactorResult.Enrolled;
return response;
}
if (ext.Configuration.ResponseConfiguration.ShowResponse)
await inter.Result.Interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().WithV2Components().AddComponents(new DiscordContainerComponent([new DiscordTextDisplayComponent("Two Factor"), new DiscordTextDisplayComponent(ext.Configuration.ResponseConfiguration.AuthenticationFailureMessage)], accentColor: DiscordColor.Red)));
response.Result = TwoFactorResult.InvalidCode;
return response;
}
/// <summary>
/// <para>Unenrolls the user from two factor authentication via modal verification.</para>
/// <para>This uses DisCatSharp.Interactivity.</para>
/// <para>To be used as first action for application commands.</para>
/// </summary>
/// <param name="ctx">The base context.</param>
/// <returns>A <see cref="TwoFactorResponse" />.</returns>
public static async Task<TwoFactorResponse> UnenrollTwoFactorAsync(this BaseContext ctx)
{
var ext = ctx.Client.GetTwoFactor() ?? throw new InvalidOperationException("Two factor extension is not registered on this client.");
if (!ext.IsEnrolled(ctx.User.Id))
{
if (ext.Configuration.ResponseConfiguration.ShowResponse)
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().AsEphemeral().WithV2Components().AddComponents(new DiscordContainerComponent([new DiscordTextDisplayComponent("Two Factor"), new DiscordTextDisplayComponent(ext.Configuration.ResponseConfiguration.AuthenticationNotEnrolledMessage)], accentColor: DiscordColor.Orange)));
return new()
{
Client = ctx.Client,
Result = TwoFactorResult.NotEnrolled
};
}
DiscordInteractionModalBuilder builder = new("Remove Two Factor");
builder.AddTextDisplayComponent(new DiscordTextDisplayComponent("## To confirm removal, please enter your two factor authentication code below:"));
builder.AddLabelComponent(new("Two Factor Code", "The code displayed in your authenticator app.", new DiscordTextInputComponent(TextComponentStyle.Small, customId: "code", minLength: ext.Configuration.Digits, maxLength: ext.Configuration.Digits)));
await ctx.CreateModalResponseAsync(builder);
var response = new TwoFactorResponse
{
Client = ctx.Client
};
var inter = await ctx.Client.GetInteractivity().WaitForModalAsync(builder.CustomId, TimeSpan.FromSeconds(ext.Configuration.TwoFactorTimeout));
if (inter.TimedOut)
{
response.Result = TwoFactorResult.TimedOut;
return response;
}
response.ComponentInteraction = inter.Result;
if (ext.Configuration.ResponseConfiguration.ShowResponse)
await inter.Result.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().AsEphemeral().WithV2Components().AddComponents(new DiscordContainerComponent([new DiscordTextDisplayComponent("Two Factor"), new DiscordTextDisplayComponent("Checking..")], accentColor: DiscordColor.Blurple)));
var codeResult = inter.Result.Interaction.Data.ModalComponents
.OfType<DiscordLabelComponent>()
.Select(component => component.Component as DiscordTextInputComponent)
.FirstOrDefault(component => component?.CustomId == "code")
?.Value;
if (string.IsNullOrWhiteSpace(codeResult) || codeResult.Any(dig => !char.IsDigit(dig)))
{
if (ext.Configuration.ResponseConfiguration.ShowResponse)
await inter.Result.Interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().WithV2Components().AddComponents(new DiscordContainerComponent([new DiscordTextDisplayComponent("Two Factor"), new DiscordTextDisplayComponent(ext.Configuration.ResponseConfiguration.AuthenticationFailureMessage)], accentColor: DiscordColor.Red)));
response.Result = TwoFactorResult.InvalidCode;
return response;
}
var res = ext.IsValidCode(ctx.User.Id, codeResult);
if (res)
{
ext.DisenrollUser(ctx.User.Id);
if (ext.Configuration.ResponseConfiguration.ShowResponse)
await inter.Result.Interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().WithV2Components().AddComponents(new DiscordContainerComponent([new DiscordTextDisplayComponent("Two Factor"), new DiscordTextDisplayComponent("Two factor authentication has been removed from your account.")], accentColor: DiscordColor.Green)));
response.Result = TwoFactorResult.Unenrolled;
return response;
}
if (ext.Configuration.ResponseConfiguration.ShowResponse)
await inter.Result.Interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().WithV2Components().AddComponents(new DiscordContainerComponent([new DiscordTextDisplayComponent("Two Factor"), new DiscordTextDisplayComponent(ext.Configuration.ResponseConfiguration.AuthenticationFailureMessage)], accentColor: DiscordColor.Red)));
response.Result = TwoFactorResult.InvalidCode;
return response;
}
/// <summary>
/// <para>Asks the user via modal input for the two factor code.</para>
/// <para>This uses DisCatSharp.Interactivity.</para>
/// <para>To be used as first action for application commands.</para>
/// </summary>
/// <param name="ctx">The base context.</param>
/// <returns>A <see cref="TwoFactorResponse" />.</returns>
public static async Task<TwoFactorResponse> RequestTwoFactorAsync(this BaseContext ctx)
{
var ext = ctx.Client.GetTwoFactor() ?? throw new InvalidOperationException("Two factor extension is not registered on this client.");
if (!ext.IsEnrolled(ctx.User.Id))
{
if (ext.Configuration.ResponseConfiguration.ShowResponse)
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().AsEphemeral().WithV2Components().AddComponents(new DiscordContainerComponent([new DiscordTextDisplayComponent("Two Factor"), new DiscordTextDisplayComponent(ext.Configuration.ResponseConfiguration.AuthenticationNotEnrolledMessage)], accentColor: DiscordColor.Orange)));
return new()
{
Client = ctx.Client,
Result = TwoFactorResult.NotEnrolled
};
}
DiscordInteractionModalBuilder builder = new(ext.Configuration.ResponseConfiguration.AuthenticationModalRequestTitle);
builder.AddTextDisplayComponent(new DiscordTextDisplayComponent("Please enter your two factor authentication code below:"));
builder.AddLabelComponent(new("Two Factor Code", "The code displayed in your authenticator app.", new DiscordTextInputComponent(TextComponentStyle.Small, customId: "code", minLength: ext.Configuration.Digits, maxLength: ext.Configuration.Digits)));
await ctx.CreateModalResponseAsync(builder);
var response = new TwoFactorResponse
{
Client = ctx.Client
};
var inter = await ctx.Client.GetInteractivity().WaitForModalAsync(builder.CustomId, TimeSpan.FromSeconds(ext.Configuration.TwoFactorTimeout));
if (inter.TimedOut)
{
response.Result = TwoFactorResult.TimedOut;
return response;
}
response.ComponentInteraction = inter.Result;
if (ext.Configuration.ResponseConfiguration.ShowResponse)
await inter.Result.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().AsEphemeral().WithV2Components().AddComponents(new DiscordContainerComponent([new DiscordTextDisplayComponent("Two Factor"), new DiscordTextDisplayComponent("Checking..")], accentColor: DiscordColor.Blurple)));
var codeResult = (inter.Result.Interaction.Data.ModalComponents.OfType<DiscordLabelComponent>().First().Component as DiscordTextInputComponent)!.Value!;
if (codeResult.Any(dig => !char.IsDigit(dig)))
{
if (ext.Configuration.ResponseConfiguration.ShowResponse)
await inter.Result.Interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().WithV2Components().AddComponents(new DiscordContainerComponent([new DiscordTextDisplayComponent("Two Factor"), new DiscordTextDisplayComponent(ext.Configuration.ResponseConfiguration.AuthenticationFailureMessage)], accentColor: DiscordColor.Red)));
response.Result = TwoFactorResult.InvalidCode;
return response;
}
var res = ext.IsValidCode(ctx.User.Id, codeResult);
if (res)
{
if (ext.Configuration.ResponseConfiguration.ShowResponse)
await inter.Result.Interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().WithV2Components().AddComponents(new DiscordContainerComponent([new DiscordTextDisplayComponent("Two Factor"), new DiscordTextDisplayComponent(ext.Configuration.ResponseConfiguration.AuthenticationSuccessMessage)], accentColor: DiscordColor.Green)));
response.Result = TwoFactorResult.ValidCode;
return response;
}
if (ext.Configuration.ResponseConfiguration.ShowResponse)
await inter.Result.Interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().WithV2Components().AddComponents(new DiscordContainerComponent([new DiscordTextDisplayComponent("Two Factor"), new DiscordTextDisplayComponent(ext.Configuration.ResponseConfiguration.AuthenticationFailureMessage)], accentColor: DiscordColor.Red)));
response.Result = TwoFactorResult.InvalidCode;
return response;
}
/// <summary>
/// <para>Asks the user via modal input for the two factor code.</para>
/// <para>This uses DisCatSharp.Interactivity.</para>
/// <para>To be used as first action for button.</para>
/// </summary>
/// <param name="evt">The interaction context.</param>
/// <param name="client">The discord client to use.</param>
/// <returns>A <see cref="TwoFactorResponse" />.</returns>
public static async Task<TwoFactorResponse> RequestTwoFactorAsync(this ComponentInteractionCreateEventArgs evt, DiscordClient client)
{
var ext = client.GetTwoFactor() ?? throw new InvalidOperationException("Two factor extension is not registered on this client.");
if (!ext.IsEnrolled(evt.User.Id))
{
if (ext.Configuration.ResponseConfiguration.ShowResponse)
await evt.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().AsEphemeral().WithV2Components().AddComponents(new DiscordContainerComponent([new DiscordTextDisplayComponent("Two Factor"), new DiscordTextDisplayComponent(ext.Configuration.ResponseConfiguration.AuthenticationNotEnrolledMessage)], accentColor: DiscordColor.Orange)));
return new()
{
Client = client,
Result = TwoFactorResult.NotEnrolled
};
}
DiscordInteractionModalBuilder builder = new(ext.Configuration.ResponseConfiguration.AuthenticationModalRequestTitle);
builder.AddTextDisplayComponent(new DiscordTextDisplayComponent("Please enter your two factor authentication code below:"));
builder.AddLabelComponent(new("Two Factor Code", "The code displayed in your authenticator app.", new DiscordTextInputComponent(TextComponentStyle.Small, customId: "code", minLength: ext.Configuration.Digits, maxLength: ext.Configuration.Digits)));
await evt.Interaction.CreateInteractionModalResponseAsync(builder);
var response = new TwoFactorResponse
{
Client = client
};
var inter = await client.GetInteractivity().WaitForModalAsync(builder.CustomId, TimeSpan.FromSeconds(ext.Configuration.TwoFactorTimeout));
if (inter.TimedOut)
{
response.Result = TwoFactorResult.TimedOut;
return response;
}
response.ComponentInteraction = inter.Result;
if (ext.Configuration.ResponseConfiguration.ShowResponse)
await inter.Result.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().AsEphemeral().WithV2Components().AddComponents(new DiscordContainerComponent([new DiscordTextDisplayComponent("Two Factor"), new DiscordTextDisplayComponent("Checking..")], accentColor: DiscordColor.Blurple)));
var codeResult = (inter.Result.Interaction.Data.ModalComponents.OfType<DiscordLabelComponent>().First().Component as DiscordTextInputComponent)!.Value!;
if (codeResult.Any(dig => !char.IsDigit(dig)))
{
if (ext.Configuration.ResponseConfiguration.ShowResponse)
await inter.Result.Interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().WithV2Components().AddComponents(new DiscordContainerComponent([new DiscordTextDisplayComponent("Two Factor"), new DiscordTextDisplayComponent(ext.Configuration.ResponseConfiguration.AuthenticationFailureMessage)], accentColor: DiscordColor.Red)));
response.Result = TwoFactorResult.InvalidCode;
return response;
}
var res = ext.IsValidCode(evt.User.Id, codeResult);
if (res)
{
if (ext.Configuration.ResponseConfiguration.ShowResponse)
await inter.Result.Interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().WithV2Components().AddComponents(new DiscordContainerComponent([new DiscordTextDisplayComponent("Two Factor"), new DiscordTextDisplayComponent(ext.Configuration.ResponseConfiguration.AuthenticationSuccessMessage)], accentColor: DiscordColor.Green)));
response.Result = TwoFactorResult.ValidCode;
return response;
}
if (ext.Configuration.ResponseConfiguration.ShowResponse)
await inter.Result.Interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().WithV2Components().AddComponents(new DiscordContainerComponent([new DiscordTextDisplayComponent("Two Factor"), new DiscordTextDisplayComponent(ext.Configuration.ResponseConfiguration.AuthenticationFailureMessage)], accentColor: DiscordColor.Red)));
response.Result = TwoFactorResult.InvalidCode;
return response;
}
}