Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ All trademarks are recognised, including but not limited to:

----

## Native AOT integration

Steam saved sessions, refresh-token responses and token expiry payloads use generated JSON metadata. Legacy XML import constructs the supported authenticator types explicitly instead of resolving type names through assembly reflection.

SteamKit2 3.5.0 uses protobuf-net 3.4.30 for the authenticator transfer challenge. Its runtime serializer reads `ProtoContractAttribute` and `ProtoMemberAttribute` properties (including non-public presence flags) and `DefaultValueAttribute.Value` through reflection. The request/response entry points preserve these attribute shapes, the replacement-token message graph, and the constructors/methods of protobuf-net's `DynamicStub.ConcreteStub<T>` and `SubValueSerializer<T>` adapters used to discover nested messages. No entire SteamKit assembly is rooted. The native offline probe verifies both challenge operations, every replacement-token field, fixed64 encoding, default-field presence and unknown-field round trips. New protobuf message families require their own native wire-format validation; this does not establish AOT support for every SteamKit service.

## Author

WinAuth was written by Colin Mackie. Copyright (C) 2010-2017.
Expand Down
6 changes: 6 additions & 0 deletions src/WinAuth/BD.WTTS/Models/Steam/SteamJsonContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ namespace BD.WTTS.Models;
public partial class SteamJsonContext : JsonSerializerContext
{
}

[JsonSerializable(typeof(SteamClient.SteamSession.SteamAccessToken))]
[JsonSerializable(typeof(SteamClient.SteamSession.GenerateAccessTokenForAppResponse))]
internal partial class SteamSessionTokenJsonContext : JsonSerializerContext
{
}
16 changes: 11 additions & 5 deletions src/WinAuth/WinAuth/Authenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,17 @@ public virtual void Sync()
authenticatorType = authenticatorType.Replace(
"WindowsAuthenticator.",
typeof(AuthenticatorValueDTO).FullName + ".");
var authenticatorType_ = Assembly.GetExecutingAssembly().GetType(authenticatorType, false, true);
authenticatorType_ = authenticatorType_.ThrowIsNull();
#pragma warning disable IL2072 // Target parameter argument does not satisfy 'DynamicallyAccessedMembersAttribute' in call to target method. The return value of the source method does not have matching annotations.
authenticator = (AuthenticatorValueDTO?)Activator.CreateInstance(authenticatorType_);
#pragma warning restore IL2072 // Target parameter argument does not satisfy 'DynamicallyAccessedMembersAttribute' in call to target method. The return value of the source method does not have matching annotations.
authenticator = authenticatorType switch
{
var name when string.Equals(name, typeof(AuthenticatorValueDTO).FullName, StringComparison.OrdinalIgnoreCase) => new AuthenticatorValueDTO(),
var name when string.Equals(name, typeof(BattleNetAuthenticator).FullName, StringComparison.OrdinalIgnoreCase) => new BattleNetAuthenticator(),
var name when string.Equals(name, typeof(GoogleAuthenticator).FullName, StringComparison.OrdinalIgnoreCase) => new GoogleAuthenticator(),
var name when string.Equals(name, typeof(HOTPAuthenticator).FullName, StringComparison.OrdinalIgnoreCase) => new HOTPAuthenticator(),
var name when string.Equals(name, typeof(MicrosoftAuthenticator).FullName, StringComparison.OrdinalIgnoreCase) => new MicrosoftAuthenticator(),
var name when string.Equals(name, typeof(SteamAuthenticator).FullName, StringComparison.OrdinalIgnoreCase) => new SteamAuthenticator(),
var name when string.Equals(name, typeof(TOTPAuthenticator).FullName, StringComparison.OrdinalIgnoreCase) => new TOTPAuthenticator(),
_ => throw new InvalidOperationException($"Unknown authenticator type: {authenticatorType}"),
};
}
if (authenticator == null)
{
Expand Down
3 changes: 1 addition & 2 deletions src/WinAuth/WinAuth/SteamAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -911,8 +911,7 @@ public override async void Sync()
try
{
var response = await Client.TwoFAQueryTime();
var options = new JsonSerializerOptions { TypeInfoResolver = SteamJsonContext.Default };
var json = JsonSerializer.Deserialize<SteamSyncStruct>(response, options);
var json = JsonSerializer.Deserialize(response, SteamJsonContext.Default.SteamSyncStruct);
json.ThrowIsNull();
json.Response.ThrowIsNull();

Expand Down
39 changes: 26 additions & 13 deletions src/WinAuth/WinAuth/SteamClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public SteamSession(string json)

try
{
var session = System.Text.Json.JsonSerializer.Deserialize<SteamSession>(json);
var session = System.Text.Json.JsonSerializer.Deserialize(json, SteamJsonContext.Default.SteamSession);
if (session == null)
throw new ArgumentNullException(nameof(session));

Expand Down Expand Up @@ -339,7 +339,7 @@ public async Task RefreshAccessToken(SteamClient client)
throw new Exception("Failed to refresh token: " + ex.Message);
}

var response = JsonConvert.DeserializeObject<GenerateAccessTokenForAppResponse>(responseStr);
var response = JsonSerializer.Deserialize(responseStr, SteamSessionTokenJsonContext.Default.GenerateAccessTokenForAppResponse);
this.AccessToken = response.Response.AccessToken;
}

Expand Down Expand Up @@ -371,7 +371,7 @@ private bool IsTokenExpired(string token)
}

var payloadBytes = Convert.FromBase64String(base64);
var jwt = JsonConvert.DeserializeObject<SteamAccessToken>(System.Text.Encoding.UTF8.GetString(payloadBytes));
var jwt = JsonSerializer.Deserialize(payloadBytes, SteamSessionTokenJsonContext.Default.SteamAccessToken);

// Compare expire time of the token to the current time
return DateTimeOffset.UtcNow.ToUnixTimeSeconds() > jwt.Exp;
Expand Down Expand Up @@ -414,24 +414,24 @@ private static string GetRandomHexNumber(int digits)

public override string ToString()
{
return JsonSerializer.Serialize(this);
return JsonSerializer.Serialize(this, SteamJsonContext.Default.SteamSession);
}

private class SteamAccessToken
internal class SteamAccessToken
{
[JsonProperty("exp")]
[JsonPropertyName("exp")]
public long Exp { get; set; }
}

private class GenerateAccessTokenForAppResponse
internal class GenerateAccessTokenForAppResponse
{
[JsonProperty("response")]
public GenerateAccessTokenForAppResponseResponse? Response;
[JsonPropertyName("response")]
public GenerateAccessTokenForAppResponseResponse? Response { get; set; }
}

private class GenerateAccessTokenForAppResponseResponse
internal class GenerateAccessTokenForAppResponseResponse
{
[JsonProperty("access_token")]
[JsonPropertyName("access_token")]
public string? AccessToken { get; set; }
}
}
Expand Down Expand Up @@ -1085,7 +1085,16 @@ public async Task<string> TwoFAQueryTime()
/// <param name="headers"></param>
/// <param name="timeout"></param>
/// <returns>response body</returns>
async Task<T> RequestAsync<T>(string url, string method, NameValueCollection? data = null,
// protobuf-net 3.4 reads these attribute getters through AttributeMap; the nested token
// is discovered from the response property rather than a generic serializer argument.
// Nested messages also require protobuf-net's reflection-created generic adapters.
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties, typeof(ProtoContractAttribute))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties, typeof(ProtoMemberAttribute))]
[DynamicDependency("get_Value", typeof(System.ComponentModel.DefaultValueAttribute))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(CRemoveAuthenticatorViaChallengeContinue_Replacement_Token))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors | DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods, "ProtoBuf.Internal.DynamicStub.ConcreteStub`1", "protobuf-net.Core")]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors | DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods, "ProtoBuf.Internal.Serializers.SubValueSerializer`1", "protobuf-net")]
async Task<T> RequestAsync<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(string url, string method, NameValueCollection? data = null,
CookieContainer? cookies = null, NameValueCollection? headers = null, int timeout = 0, bool isProtobuf = false)
{
// create form-encoded data for query or body
Expand Down Expand Up @@ -1304,7 +1313,11 @@ static void LogRequest(string? method, string url, CookieContainer? cookies, Nam

#region ToolMethod

static string ConvertBase64String<T>(T obj)
// Preserve the attribute getters used by protobuf-net's reflection serializer under AOT.
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties, typeof(ProtoContractAttribute))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties, typeof(ProtoMemberAttribute))]
[DynamicDependency("get_Value", typeof(System.ComponentModel.DefaultValueAttribute))]
static string ConvertBase64String<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(T obj)
{
using var stream = new MemoryStream();
Serializer.Serialize(stream, obj);
Expand Down
Loading