1
0
mirror of https://github.com/bitwarden/mobile synced 2026-01-02 16:43:20 +00:00
Files
mobile/src/App/Utilities/TotpHelper.cs
André Bispo 2f4cd36595 [SG-671] OTP Menu Screen causes Crash on Android (#2097)
* [SG-671] removed unnecessary calc of otpauth period. protected cal of otpauth from crashing the app if url has a wrong format.

* [SG-671] changed logger

* [SG-671] Refactored GetQueryParams code to used HttpUtility.ParseQueryString.

* [SG-671] refactor and null protection.

* [SG-671] code format

* [SG-671] fixed bug where totp circle countdown was fixed to 30.

* [SG-167] added fallback for uri check. Changed all default totp timers to constant.

* [SG-671] missed unsaved file

* [SG-671] simplified code
2022-09-26 17:51:03 +01:00

59 lines
1.8 KiB
C#

using System;
using System.Threading.Tasks;
using Bit.Core;
using Bit.Core.Abstractions;
using Bit.Core.Models.View;
using Bit.Core.Utilities;
namespace Bit.App.Utilities
{
public class TotpHelper
{
private ITotpService _totpService;
private CipherView _cipher;
public TotpHelper(CipherView cipher)
{
_totpService = ServiceContainer.Resolve<ITotpService>("totpService");
_cipher = cipher;
Interval = _totpService.GetTimeInterval(cipher?.Login?.Totp);
}
public string TotpSec { get; private set; }
public string TotpCodeFormatted { get; private set; }
public double Progress { get; private set; }
public double Interval { get; private set; } = Constants.TotpDefaultTimer;
public async Task GenerateNewTotpValues()
{
var epoc = CoreHelpers.EpocUtcNow() / 1000;
var mod = epoc % Interval;
var totpSec = Interval - mod;
TotpSec = totpSec.ToString();
Progress = totpSec * 100 / Interval;
if (mod == 0 || string.IsNullOrEmpty(TotpCodeFormatted))
{
TotpCodeFormatted = await TotpUpdateCodeAsync();
}
}
private async Task<string> TotpUpdateCodeAsync()
{
var totpCode = await _totpService.GetCodeAsync(_cipher?.Login?.Totp);
if (totpCode == null)
{
return null;
}
if (totpCode.Length <= 4)
{
return totpCode;
}
var half = (int)Math.Floor(totpCode.Length / 2M);
return string.Format("{0} {1}", totpCode.Substring(0, half),
totpCode.Substring(half));
}
}
}