网站首页 网站源码
website
站点相关全部源代码,隐藏了一些关于服务器的信息
using System.Text;
using Dpz.Core.Auth.Models;
using Dpz.Core.Authenticator;
using Dpz.Core.Service.RepositoryService;

namespace Dpz.Core.Auth.Service;

public class PinCodeValidator(
    IConfiguration configuration,
    IUserTwoFactorService userTwoFactorService
) : IPinCodeValidator
{
    public async Task<PinCodeValidatorModel> ValidateAsync(string? account, string pinCode)
    {
        if (configuration.GetValue("BypassTwoFactorIn", false))
        {
            return new PinCodeValidatorModel(true, null);
        }
        var (key, isBind) = await userTwoFactorService.GetKeyAsync(account);

        if (!isBind)
        {
            return new PinCodeValidatorModel(false, "请先绑定双因素认证");
        }

        if (string.IsNullOrEmpty(pinCode))
        {
            return new PinCodeValidatorModel(false, "请输入PIN码");
        }

        var twoFactorAuthenticator = new TwoFactorAuthenticator();
        var keyBuffer = Encoding.UTF8.GetBytes(key);
        var keyBase32 = Base32Encoding.ToString(keyBuffer);
        var twoFactorResult = twoFactorAuthenticator.ValidateTwoFactorPIN(keyBase32, pinCode, true);

        return !twoFactorResult
            ? new PinCodeValidatorModel(false, "PIN码验证错误")
            : new PinCodeValidatorModel(true, null);
    }

    public async Task<PinCodeValidatorModel> ValidateForAuthAsync(string? account, string? pinCode)
    {

        // 获取用户的2FA信息
        var (key, isBind) = await userTwoFactorService.GetKeyAsync(account);

        // 如果用户未绑定2FA,则跳过验证
        if (!isBind)
        {
            return new PinCodeValidatorModel(true, null);
        }

        // 如果已绑定2FA但未提供PIN码,返回错误
        if (string.IsNullOrWhiteSpace(pinCode))
        {
            return new PinCodeValidatorModel(false, "您已启用双因素验证,请输入验证码");
        }

        // 验证PIN码
        var twoFactorAuthenticator = new TwoFactorAuthenticator();
        var keyBuffer = Encoding.UTF8.GetBytes(key);
        var keyBase32 = Base32Encoding.ToString(keyBuffer);
        var twoFactorResult = twoFactorAuthenticator.ValidateTwoFactorPIN(keyBase32, pinCode, true);

        return !twoFactorResult
            ? new PinCodeValidatorModel(false, "双因素验证码验证失败,请检查验证码是否正确")
            : new PinCodeValidatorModel(true, null);
    }
}
⚠⚠    以下内容为AI分析的结果,请根据实际情况进行判断。

上述代码定义了一个名为 PinCodeValidator 的类,它实现了 IPinCodeValidator 接口,主要用于验证用户的双因素认证(2FA)PIN码。以下是代码的主要功能和结构的详细解释:

类和构造函数

  • 类定义: PinCodeValidator 类负责处理与双因素认证相关的 PIN 码验证逻辑。
  • 构造函数:
    • 接受两个参数:IConfiguration configurationIUserTwoFactorService userTwoFactorService
    • IConfiguration 用于获取应用程序的配置设置,例如是否绕过双因素认证的设置。
    • IUserTwoFactorService 是一个服务接口,用于获取用户的双因素认证密钥。

方法

  1. ValidateAsync 方法:

    • 参数: 接受一个可选的 account 字符串和一个 pinCode 字符串。
    • 功能:
      • 首先检查配置中是否启用了绕过双因素认证的选项。如果启用,直接返回验证成功。
      • 调用 userTwoFactorService.GetKeyAsync(account) 获取用户的双因素认证密钥和绑定状态。
      • 如果用户未绑定双因素认证,返回相应的错误信息。
      • 如果未提供 PIN 码,返回提示用户输入 PIN 码的错误信息。
      • 使用 TwoFactorAuthenticator 类验证提供的 PIN 码是否有效。
      • 返回验证结果,成功或失败,并附带相应的错误信息(如果有)。
  2. ValidateForAuthAsync 方法:

    • 参数: 接受一个可选的 account 字符串和一个可选的 pinCode 字符串。
    • 功能:
      • 获取用户的双因素认证信息。
      • 如果用户未绑定双因素认证,返回验证成功(跳过验证)。
      • 如果用户已绑定双因素认证但未提供 PIN 码,返回提示用户输入验证码的错误信息。
      • 验证提供的 PIN 码的有效性。
      • 返回验证结果,成功或失败,并附带相应的错误信息(如果有)。

总结

PinCodeValidator 类的主要功能是处理用户的双因素认证 PIN 码的验证逻辑。它通过与配置和用户服务的交互,确保用户在需要时提供有效的 PIN 码,并根据用户的绑定状态和输入的有效性返回相应的结果。这种设计有助于增强应用程序的安全性,确保只有经过验证的用户才能访问敏感操作。

loading