Skip to content

Commit

Permalink
Rudimentary support for new access tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
yaakov-h committed Mar 23, 2023
1 parent b31d79f commit 0112ca7
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 66 deletions.
8 changes: 5 additions & 3 deletions DepotDownloader/AccountSettingsStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ class AccountSettingsStore
[ProtoMember(2, IsRequired = false)]
public ConcurrentDictionary<string, int> ContentServerPenalty { get; private set; }

[ProtoMember(3, IsRequired = false)]
public Dictionary<string, string> LoginKeys { get; private set; }
// Member 3 was a Dictionary<string, string> for LoginKeys.

[ProtoMember(4, IsRequired = false)]
public Dictionary<string, string> LoginTokens { get; private set; }

string FileName;

AccountSettingsStore()
{
SentryData = new Dictionary<string, byte[]>();
ContentServerPenalty = new ConcurrentDictionary<string, int>();
LoginKeys = new Dictionary<string, string>();
LoginTokens = new Dictionary<string, string>();
}

static bool Loaded
Expand Down
9 changes: 4 additions & 5 deletions DepotDownloader/ContentDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,20 +341,20 @@ static string GetAppOrDepotName(uint depotId, uint appId)

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"
}
);
Expand All @@ -381,7 +381,6 @@ public static void ShutdownSteam3()
if (steam3 == null)
return;

steam3.TryWaitForLoginKey();
steam3.Disconnect();
}

Expand Down
2 changes: 1 addition & 1 deletion DepotDownloader/DepotDownloader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@

<ItemGroup>
<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>
</Project>
1 change: 0 additions & 1 deletion DepotDownloader/DownloadConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class DownloadConfig
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
Expand Down
5 changes: 1 addition & 4 deletions DepotDownloader/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ ex is ContentDownloaderException

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
{
Expand All @@ -291,9 +291,6 @@ static bool InitializeSteam(string username, string password)
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);
}

Expand Down
94 changes: 42 additions & 52 deletions DepotDownloader/Steam3Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading;
using System.Threading.Tasks;
using SteamKit2;
using SteamKit2.Authentication;
using SteamKit2.Internal;

namespace DepotDownloader
Expand Down Expand Up @@ -53,7 +54,6 @@ public ReadOnlyCollection<SteamApps.LicenseListCallback.License> Licenses
bool bAborted;
bool bExpectingDisconnectRemote;
bool bDidDisconnect;
bool bDidReceiveLoginKey;
bool bIsConnectionRecovery;
int connectionBackoff;
int seq; // more hack fixes
Expand All @@ -79,7 +79,6 @@ public Steam3Session(SteamUser.LogOnDetails details)
this.bAborted = false;
this.bExpectingDisconnectRemote = false;
this.bDidDisconnect = false;
this.bDidReceiveLoginKey = false;
this.seq = 0;

this.AppTokens = new Dictionary<uint, ulong>();
Expand Down Expand Up @@ -112,7 +111,6 @@ public Steam3Session(SteamUser.LogOnDetails details)
this.callbacks.Subscribe<SteamUser.SessionTokenCallback>(SessionTokenCallback);
this.callbacks.Subscribe<SteamApps.LicenseListCallback>(LicenseListCallback);
this.callbacks.Subscribe<SteamUser.UpdateMachineAuthCallback>(UpdateMachineAuthCallback);
this.callbacks.Subscribe<SteamUser.LoginKeyCallback>(LoginKeyCallback);

Console.Write("Connecting to Steam3...");

Expand Down Expand Up @@ -419,7 +417,6 @@ private void ResetConnectionFlags()
bExpectingDisconnectRemote = false;
bDidDisconnect = false;
bIsConnectionRecovery = false;
bDidReceiveLoginKey = false;
}

void Connect()
Expand Down Expand Up @@ -466,23 +463,6 @@ private void Reconnect()
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));
Expand All @@ -496,7 +476,7 @@ private void WaitForCallbacks()
}
}

private void ConnectedCallback(SteamClient.ConnectedCallback connected)
private async void ConnectedCallback(SteamClient.ConnectedCallback connected)
{
Console.WriteLine(" Done!");
bConnecting = false;
Expand All @@ -508,7 +488,37 @@ private void ConnectedCallback(SteamClient.ConnectedCallback connected)
}
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);
}
}
Expand Down Expand Up @@ -553,14 +563,14 @@ private void LogOnCallback(SteamUser.LoggedOnCallback loggedOn)
{
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.");
}
Expand All @@ -573,23 +583,15 @@ private void LogOnCallback(SteamUser.LoggedOnCallback loggedOn)
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
{
Expand Down Expand Up @@ -699,17 +701,5 @@ private void UpdateMachineAuthCallback(SteamUser.UpdateMachineAuthCallback machi
// 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;
}
}
}

0 comments on commit 0112ca7

Please sign in to comment.