Skip to content

Commit

Permalink
Add guard data to auth sample
Browse files Browse the repository at this point in the history
  • Loading branch information
xPaw committed Nov 6, 2023
1 parent 6e4c1b5 commit e3950f9
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions Samples/1a.Authentication/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// save our logon details
var user = args[ 0 ];
var pass = args[ 1 ];
string previouslyStoredGuardData = null; // For the sake of this sample, we do not persist guard data

// create our steamclient instance
var steamClient = new SteamClient();
Expand Down Expand Up @@ -48,25 +49,43 @@ async void OnConnected( SteamClient.ConnectedCallback callback )
{
Console.WriteLine( "Connected to Steam! Logging in '{0}'...", user );

var shouldRememberPassword = false;

// Begin authenticating via credentials
var authSession = await steamClient.Authentication.BeginAuthSessionViaCredentialsAsync( new AuthSessionDetails
{
Username = user,
Password = pass,
IsPersistentSession = false,
IsPersistentSession = shouldRememberPassword,

// See NewGuardData comment below
GuardData = previouslyStoredGuardData,

/// <see cref="UserConsoleAuthenticator"/> is the default authenticator implemention provided by SteamKit
/// for ease of use which blocks the thread and asks for user input to enter the code.
/// However, if you require special handling (e.g. you have the TOTP secret and can generate codes on the fly),
/// you can implement your own <see cref="SteamKit2.Authentication.IAuthenticator"/>.
Authenticator = new UserConsoleAuthenticator(),
} );

// Starting polling Steam for authentication response
var pollResponse = await authSession.PollingWaitForResultAsync();

if ( pollResponse.NewGuardData != null )
{
// When using certain two factor methods (such as email 2fa), guard data may be provided by Steam
// for use in future authentication sessions to avoid triggering 2FA again (this works similarly to the old sentry file system).
// Do note that this guard data is also a JWT token and has an expiration date.
previouslyStoredGuardData = pollResponse.NewGuardData;
}

// Logon to Steam with the access token we have received
// Note that we are using RefreshToken for logging on here
steamUser.LogOn( new SteamUser.LogOnDetails
{
Username = pollResponse.AccountName,
AccessToken = pollResponse.RefreshToken,
ShouldRememberPassword = false, // If you set IsPersistentSession to true, this also must be set to true for it to work correctly
ShouldRememberPassword = shouldRememberPassword, // If you set IsPersistentSession to true, this also must be set to true for it to work correctly
} );

// This is not required, but it is possible to parse the JWT access token to see the scope and expiration date.
Expand Down

0 comments on commit e3950f9

Please sign in to comment.