diff --git a/DepotDownloader/AccountSettingsStore.cs b/DepotDownloader/AccountSettingsStore.cs index c6c9a25..8bed962 100644 --- a/DepotDownloader/AccountSettingsStore.cs +++ b/DepotDownloader/AccountSettingsStore.cs @@ -17,8 +17,10 @@ namespace DepotDownloader [ProtoMember(2, IsRequired = false)] public ConcurrentDictionary ContentServerPenalty { get; private set; } - [ProtoMember(3, IsRequired = false)] - public Dictionary LoginKeys { get; private set; } + // Member 3 was a Dictionary for LoginKeys. + + [ProtoMember(4, IsRequired = false)] + public Dictionary LoginTokens { get; private set; } string FileName; @@ -26,7 +28,7 @@ namespace DepotDownloader { SentryData = new Dictionary(); ContentServerPenalty = new ConcurrentDictionary(); - LoginKeys = new Dictionary(); + LoginTokens = new Dictionary(); } static bool Loaded diff --git a/DepotDownloader/ContentDownloader.cs b/DepotDownloader/ContentDownloader.cs index 7cee83f..1a57dc6 100644 --- a/DepotDownloader/ContentDownloader.cs +++ b/DepotDownloader/ContentDownloader.cs @@ -341,20 +341,20 @@ namespace DepotDownloader public static bool InitializeSteam3(string username, string password) { - string loginKey = null; + string loginToken = null; if (username != null && Config.RememberPassword) { - _ = AccountSettingsStore.Instance.LoginKeys.TryGetValue(username, out loginKey); + _ = AccountSettingsStore.Instance.LoginTokens.TryGetValue(username, out loginToken); } steam3 = new Steam3Session( new SteamUser.LogOnDetails { Username = username, - Password = loginKey == null ? password : null, + Password = loginToken == null ? password : null, ShouldRememberPassword = Config.RememberPassword, - LoginKey = loginKey, + AccessToken = loginToken, LoginID = Config.LoginID ?? 0x534B32, // "SK2" } ); @@ -381,7 +381,6 @@ namespace DepotDownloader if (steam3 == null) return; - steam3.TryWaitForLoginKey(); steam3.Disconnect(); } diff --git a/DepotDownloader/DepotDownloader.csproj b/DepotDownloader/DepotDownloader.csproj index 3fadc13..c64d005 100644 --- a/DepotDownloader/DepotDownloader.csproj +++ b/DepotDownloader/DepotDownloader.csproj @@ -12,6 +12,6 @@ - + diff --git a/DepotDownloader/DownloadConfig.cs b/DepotDownloader/DownloadConfig.cs index 448e589..bcb46cb 100644 --- a/DepotDownloader/DownloadConfig.cs +++ b/DepotDownloader/DownloadConfig.cs @@ -22,7 +22,6 @@ namespace DepotDownloader public int MaxServers { get; set; } public int MaxDownloads { get; set; } - public string SuppliedPassword { get; set; } public bool RememberPassword { get; set; } // A Steam LoginID to allow multiple concurrent connections diff --git a/DepotDownloader/Program.cs b/DepotDownloader/Program.cs index e4ceb00..9c07fbb 100644 --- a/DepotDownloader/Program.cs +++ b/DepotDownloader/Program.cs @@ -268,7 +268,7 @@ namespace DepotDownloader static bool InitializeSteam(string username, string password) { - if (username != null && password == null && (!ContentDownloader.Config.RememberPassword || !AccountSettingsStore.Instance.LoginKeys.ContainsKey(username))) + if (username != null && password == null && (!ContentDownloader.Config.RememberPassword || !AccountSettingsStore.Instance.LoginTokens.ContainsKey(username))) { do { @@ -291,9 +291,6 @@ namespace DepotDownloader Console.WriteLine("No username given. Using anonymous account with dedicated server subscription."); } - // capture the supplied password in case we need to re-use it after checking the login key - ContentDownloader.Config.SuppliedPassword = password; - return ContentDownloader.InitializeSteam3(username, password); } diff --git a/DepotDownloader/Steam3Session.cs b/DepotDownloader/Steam3Session.cs index 071981f..d88fecc 100644 --- a/DepotDownloader/Steam3Session.cs +++ b/DepotDownloader/Steam3Session.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using SteamKit2; +using SteamKit2.Authentication; using SteamKit2.Internal; namespace DepotDownloader @@ -53,7 +54,6 @@ namespace DepotDownloader bool bAborted; bool bExpectingDisconnectRemote; bool bDidDisconnect; - bool bDidReceiveLoginKey; bool bIsConnectionRecovery; int connectionBackoff; int seq; // more hack fixes @@ -79,7 +79,6 @@ namespace DepotDownloader this.bAborted = false; this.bExpectingDisconnectRemote = false; this.bDidDisconnect = false; - this.bDidReceiveLoginKey = false; this.seq = 0; this.AppTokens = new Dictionary(); @@ -112,7 +111,6 @@ namespace DepotDownloader this.callbacks.Subscribe(SessionTokenCallback); this.callbacks.Subscribe(LicenseListCallback); this.callbacks.Subscribe(UpdateMachineAuthCallback); - this.callbacks.Subscribe(LoginKeyCallback); Console.Write("Connecting to Steam3..."); @@ -419,7 +417,6 @@ namespace DepotDownloader bExpectingDisconnectRemote = false; bDidDisconnect = false; bIsConnectionRecovery = false; - bDidReceiveLoginKey = false; } void Connect() @@ -466,23 +463,6 @@ namespace DepotDownloader steamClient.Disconnect(); } - public void TryWaitForLoginKey() - { - if (logonDetails.Username == null || !credentials.LoggedOn || !ContentDownloader.Config.RememberPassword) return; - - var totalWaitPeriod = DateTime.Now.AddSeconds(3); - - while (true) - { - var now = DateTime.Now; - if (now >= totalWaitPeriod) break; - - if (bDidReceiveLoginKey) break; - - callbacks.RunWaitAllCallbacks(TimeSpan.FromMilliseconds(100)); - } - } - private void WaitForCallbacks() { callbacks.RunWaitCallbacks(TimeSpan.FromSeconds(1)); @@ -496,7 +476,7 @@ namespace DepotDownloader } } - private void ConnectedCallback(SteamClient.ConnectedCallback connected) + private async void ConnectedCallback(SteamClient.ConnectedCallback connected) { Console.WriteLine(" Done!"); bConnecting = false; @@ -508,7 +488,37 @@ namespace DepotDownloader } else { - Console.Write("Logging '{0}' into Steam3...", logonDetails.Username); + Console.WriteLine("Logging '{0}' into Steam3...", logonDetails.Username); + + if (logonDetails.Username != null && logonDetails.Password != null && logonDetails.AccessToken is null) + { + try + { + var session = await steamClient.Authentication.BeginAuthSessionViaCredentialsAsync(new SteamKit2.Authentication.AuthSessionDetails + { + Username = logonDetails.Username, + Password = logonDetails.Password, + IsPersistentSession = ContentDownloader.Config.RememberPassword, + Authenticator = new UserConsoleAuthenticator(), + }); + + var result = await session.PollingWaitForResultAsync(); + + DebugLog.WriteLine(nameof(Steam3Session), "Completed authentication initialization, got access token."); + + // Assume that we get back the same username, no need to reset it. + logonDetails.Password = null; + logonDetails.AccessToken = result.RefreshToken; + + AccountSettingsStore.Instance.LoginTokens[result.AccountName] = result.RefreshToken; + AccountSettingsStore.Save(); + } + catch (Exception ex) + { + Console.Error.WriteLine("Failed to authenticate with Steam: " + ex.Message); + Abort(false); + } + } steamUser.LogOn(logonDetails); } } @@ -553,14 +563,14 @@ namespace DepotDownloader { var isSteamGuard = loggedOn.Result == EResult.AccountLogonDenied; var is2FA = loggedOn.Result == EResult.AccountLoginDeniedNeedTwoFactor; - var isLoginKey = ContentDownloader.Config.RememberPassword && logonDetails.LoginKey != null && loggedOn.Result == EResult.InvalidPassword; + var isAccessToken = ContentDownloader.Config.RememberPassword && logonDetails.AccessToken != null && loggedOn.Result == EResult.InvalidPassword; // TODO: Get EResult for bad access token - if (isSteamGuard || is2FA || isLoginKey) + if (isSteamGuard || is2FA || isAccessToken) { bExpectingDisconnectRemote = true; Abort(false); - if (!isLoginKey) + if (!isAccessToken) { Console.WriteLine("This account is protected by Steam Guard."); } @@ -573,23 +583,15 @@ namespace DepotDownloader logonDetails.TwoFactorCode = Console.ReadLine(); } while (String.Empty == logonDetails.TwoFactorCode); } - else if (isLoginKey) + else if (isAccessToken) { - AccountSettingsStore.Instance.LoginKeys.Remove(logonDetails.Username); + AccountSettingsStore.Instance.LoginTokens.Remove(logonDetails.Username); AccountSettingsStore.Save(); - logonDetails.LoginKey = null; - - if (ContentDownloader.Config.SuppliedPassword != null) - { - Console.WriteLine("Login key was expired. Connecting with supplied password."); - logonDetails.Password = ContentDownloader.Config.SuppliedPassword; - } - else - { - Console.Write("Login key was expired. Please enter your password: "); - logonDetails.Password = Util.ReadPassword(); - } + // TODO: Handle gracefully by falling back to password prompt? + Console.WriteLine("Access token was rejected."); + Abort(false); + return; } else { @@ -699,17 +701,5 @@ namespace DepotDownloader // send off our response steamUser.SendMachineAuthResponse(authResponse); } - - private void LoginKeyCallback(SteamUser.LoginKeyCallback loginKey) - { - Console.WriteLine("Accepted new login key for account {0}", logonDetails.Username); - - AccountSettingsStore.Instance.LoginKeys[logonDetails.Username] = loginKey.LoginKey; - AccountSettingsStore.Save(); - - steamUser.AcceptNewLoginKey(loginKey); - - bDidReceiveLoginKey = true; - } } }