Skip to content

Commit e2a9b39

Browse files
committed
feat: 增加基于 OTP 的验证器组件
1 parent d1a0506 commit e2a9b39

5 files changed

Lines changed: 108 additions & 0 deletions

File tree

BootstrapBlazor.Extensions.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BootstrapBlazor.UniverIcon"
182182
EndProject
183183
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BootstrapBlazor.ChatBot", "src\components\BootstrapBlazor.ChatBot\BootstrapBlazor.ChatBot.csproj", "{2F37FBF4-5C1C-4493-B614-0E8361432621}"
184184
EndProject
185+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BootstrapBlazor.Authenticator", "src\components\BootstrapBlazor.Authenticator\BootstrapBlazor.Authenticator.csproj", "{1FDDF0AD-7AB6-4706-A183-26C680817BB4}"
186+
EndProject
185187
Global
186188
GlobalSection(SolutionConfigurationPlatforms) = preSolution
187189
Debug|Any CPU = Debug|Any CPU
@@ -492,6 +494,10 @@ Global
492494
{2F37FBF4-5C1C-4493-B614-0E8361432621}.Debug|Any CPU.Build.0 = Debug|Any CPU
493495
{2F37FBF4-5C1C-4493-B614-0E8361432621}.Release|Any CPU.ActiveCfg = Release|Any CPU
494496
{2F37FBF4-5C1C-4493-B614-0E8361432621}.Release|Any CPU.Build.0 = Release|Any CPU
497+
{1FDDF0AD-7AB6-4706-A183-26C680817BB4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
498+
{1FDDF0AD-7AB6-4706-A183-26C680817BB4}.Debug|Any CPU.Build.0 = Debug|Any CPU
499+
{1FDDF0AD-7AB6-4706-A183-26C680817BB4}.Release|Any CPU.ActiveCfg = Release|Any CPU
500+
{1FDDF0AD-7AB6-4706-A183-26C680817BB4}.Release|Any CPU.Build.0 = Release|Any CPU
495501
EndGlobalSection
496502
GlobalSection(SolutionProperties) = preSolution
497503
HideSolutionNode = FALSE
@@ -577,6 +583,7 @@ Global
577583
{E30AAB64-BF28-4960-89C1-1F521025F531} = {FF1089BE-C704-4374-B629-C57C08E1798F}
578584
{A657E04C-1495-439E-BC2E-1EEAB2D1B4DA} = {FF1089BE-C704-4374-B629-C57C08E1798F}
579585
{2F37FBF4-5C1C-4493-B614-0E8361432621} = {FF1089BE-C704-4374-B629-C57C08E1798F}
586+
{1FDDF0AD-7AB6-4706-A183-26C680817BB4} = {FF1089BE-C704-4374-B629-C57C08E1798F}
580587
EndGlobalSection
581588
GlobalSection(ExtensibilityGlobals) = postSolution
582589
SolutionGuid = {D5EB1960-6F30-4CE1-B375-EAE1F787D6FF}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Razor">
2+
3+
<PropertyGroup>
4+
<Version>9.0.0</Version>
5+
</PropertyGroup>
6+
7+
<PropertyGroup>
8+
<PackageTags>Bootstrap Blazor WebAssembly wasm Authenticator 2FA MFA OTP TOTP HOTP</PackageTags>
9+
<Description>Bootstrap UI components extensions of Authenticator</Description>
10+
<RootNamespace>BootstrapBlazor.Components</RootNamespace>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="BootstrapBlazor" Version="$(BBVersion)" />
15+
<PackageReference Include="Otp.NET" Version="1.4.0" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\..\..\..\BootstrapBlazor\src\BootstrapBlazor\BootstrapBlazor.csproj" />
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
namespace BootstrapBlazor.Components;
6+
7+
internal static class OTPExtensions
8+
{
9+
public static OtpNet.OtpHashMode ToMode(this OTPHashMode mode) => mode switch
10+
{
11+
OTPHashMode.Sha256 => OtpNet.OtpHashMode.Sha256,
12+
OTPHashMode.Sha512 => OtpNet.OtpHashMode.Sha512,
13+
_ => OtpNet.OtpHashMode.Sha1
14+
};
15+
16+
public static OtpNet.OtpType ToType(this OTPType type) => type switch
17+
{
18+
OTPType.Hotp => OtpNet.OtpType.Hotp,
19+
_ => OtpNet.OtpType.Totp
20+
};
21+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.DependencyInjection;
6+
using Microsoft.Extensions.DependencyInjection.Extensions;
7+
using Microsoft.Extensions.Options;
8+
9+
namespace BootstrapBlazor.Components;
10+
11+
/// <summary>
12+
/// BootstrapBlazor 服务扩展类
13+
/// </summary>
14+
public static class ServiceCollectionExtension
15+
{
16+
/// <summary>
17+
/// 添加百度文字识别服务
18+
/// </summary>
19+
/// <param name="services"></param>
20+
/// <param name="configOptions"></param>
21+
/// <returns></returns>
22+
public static IServiceCollection AddBootstrapBlazorAuthenticator(this IServiceCollection services, Action<
23+
AuthenticatorOptions>? configOptions = null)
24+
{
25+
services.TryAddSingleton<IAuthenticatorService, DefaultAuthenticatorServices>();
26+
services.AddOptionsMonitor<AuthenticatorOptions>();
27+
services.Configure<AuthenticatorOptions>(options =>
28+
{
29+
configOptions?.Invoke(options);
30+
31+
options.AccountName ??= "BootstrapBlazor";
32+
options.UserName ??= "Simulator";
33+
options.IssuerName ??= options.AccountName;
34+
options.SecretKey ??= "OMM2LVLFX6QJHMYI";
35+
});
36+
return services;
37+
}
38+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
internal class DefaultAuthenticatorServices(IOptionsMonitor<AuthenticatorOptions> optionsMonitor) : IAuthenticatorService
11+
{
12+
public string GenerateOtpUri(AuthenticatorOptions? options = null)
13+
{
14+
options ??= optionsMonitor.CurrentValue;
15+
var type = options.Type.ToType();
16+
var mode = options.Algorithm.ToMode();
17+
var uri = new OtpUri(type, options.SecretKey, options.UserName, options.IssuerName, mode, options.Digits, options.Period, options.Counter);
18+
return uri.ToString();
19+
}
20+
}

0 commit comments

Comments
 (0)