Skip to content

Commit

Permalink
OTP Generation re-write to fit new MVVM design
Browse files Browse the repository at this point in the history
  • Loading branch information
trowgundam committed Jan 25, 2022
1 parent 7b79724 commit ff1dc9f
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/XIVLauncher/Windows/ViewModel/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace XIVLauncher.Windows.ViewModel
{
class MainWindowViewModel : INotifyPropertyChanged
{
public bool PreviousLoginFailure = false;
public bool IsLoggingIn;

private readonly Launcher _launcher = new();
Expand Down Expand Up @@ -365,6 +366,7 @@ private async Task LoginToGame(string username, string password, string otp, boo
}
catch (OauthLoginException oauthLoginException)
{
PreviousLoginFailure = true;
var failedOauthMessage = oauthLoginException.Message.Replace("\\r\\n", "\n").Replace("\r\n", "\n");
if (App.Settings.AutologinEnabled)
{
Expand Down Expand Up @@ -413,6 +415,7 @@ private async Task LoginToGame(string username, string password, string otp, boo
return;
}

PreviousLoginFailure = true;
ErrorWindow.Show(ex, "Please also check your login information or try again.", "Login");
Reactivate();
}
Expand Down Expand Up @@ -602,6 +605,49 @@ public void OnWindowClosing(object sender, CancelEventArgs args)

private string AskForOtp()
{
var otp = "";
if (!string.IsNullOrWhiteSpace(AccountManager?.CurrentAccount?.OtpUri) && !PreviousLoginFailure)
{
try
{
OtpNet.Totp totp;

if (Uri.TryCreate(AccountManager.CurrentAccount.OtpUri, UriKind.Absolute, out var uri))
{
var query = System.Web.HttpUtility.ParseQueryString(uri.Query);

if (!query.AllKeys.Contains("secret"))
{
throw new Exception("No Secret");
}

var secretKey = OtpNet.Base32Encoding.ToBytes(query["secret"]);
if (!query.AllKeys.Contains("period") || !int.TryParse(query["period"], out var period))
period = 30;
if (!query.AllKeys.Contains("digits") || !int.TryParse(query["digits"], out var digits))
digits = 6;
if (!query.AllKeys.Contains("algorithm") || !Enum.TryParse(query["algorithm"], true, out OtpNet.OtpHashMode algorithm))
algorithm = OtpNet.OtpHashMode.Sha1;

totp = new OtpNet.Totp(secretKey, step: period, mode: algorithm, totpSize: digits);
}
else
{
var secretKey = OtpNet.Base32Encoding.ToBytes(AccountManager.CurrentAccount.OtpUri);
totp = new OtpNet.Totp(secretKey);
}

otp = totp.ComputeTotp();
}
catch (Exception)
{
otp = "";
}
}

if (!string.IsNullOrWhiteSpace(otp))
return otp;

var otpDialog = OtpInputDialogFactory();
otpDialog.ShowDialog();

Expand Down

0 comments on commit ff1dc9f

Please sign in to comment.