/// <summary> /// Pass in a valid Base-32 encoded TOTP-Secret; receive a valid 2FA token.<para> </para> /// If the token would be closer than 2 seconds to expiry, the method waits for a fresh one to avoid authentication failures due to latency. /// </summary> /// <param name="totpSecret">The Base-32 encoded secret to use for TOTP generation. If this is <c>null</c> or empty, <c>null</c> is returned.</param> /// <returns>The 6-cipher string containing the TOTP; <c>null</c> if generation failed for some reason (e.g. invalid <paramref name="totpSecret"/> parameter).</returns> public async Task <string> GetTotp(string totpSecret) { if (totpSecret.NullOrEmpty()) { return(null); } try { var totp = new OtpNet.Totp(Base32Encoding.ToBytes(totpSecret)); if (totp.RemainingSeconds() < 2) { await Task.Delay(1250); } return(totp.ComputeTotp()); } catch (Exception) { return(null); } }
public DateTime GetRenewTime() { return(DateTime.UtcNow.AddSeconds(_totp.RemainingSeconds())); }