-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoFactorExtension.cs
More file actions
202 lines (170 loc) · 6.62 KB
/
Copy pathTwoFactorExtension.cs
File metadata and controls
202 lines (170 loc) · 6.62 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
// 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.Collections.Generic;
using System.Reflection;
using DatabaseWrapper.Core;
using DatabaseWrapper.Sqlite;
using ExpressionTree;
using TwoFactorAuthNet;
namespace DisCatSharp.Extensions.TwoFactorCommands;
/// <summary>
/// Represents a <see cref="TwoFactorExtension" />.
/// </summary>
public sealed class TwoFactorExtension : BaseExtension
{
/// <summary>
/// Gets the secret field name.
/// </summary>
private readonly string _secretField = "secret";
/// <summary>
/// Gets the table name.
/// </summary>
private readonly string _tableName = "twofactor_mapper";
/// <summary>
/// Gets the user field name.
/// </summary>
private readonly string _userField = "user";
/// <summary>
/// Initializes a new instance of the <see cref="TwoFactorExtension" /> class.
/// </summary>
/// <param name="configuration">The config.</param>
internal TwoFactorExtension(TwoFactorConfiguration? configuration = null)
{
configuration ??= new();
this.Configuration = configuration;
this.TwoFactorClient = new(configuration.Issuer, configuration.Digits, configuration.Period, configuration.Algorithm);
this.DatabaseClient = new(this.Configuration.DatabasePath);
if (this.DatabaseClient.TableExists(this._tableName))
return;
var columns = new List<Column>
{
new(this._userField, true, DataTypeEnum.Varchar, 128, null, false),
new(this._secretField, false, DataTypeEnum.Varchar, 512, null, false)
};
this.DatabaseClient.CreateTable(this._tableName, columns);
}
/// <summary>
/// Gets the two factor configuration.
/// </summary>
internal TwoFactorConfiguration Configuration { get; }
/// <summary>
/// Gets the database client.
/// </summary>
internal DatabaseClient DatabaseClient { get; }
/// <summary>
/// Gets the tfa client.
/// </summary>
internal TwoFactorAuth TwoFactorClient { get; }
/// <summary>
/// Gets the service provider this TwoFactor module was configured with.
/// </summary>
public IServiceProvider Services
=> this.Configuration.ServiceProvider;
/// <summary>
/// DO NOT USE THIS MANUALLY.
/// </summary>
/// <param name="client">DO NOT USE THIS MANUALLY.</param>
/// <exception cref="InvalidOperationException" />
protected internal override void Setup(DiscordClient client)
{
if (this.Client != null)
throw new InvalidOperationException("What did I tell you?");
this.Client = client;
var a = typeof(TwoFactorExtension).GetTypeInfo().Assembly;
var iv = a.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
if (iv != null)
this.VersionString = iv.InformationalVersion;
else
{
var v = a.GetName().Version;
var vs = v!.ToString(3);
if (v.Revision > 0)
this.VersionString = $"{vs}, CI build {v.Revision}";
}
this.HasVersionCheckSupport = true;
this.RepositoryOwner = "Aiko-IT-Systems";
this.Repository = "DisCatSharp.Extensions";
this.PackageId = "DisCatSharp.Extensions.OAuth2Web";
}
/// <summary>
/// Checks whether the database has info about the given user id.
/// </summary>
/// <param name="user">The user id to check.</param>
/// <returns></returns>
private bool HasData(ulong user)
=> this.DatabaseClient.Exists(this._tableName, new(this._userField, OperatorEnum.Equals, user.ToString()));
/// <summary>
/// Gets the secret for the given user id from the database.
/// </summary>
/// <param name="user">The user id to get data for.</param>
private string GetSecretFor(ulong user)
=> this.DatabaseClient.Select(this._tableName, null, 1, null, new(this._userField, OperatorEnum.Equals, user.ToString())).Rows[0].ItemArray[1]!.ToString()!;
/// <summary>
/// Adds a secret for the given user id to the database.
/// </summary>
/// <param name="user">The user id to add data for.</param>
/// <param name="secret">The secret to add.</param>
private void AddSecretFor(ulong user, string secret)
{
Dictionary<string, object> data = new()
{
{ this._userField, user.ToString() },
{ this._secretField, secret }
};
this.DatabaseClient.Insert(this._tableName, data);
}
/// <summary>
/// Removes a secret for the given user id in the database.
/// </summary>
/// <param name="user">The user id to remove data for.</param>
private void RemoveSecret(ulong user)
=> this.DatabaseClient.Delete(this._tableName, new(this._userField, OperatorEnum.Equals, user.ToString()));
/// <summary>
/// Checks whether the two factor auth code is valid for given user id.
/// </summary>
/// <param name="user">The user id entering the code.</param>
/// <param name="code">The code to check.</param>
/// <returns>Whether the code is valid.</returns>
public bool IsValidCode(ulong user, string code)
=> this.HasData(user) && this.TwoFactorClient.VerifyCode(this.GetSecretFor(user), code);
/// <summary>
/// Checks whether given user id is enrolled in two factor auth.
/// </summary>
/// <param name="user">User id to check for enrollment.</param>
/// <returns>Whether the user is enrolled.</returns>
public bool IsEnrolled(ulong user)
=> this.HasData(user);
/// <summary>
/// Enrolls given user id with two factor auth.
/// </summary>
/// <param name="user">User id to enroll.</param>
/// <param name="secret">Secret to use.</param>
public void EnrollUser(ulong user, string secret)
=> this.AddSecretFor(user, secret);
/// <summary>
/// Unenrolls given user id from two factor auth.
/// </summary>
/// <param name="user">User id to unenroll.</param>
public void DisenrollUser(ulong user)
=> this.RemoveSecret(user);
}