diff --git a/README.md b/README.md index 4dbc064..3ed9e7e 100644 --- a/README.md +++ b/README.md @@ -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` and `SubValueSerializer` 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. diff --git a/src/WinAuth/BD.WTTS/Models/Steam/SteamJsonContext.cs b/src/WinAuth/BD.WTTS/Models/Steam/SteamJsonContext.cs index b09a1a8..460cdaa 100644 --- a/src/WinAuth/BD.WTTS/Models/Steam/SteamJsonContext.cs +++ b/src/WinAuth/BD.WTTS/Models/Steam/SteamJsonContext.cs @@ -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 +{ +} diff --git a/src/WinAuth/WinAuth/Authenticator.cs b/src/WinAuth/WinAuth/Authenticator.cs index cf66841..b89e982 100644 --- a/src/WinAuth/WinAuth/Authenticator.cs +++ b/src/WinAuth/WinAuth/Authenticator.cs @@ -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) { diff --git a/src/WinAuth/WinAuth/SteamAuthenticator.cs b/src/WinAuth/WinAuth/SteamAuthenticator.cs index 0748711..9ea4ef6 100644 --- a/src/WinAuth/WinAuth/SteamAuthenticator.cs +++ b/src/WinAuth/WinAuth/SteamAuthenticator.cs @@ -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(response, options); + var json = JsonSerializer.Deserialize(response, SteamJsonContext.Default.SteamSyncStruct); json.ThrowIsNull(); json.Response.ThrowIsNull(); diff --git a/src/WinAuth/WinAuth/SteamClient.cs b/src/WinAuth/WinAuth/SteamClient.cs index 001278f..d34ce5c 100644 --- a/src/WinAuth/WinAuth/SteamClient.cs +++ b/src/WinAuth/WinAuth/SteamClient.cs @@ -288,7 +288,7 @@ public SteamSession(string json) try { - var session = System.Text.Json.JsonSerializer.Deserialize(json); + var session = System.Text.Json.JsonSerializer.Deserialize(json, SteamJsonContext.Default.SteamSession); if (session == null) throw new ArgumentNullException(nameof(session)); @@ -339,7 +339,7 @@ public async Task RefreshAccessToken(SteamClient client) throw new Exception("Failed to refresh token: " + ex.Message); } - var response = JsonConvert.DeserializeObject(responseStr); + var response = JsonSerializer.Deserialize(responseStr, SteamSessionTokenJsonContext.Default.GenerateAccessTokenForAppResponse); this.AccessToken = response.Response.AccessToken; } @@ -371,7 +371,7 @@ private bool IsTokenExpired(string token) } var payloadBytes = Convert.FromBase64String(base64); - var jwt = JsonConvert.DeserializeObject(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; @@ -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; } } } @@ -1085,7 +1085,16 @@ public async Task TwoFAQueryTime() /// /// /// response body - async Task RequestAsync(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 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 @@ -1304,7 +1313,11 @@ static void LogRequest(string? method, string url, CookieContainer? cookies, Nam #region ToolMethod - static string ConvertBase64String(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);