Skip to content

Commit

Permalink
Add GenerateAccessTokenForAppAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
xPaw committed Oct 18, 2023
1 parent 68beb55 commit 4e5986d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* This file is subject to the terms and conditions defined in
* file 'license.txt', which is part of this source code package.
*/

using SteamKit2.Internal;

namespace SteamKit2.Authentication
{
/// <summary>
/// Represents access token generation result.
/// </summary>
public sealed class AccessTokenGenerateResult
{
/// <summary>
/// New refresh token.
/// This can be provided to <see cref="SteamUser.LogOnDetails.AccessToken"/>.
/// </summary>
public string RefreshToken { get; }
/// <summary>
/// New token subordinate to <see cref="RefreshToken"/>.
/// </summary>
public string AccessToken { get; }

internal AccessTokenGenerateResult( CAuthentication_AccessToken_GenerateForApp_Response response )
{
AccessToken = response.access_token;
RefreshToken = response.refresh_token;
}
}
}
26 changes: 26 additions & 0 deletions SteamKit2/SteamKit2/Steam/Authentication/SteamAuthentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,32 @@ async Task<CAuthentication_GetPasswordRSAPublicKey_Response> GetPasswordRSAPubli
return response;
}

/// <summary>
/// Given a refresh token for a client app audience (e.g. desktop client / mobile client), generate an access token.
/// </summary>
/// <param name="steamID">The SteamID this token belongs to.</param>
/// <param name="refreshToken">The refresh token.</param>
public async Task<AccessTokenGenerateResult> GenerateAccessTokenForAppAsync( SteamID steamID, string refreshToken )
{
var request = new CAuthentication_AccessToken_GenerateForApp_Request
{
refresh_token = refreshToken,
steamid = steamID.ConvertToUInt64(),
renewal_type = ETokenRenewalType.k_ETokenRenewalType_Allow,
};

var message = await AuthenticationService.SendMessage( api => api.GenerateAccessTokenForApp( request ) );

if ( message.Result != EResult.OK )
{
throw new AuthenticationException( "Failed to generate token", message.Result );
}

var response = message.GetDeserializedResponse<CAuthentication_AccessToken_GenerateForApp_Response>();

return new AccessTokenGenerateResult( response );
}

/// <summary>
/// Start the authentication process using QR codes.
/// </summary>
Expand Down

0 comments on commit 4e5986d

Please sign in to comment.