mirror of
https://github.com/bitwarden/mobile
synced 2026-01-04 17:43:17 +00:00
support otpath:// totp secrets
This commit is contained in:
58
src/App/Models/OtpAuth.cs
Normal file
58
src/App/Models/OtpAuth.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using Bit.App.Utilities;
|
||||
using PCLCrypto;
|
||||
|
||||
namespace Bit.App.Models
|
||||
{
|
||||
public class OtpAuth
|
||||
{
|
||||
public OtpAuth(string key)
|
||||
{
|
||||
if(key?.ToLowerInvariant().StartsWith("otpauth://") ?? false)
|
||||
{
|
||||
var qsParams = Helpers.GetQueryParams(key);
|
||||
if(qsParams.ContainsKey("digits") && qsParams["digits"] != null &&
|
||||
int.TryParse(qsParams["digits"].Trim(), out var digitParam))
|
||||
{
|
||||
if(digitParam > 10)
|
||||
{
|
||||
Digits = 10;
|
||||
}
|
||||
else if(digitParam > 0)
|
||||
{
|
||||
Digits = digitParam;
|
||||
}
|
||||
}
|
||||
if(qsParams.ContainsKey("period") && qsParams["period"] != null &&
|
||||
int.TryParse(qsParams["period"].Trim(), out var periodParam) && periodParam > 0)
|
||||
{
|
||||
Period = periodParam;
|
||||
}
|
||||
if(qsParams.ContainsKey("secret") && qsParams["secret"] != null)
|
||||
{
|
||||
Secret = qsParams["secret"];
|
||||
}
|
||||
if(qsParams.ContainsKey("algorithm") && qsParams["algorithm"] != null)
|
||||
{
|
||||
var algParam = qsParams["algorithm"].ToLowerInvariant();
|
||||
if(algParam == "sha256")
|
||||
{
|
||||
Algorithm = MacAlgorithm.HmacSha256;
|
||||
}
|
||||
else if(algParam == "sha512")
|
||||
{
|
||||
Algorithm = MacAlgorithm.HmacSha512;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Secret = key;
|
||||
}
|
||||
}
|
||||
|
||||
public int Period { get; set; } = 30;
|
||||
public int Digits { get; set; } = 6;
|
||||
public MacAlgorithm Algorithm { get; set; } = MacAlgorithm.HmacSha1;
|
||||
public string Secret { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -178,8 +178,18 @@ namespace Bit.App.Models.Page
|
||||
public bool LoginTotpLow => LoginTotpSecond <= 7;
|
||||
public Color LoginTotpColor => !string.IsNullOrWhiteSpace(LoginTotpCode) && LoginTotpLow ?
|
||||
Color.Red : Color.Black;
|
||||
public string LoginTotpCodeFormatted => !string.IsNullOrWhiteSpace(LoginTotpCode) ?
|
||||
string.Format("{0} {1}", LoginTotpCode.Substring(0, 3), LoginTotpCode.Substring(3)) : null;
|
||||
public string LoginTotpCodeFormatted
|
||||
{
|
||||
get
|
||||
{
|
||||
if(string.IsNullOrWhiteSpace(LoginTotpCode) || LoginTotpCode.Length < 5)
|
||||
{
|
||||
return LoginTotpCode;
|
||||
}
|
||||
var half = (int)Math.Floor((double)LoginTotpCode.Length / 2);
|
||||
return string.Format("{0} {1}", LoginTotpCode.Substring(0, half), LoginTotpCode.Substring(half));
|
||||
}
|
||||
}
|
||||
|
||||
// Card
|
||||
public string CardName
|
||||
|
||||
Reference in New Issue
Block a user