Skip to content

Commit

Permalink
Fixes from upstream changes
Browse files Browse the repository at this point in the history
  • Loading branch information
trowgundam committed Mar 9, 2022
1 parent 2c300bc commit 7f91b59
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 28 deletions.
4 changes: 0 additions & 4 deletions src/XIVLauncher/Windows/OtpUriSetupWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using NuGet;
using XIVLauncher.Addon;
using XIVLauncher.Windows.ViewModel;
using CheckBox = System.Windows.Controls.CheckBox;

Expand Down
76 changes: 58 additions & 18 deletions src/XIVLauncher/Windows/ViewModel/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,31 +206,71 @@ public async Task Login(string username, string password, bool isOtp, bool isSte

if (isOtp && (!hasValidCache || action == AfterLoginAction.Repair))
{
var otpDialog = App.OtpInputDialog;
otpDialog.Dispatcher.Invoke(() =>
if (!string.IsNullOrWhiteSpace(AccountManager?.CurrentAccount?.OtpUri) && !PreviousLoginFailure)
{
otpDialog.Reset();
otpDialog.Show();
otpDialog.Activate();
});
otpDialog.OnResult += OnOtpResult;

void OnOtpResult(string result)
{
if (AccountManager.CurrentAccount != null && result != null && AccountManager.CurrentAccount.LastSuccessfulOtp == result)
try
{
otpDialog.IgnoreCurrentResult(Loc.Localize("DuplicateOtpAfterSuccess",
"This OTP has been already used.\nIt may take up to 30 seconds for a new one."));
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 = 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;

var algorithm = "sha1";
if (query.AllKeys.Contains("algorithm") || new[] { "sha1", "sha256", "sha512" }.Any(a => a.Equals(query["algorithm"], StringComparison.OrdinalIgnoreCase)))
algorithm = query["algorithm"].ToLowerInvariant();

otp = Util.GetTotpToken(secretKey, algorithm, digits, period);
}
else
{
var secretKey = AccountManager.CurrentAccount.OtpUri;
otp = Util.GetTotpToken(secretKey);
}
}
else
catch (Exception)
{
otp = result;
signal.Set();
otp = "";
}
}

signal.WaitOne();
otpDialog.OnResult -= OnOtpResult;
if (string.IsNullOrWhiteSpace(otp))
{
var otpDialog = App.OtpInputDialog;
otpDialog.Dispatcher.Invoke(() =>
{
otpDialog.Reset();
otpDialog.Show();
otpDialog.Activate();
});
otpDialog.OnResult += OnOtpResult;

void OnOtpResult(string result)
{
if (AccountManager.CurrentAccount != null && result != null && AccountManager.CurrentAccount.LastSuccessfulOtp == result)
{
otpDialog.IgnoreCurrentResult(Loc.Localize("DuplicateOtpAfterSuccess",
"This OTP has been already used.\nIt may take up to 30 seconds for a new one."));
}
else
{
otp = result;
signal.Set();
}
}

signal.WaitOne();
otpDialog.OnResult -= OnOtpResult;
}
}

if (otp == null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CheapLoc;
using CheapLoc;

namespace XIVLauncher.Windows.ViewModel
{
Expand Down

0 comments on commit 7f91b59

Please sign in to comment.