Rudimentary support for new access tokens

pull/401/head
Yaakov 3 years ago
parent b31d79f556
commit 0112ca749d

@ -17,8 +17,10 @@ namespace DepotDownloader
[ProtoMember(2, IsRequired = false)] [ProtoMember(2, IsRequired = false)]
public ConcurrentDictionary<string, int> ContentServerPenalty { get; private set; } public ConcurrentDictionary<string, int> ContentServerPenalty { get; private set; }
[ProtoMember(3, IsRequired = false)] // Member 3 was a Dictionary<string, string> for LoginKeys.
public Dictionary<string, string> LoginKeys { get; private set; }
[ProtoMember(4, IsRequired = false)]
public Dictionary<string, string> LoginTokens { get; private set; }
string FileName; string FileName;
@ -26,7 +28,7 @@ namespace DepotDownloader
{ {
SentryData = new Dictionary<string, byte[]>(); SentryData = new Dictionary<string, byte[]>();
ContentServerPenalty = new ConcurrentDictionary<string, int>(); ContentServerPenalty = new ConcurrentDictionary<string, int>();
LoginKeys = new Dictionary<string, string>(); LoginTokens = new Dictionary<string, string>();
} }
static bool Loaded static bool Loaded

@ -341,20 +341,20 @@ namespace DepotDownloader
public static bool InitializeSteam3(string username, string password) public static bool InitializeSteam3(string username, string password)
{ {
string loginKey = null; string loginToken = null;
if (username != null && Config.RememberPassword) if (username != null && Config.RememberPassword)
{ {
_ = AccountSettingsStore.Instance.LoginKeys.TryGetValue(username, out loginKey); _ = AccountSettingsStore.Instance.LoginTokens.TryGetValue(username, out loginToken);
} }
steam3 = new Steam3Session( steam3 = new Steam3Session(
new SteamUser.LogOnDetails new SteamUser.LogOnDetails
{ {
Username = username, Username = username,
Password = loginKey == null ? password : null, Password = loginToken == null ? password : null,
ShouldRememberPassword = Config.RememberPassword, ShouldRememberPassword = Config.RememberPassword,
LoginKey = loginKey, AccessToken = loginToken,
LoginID = Config.LoginID ?? 0x534B32, // "SK2" LoginID = Config.LoginID ?? 0x534B32, // "SK2"
} }
); );
@ -381,7 +381,6 @@ namespace DepotDownloader
if (steam3 == null) if (steam3 == null)
return; return;
steam3.TryWaitForLoginKey();
steam3.Disconnect(); steam3.Disconnect();
} }

@ -12,6 +12,6 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="protobuf-net" Version="3.2.16" /> <PackageReference Include="protobuf-net" Version="3.2.16" />
<PackageReference Include="SteamKit2" Version="2.4.1" /> <PackageReference Include="SteamKit2" Version="2.5.0-Beta.1" />
</ItemGroup> </ItemGroup>
</Project> </Project>

@ -22,7 +22,6 @@ namespace DepotDownloader
public int MaxServers { get; set; } public int MaxServers { get; set; }
public int MaxDownloads { get; set; } public int MaxDownloads { get; set; }
public string SuppliedPassword { get; set; }
public bool RememberPassword { get; set; } public bool RememberPassword { get; set; }
// A Steam LoginID to allow multiple concurrent connections // A Steam LoginID to allow multiple concurrent connections

@ -268,7 +268,7 @@ namespace DepotDownloader
static bool InitializeSteam(string username, string password) 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 do
{ {
@ -291,9 +291,6 @@ namespace DepotDownloader
Console.WriteLine("No username given. Using anonymous account with dedicated server subscription."); 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); return ContentDownloader.InitializeSteam3(username, password);
} }

@ -7,6 +7,7 @@ using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using SteamKit2; using SteamKit2;
using SteamKit2.Authentication;
using SteamKit2.Internal; using SteamKit2.Internal;
namespace DepotDownloader namespace DepotDownloader
@ -53,7 +54,6 @@ namespace DepotDownloader
bool bAborted; bool bAborted;
bool bExpectingDisconnectRemote; bool bExpectingDisconnectRemote;
bool bDidDisconnect; bool bDidDisconnect;
bool bDidReceiveLoginKey;
bool bIsConnectionRecovery; bool bIsConnectionRecovery;
int connectionBackoff; int connectionBackoff;
int seq; // more hack fixes int seq; // more hack fixes
@ -79,7 +79,6 @@ namespace DepotDownloader
this.bAborted = false; this.bAborted = false;
this.bExpectingDisconnectRemote = false; this.bExpectingDisconnectRemote = false;
this.bDidDisconnect = false; this.bDidDisconnect = false;
this.bDidReceiveLoginKey = false;
this.seq = 0; this.seq = 0;
this.AppTokens = new Dictionary<uint, ulong>(); this.AppTokens = new Dictionary<uint, ulong>();
@ -112,7 +111,6 @@ namespace DepotDownloader
this.callbacks.Subscribe<SteamUser.SessionTokenCallback>(SessionTokenCallback); this.callbacks.Subscribe<SteamUser.SessionTokenCallback>(SessionTokenCallback);
this.callbacks.Subscribe<SteamApps.LicenseListCallback>(LicenseListCallback); this.callbacks.Subscribe<SteamApps.LicenseListCallback>(LicenseListCallback);
this.callbacks.Subscribe<SteamUser.UpdateMachineAuthCallback>(UpdateMachineAuthCallback); this.callbacks.Subscribe<SteamUser.UpdateMachineAuthCallback>(UpdateMachineAuthCallback);
this.callbacks.Subscribe<SteamUser.LoginKeyCallback>(LoginKeyCallback);
Console.Write("Connecting to Steam3..."); Console.Write("Connecting to Steam3...");
@ -419,7 +417,6 @@ namespace DepotDownloader
bExpectingDisconnectRemote = false; bExpectingDisconnectRemote = false;
bDidDisconnect = false; bDidDisconnect = false;
bIsConnectionRecovery = false; bIsConnectionRecovery = false;
bDidReceiveLoginKey = false;
} }
void Connect() void Connect()
@ -466,23 +463,6 @@ namespace DepotDownloader
steamClient.Disconnect(); 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() private void WaitForCallbacks()
{ {
callbacks.RunWaitCallbacks(TimeSpan.FromSeconds(1)); 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!"); Console.WriteLine(" Done!");
bConnecting = false; bConnecting = false;
@ -508,7 +488,37 @@ namespace DepotDownloader
} }
else 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); steamUser.LogOn(logonDetails);
} }
} }
@ -553,14 +563,14 @@ namespace DepotDownloader
{ {
var isSteamGuard = loggedOn.Result == EResult.AccountLogonDenied; var isSteamGuard = loggedOn.Result == EResult.AccountLogonDenied;
var is2FA = loggedOn.Result == EResult.AccountLoginDeniedNeedTwoFactor; 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; bExpectingDisconnectRemote = true;
Abort(false); Abort(false);
if (!isLoginKey) if (!isAccessToken)
{ {
Console.WriteLine("This account is protected by Steam Guard."); Console.WriteLine("This account is protected by Steam Guard.");
} }
@ -573,23 +583,15 @@ namespace DepotDownloader
logonDetails.TwoFactorCode = Console.ReadLine(); logonDetails.TwoFactorCode = Console.ReadLine();
} while (String.Empty == logonDetails.TwoFactorCode); } 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(); AccountSettingsStore.Save();
logonDetails.LoginKey = null; // TODO: Handle gracefully by falling back to password prompt?
Console.WriteLine("Access token was rejected.");
if (ContentDownloader.Config.SuppliedPassword != null) Abort(false);
{ return;
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();
}
} }
else else
{ {
@ -699,17 +701,5 @@ namespace DepotDownloader
// send off our response // send off our response
steamUser.SendMachineAuthResponse(authResponse); 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;
}
} }
} }

Loading…
Cancel
Save