Skip to content

Commit 54cf24f

Browse files
committed
refactor: 更改为 ITOTPService 服务
1 parent 0c9eb9a commit 54cf24f

4 files changed

Lines changed: 75 additions & 61 deletions

File tree

src/components/BootstrapBlazor.Authenticator/Extensions/OTPExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal static class OTPExtensions
1515

1616
public static OtpNet.OtpType ToType(this OTPType type) => type switch
1717
{
18-
OTPType.Hotp => OtpNet.OtpType.Hotp,
18+
OTPType.HOTP => OtpNet.OtpType.Hotp,
1919
_ => OtpNet.OtpType.Totp
2020
};
2121
}

src/components/BootstrapBlazor.Authenticator/Extensions/ServiceCollectionExtension.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
using Microsoft.Extensions.DependencyInjection;
66
using Microsoft.Extensions.DependencyInjection.Extensions;
7-
using Microsoft.Extensions.Options;
87

98
namespace BootstrapBlazor.Components;
109

@@ -22,7 +21,7 @@ public static class ServiceCollectionExtension
2221
public static IServiceCollection AddBootstrapBlazorAuthenticator(this IServiceCollection services, Action<
2322
AuthenticatorOptions>? configOptions = null)
2423
{
25-
services.TryAddSingleton<IAuthenticatorService, DefaultAuthenticatorServices>();
24+
services.TryAddSingleton<ITOTPService, DefaultTOTPServices>();
2625
services.AddOptionsMonitor<AuthenticatorOptions>();
2726
services.Configure<AuthenticatorOptions>(options =>
2827
{

src/components/BootstrapBlazor.Authenticator/Services/DefaultAuthenticatorServices.cs

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
using Microsoft.Extensions.Options;
6+
using OtpNet;
7+
8+
namespace BootstrapBlazor.Components;
9+
10+
class DefaultTOTPServices(IOptionsMonitor<AuthenticatorOptions> optionsMonitor) : ITOTPService
11+
{
12+
public TOTPInstanceBase? TOTPInstance { get; private set; }
13+
14+
public string GenerateOtpUri(AuthenticatorOptions? options = null)
15+
{
16+
options ??= optionsMonitor.CurrentValue;
17+
var type = options.Type.ToType();
18+
var mode = options.Algorithm.ToMode();
19+
var uri = new OtpUri(type, options.SecretKey, options.UserName, options.IssuerName, mode, options.Digits, options.Period, options.Counter);
20+
return uri.ToString();
21+
}
22+
23+
public string Compute(string secretKey, DateTime? timestamp = null)
24+
{
25+
var instance = new Totp(Base32Encoding.ToBytes(secretKey));
26+
TOTPInstance = new DefaultTOTPInstance(instance);
27+
return timestamp == null ? instance.ComputeTotp() : instance.ComputeTotp(timestamp.Value);
28+
}
29+
30+
public int GetRemainingSeconds(DateTime? timestamp = null)
31+
{
32+
if (TOTPInstance != null)
33+
{
34+
return timestamp == null ? TOTPInstance.GetRemainingSeconds() : TOTPInstance.GetRemainingSeconds(timestamp.Value);
35+
}
36+
var instance = new Totp(Base32Encoding.ToBytes("OMM2LVLFX6QJHMYI"));
37+
return timestamp == null ? instance.RemainingSeconds() : instance.RemainingSeconds(timestamp.Value);
38+
}
39+
40+
public string GenerateSecretKey(int length = 20)
41+
{
42+
var secretKey = KeyGeneration.GenerateRandomKey(length);
43+
return Base32Encoding.ToString(secretKey);
44+
}
45+
46+
public byte[] GetSecretKeyBytes(string input)
47+
{
48+
return Base32Encoding.ToBytes(input);
49+
}
50+
51+
public bool Verify(string code, DateTime? timestamp = null)
52+
{
53+
if (TOTPInstance != null)
54+
{
55+
return timestamp == null ? TOTPInstance.Verify(code) : TOTPInstance.Verify(code, timestamp.Value);
56+
}
57+
var instance = new Totp(Base32Encoding.ToBytes("OMM2LVLFX6QJHMYI"));
58+
return timestamp == null ? instance.VerifyTotp(code, out _) : instance.VerifyTotp(timestamp.Value, code, out _);
59+
}
60+
}
61+
62+
class DefaultTOTPInstance(Totp instance) : TOTPInstanceBase
63+
{
64+
public override int GetRemainingSeconds(DateTime? timestamp = null)
65+
{
66+
return timestamp == null ? instance.RemainingSeconds() : instance.RemainingSeconds(timestamp.Value);
67+
}
68+
69+
public override bool Verify(string code, DateTime? timestamp = null)
70+
{
71+
return timestamp == null ? instance.VerifyTotp(code, out _) : instance.VerifyTotp(timestamp.Value, code, out _);
72+
}
73+
}

0 commit comments

Comments
 (0)