|
| 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 DefaultTotpService(IOptionsMonitor<OtpOptions> optionsMonitor) : ITotpService |
| 11 | +{ |
| 12 | + [NotNull] |
| 13 | + public TotpInstanceBase? Instance { get; private set; } |
| 14 | + |
| 15 | + public string GenerateOtpUri(OtpOptions? options = null) |
| 16 | + { |
| 17 | + options ??= optionsMonitor.CurrentValue; |
| 18 | + var type = options.Type.ToType(); |
| 19 | + var mode = options.Algorithm.ToMode(); |
| 20 | + var uri = new OtpUri(type, options.SecretKey, options.UserName, options.IssuerName, mode, options.Digits, options.Period, options.Counter); |
| 21 | + return uri.ToString(); |
| 22 | + } |
| 23 | + |
| 24 | + public string Compute(string secretKey, int period = 30, OtpHashMode mode = OtpHashMode.Sha1, int digits = 6, DateTime? timestamp = null) |
| 25 | + { |
| 26 | + var instance = new Totp(Base32Encoding.ToBytes(secretKey), period, mode.ToMode(), digits, timeCorrection: null); |
| 27 | + Instance = new DefaultTotpInstance(instance); |
| 28 | + return timestamp == null ? instance.ComputeTotp() : instance.ComputeTotp(timestamp.Value); |
| 29 | + } |
| 30 | + |
| 31 | + public int GetRemainingSeconds(DateTime? timestamp = null) |
| 32 | + { |
| 33 | + if (Instance != null) |
| 34 | + { |
| 35 | + return timestamp == null ? Instance.GetRemainingSeconds() : Instance.GetRemainingSeconds(timestamp.Value); |
| 36 | + } |
| 37 | + var instance = new Totp(Base32Encoding.ToBytes("OMM2LVLFX6QJHMYI")); |
| 38 | + return timestamp == null ? instance.RemainingSeconds() : instance.RemainingSeconds(timestamp.Value); |
| 39 | + } |
| 40 | + |
| 41 | + public string GenerateSecretKey(int length = 20) |
| 42 | + { |
| 43 | + var secretKey = KeyGeneration.GenerateRandomKey(length); |
| 44 | + return Base32Encoding.ToString(secretKey); |
| 45 | + } |
| 46 | + |
| 47 | + public byte[] GetSecretKeyBytes(string input) |
| 48 | + { |
| 49 | + return Base32Encoding.ToBytes(input); |
| 50 | + } |
| 51 | + |
| 52 | + public bool Verify(string code, DateTime? timestamp = null) |
| 53 | + { |
| 54 | + if (Instance != null) |
| 55 | + { |
| 56 | + return timestamp == null ? Instance.Verify(code) : Instance.Verify(code, timestamp.Value); |
| 57 | + } |
| 58 | + var instance = new Totp(Base32Encoding.ToBytes("OMM2LVLFX6QJHMYI")); |
| 59 | + return timestamp == null ? instance.VerifyTotp(code, out _) : instance.VerifyTotp(timestamp.Value, code, out _); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +class DefaultTotpInstance(Totp instance) : TotpInstanceBase |
| 64 | +{ |
| 65 | + public override int GetRemainingSeconds(DateTime? timestamp = null) |
| 66 | + { |
| 67 | + return timestamp == null ? instance.RemainingSeconds() : instance.RemainingSeconds(timestamp.Value); |
| 68 | + } |
| 69 | + |
| 70 | + public override bool Verify(string code, DateTime? timestamp = null) |
| 71 | + { |
| 72 | + return timestamp == null ? instance.VerifyTotp(code, out _) : instance.VerifyTotp(timestamp.Value, code, out _); |
| 73 | + } |
| 74 | +} |
0 commit comments