-
-
Notifications
You must be signed in to change notification settings - Fork 195
/
Authenticator.cs
228 lines (181 loc) 路 6.71 KB
/
Authenticator.cs
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
// Copyright (C) 2022 jmh
// SPDX-License-Identifier: GPL-3.0-only
using System;
using System.Text;
using AuthenticatorPro.Core.Generator;
using AuthenticatorPro.Core.Util;
using SQLite;
namespace AuthenticatorPro.Core.Entity
{
[Table("authenticator")]
public class Authenticator
{
public const int IssuerMaxLength = 32;
public const int UsernameMaxLength = 40;
public const HashAlgorithm DefaultAlgorithm = HashAlgorithm.Sha1;
private IGenerator _generator;
private long _lastCounter;
private string _code;
public Authenticator()
{
_code = null;
_generator = null;
_lastCounter = 0;
Algorithm = DefaultAlgorithm;
Type = AuthenticatorType.Totp;
Digits = Type.GetDefaultDigits();
Period = Type.GetDefaultPeriod();
}
[Column("type")]
public AuthenticatorType Type { get; set; }
[Column("icon")]
public string Icon { get; set; }
[Column("issuer")]
[MaxLength(IssuerMaxLength)]
public string Issuer { get; set; }
[Column("username")]
[MaxLength(UsernameMaxLength)]
public string Username { get; set; }
[Column("secret")]
[PrimaryKey]
public string Secret { get; set; }
[Column("pin")]
public string Pin { get; set; }
[Column("algorithm")]
public HashAlgorithm Algorithm { get; set; }
[Column("digits")]
public int Digits { get; set; }
[Column("period")]
public int Period { get; set; }
[Column("counter")]
public long Counter { get; set; }
[Column("copy_count")]
public int CopyCount { get; set; }
[Column("ranking")]
public int Ranking { get; set; }
public string GetCode(long counter)
{
_generator ??= Type switch
{
AuthenticatorType.Totp => new Totp(Secret, Period, Algorithm, Digits),
AuthenticatorType.Hotp => new Hotp(Secret, Algorithm, Digits),
AuthenticatorType.MobileOtp => new MobileOtp(Secret, Pin),
AuthenticatorType.SteamOtp => new SteamOtp(Secret),
AuthenticatorType.YandexOtp => new YandexOtp(Secret, Pin),
_ => throw new ArgumentException("Unknown authenticator type")
};
switch (Type.GetGenerationMethod())
{
case GenerationMethod.Time:
_code = _generator.Compute(counter);
break;
case GenerationMethod.Counter when _lastCounter == Counter:
return _code;
case GenerationMethod.Counter:
{
_code = _generator.Compute(Counter);
_lastCounter = Counter;
break;
}
}
return _code;
}
public string GetCode()
{
long counter;
switch (Type.GetGenerationMethod())
{
case GenerationMethod.Time:
{
var now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
counter = now - now % Period;
break;
}
case GenerationMethod.Counter:
counter = Counter;
break;
default:
throw new ArgumentException("Unknown generation method");
}
return GetCode(counter);
}
private string GetMotpUri()
{
var builder = new StringBuilder("motp://");
builder.Append(Issuer);
builder.Append(':');
builder.Append(Username);
builder.Append("?secret=");
builder.Append(Secret);
return builder.ToString();
}
private string GetOtpAuthUri()
{
var type = Type switch
{
AuthenticatorType.Hotp => "hotp",
AuthenticatorType.Totp => "totp",
AuthenticatorType.SteamOtp => "totp",
AuthenticatorType.YandexOtp => "yaotp",
_ => throw new NotSupportedException("Unsupported authenticator type")
};
var issuerUsername = string.IsNullOrEmpty(Username) ? Issuer : $"{Issuer}:{Username}";
var uri = new StringBuilder(
$"otpauth://{type}/{Uri.EscapeDataString(issuerUsername)}?secret={Secret}&issuer={Uri.EscapeDataString(Issuer)}");
if (Algorithm != DefaultAlgorithm)
{
var algorithmName = Algorithm switch
{
HashAlgorithm.Sha256 => "SHA256",
HashAlgorithm.Sha512 => "SHA512",
_ => throw new NotSupportedException("Unsupported algorithm")
};
uri.Append($"&algorithm={algorithmName}");
}
if (Digits != Type.GetDefaultDigits())
{
uri.Append($"&digits={Digits}");
}
switch (Type)
{
case AuthenticatorType.Totp when Period != Type.GetDefaultPeriod():
uri.Append($"&period={Period}");
break;
case AuthenticatorType.Hotp:
uri.Append($"&counter={Counter}");
break;
case AuthenticatorType.SteamOtp when Issuer != "Steam":
uri.Append("&steam");
break;
case AuthenticatorType.YandexOtp when Pin != null:
uri.Append($"&pin_length={Pin.Length}");
break;
}
return uri.ToString();
}
public virtual string GetUri()
{
return Type == AuthenticatorType.MobileOtp ? GetMotpUri() : GetOtpAuthUri();
}
public virtual void Validate()
{
if (string.IsNullOrEmpty(Issuer))
{
throw new ArgumentException("Issuer cannot be null or empty");
}
if (Digits < Type.GetMinDigits())
{
throw new ArgumentException($"Too few digits, expected at least {Type.GetMinDigits()}");
}
if (Digits > Type.GetMaxDigits())
{
throw new ArgumentException($"Too many digits, expected fewer than {Type.GetMaxDigits()}");
}
if (Type.GetGenerationMethod() == GenerationMethod.Time && Period <= 0)
{
throw new ArgumentException("Period cannot be negative or zero");
}
SecretUtil.Validate(Secret, Type);
}
}
}