Skip to content
This repository has been archived by the owner on Feb 23, 2025. It is now read-only.

Enable Trimmable Assembly #391

Merged
merged 13 commits into from
Feb 13, 2024
1 change: 1 addition & 0 deletions src/OidcClient/IIdentityTokenValidator.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using IdentityModel.OidcClient.Results;
Expand Down
12 changes: 9 additions & 3 deletions src/OidcClient/Infrastructure/LogSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.


using System.Text.Json;
using System.Text.Json.Serialization;
#if NET5_0_OR_GREATER
using System.Text.Json.Serialization.Metadata;
#endif

namespace IdentityModel.OidcClient.Infrastructure
{
Expand Down Expand Up @@ -34,9 +36,13 @@ static LogSerializer()
/// </summary>
/// <param name="logObject">The object.</param>
/// <returns></returns>
public static string Serialize(object logObject)
public static string Serialize<T>(T logObject)
{
#if NET5_0_OR_GREATER
return Enabled ? JsonSerializer.Serialize(logObject, (JsonTypeInfo<T>)SourceGenerationContext.Default.GetTypeInfo(typeof(T))) : "Logging has been disabled";
#else
return Enabled ? JsonSerializer.Serialize(logObject, JsonOptions) : "Logging has been disabled";
}
#endif
}
}
}
11 changes: 7 additions & 4 deletions src/OidcClient/NoValidationIdentityTokenValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Security.Claims;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using IdentityModel.OidcClient.Results;
Expand All @@ -26,10 +27,13 @@ public Task<IdentityTokenValidationResult> ValidateAsync(string identityToken, O
}

var payload = Encoding.UTF8.GetString((Base64Url.Decode(parts[1])));
#if NET5_0_OR_GREATER
var values = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(
payload, SourceGenerationContext.Default.DictionaryStringJsonElement);
#else
var values = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(payload);
#endif

var values =
JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(payload);

var claims = new List<Claim>();
foreach (var element in values)
{
Expand All @@ -43,7 +47,6 @@ public Task<IdentityTokenValidationResult> ValidateAsync(string identityToken, O
else
{
claims.Add(new Claim(element.Key, element.Value.ToString()));

}
}

Expand Down
14 changes: 8 additions & 6 deletions src/OidcClient/OidcClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
<PackageId>IdentityModel.OidcClient</PackageId>
<RootNamespace>IdentityModel.OidcClient</RootNamespace>
<AssemblyName>IdentityModel.OidcClient</AssemblyName>
<TargetFramework>netstandard2.0</TargetFramework>

<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>

<PackageTags>OAuth2;OAuth 2.0;OpenID Connect;Security;Identity;IdentityServer</PackageTags>
<Description>RFC8252 compliant and certified OpenID Connect and OAuth 2.0 client library for native applications</Description>
<Authors>Dominick Baier;Brock Allen</Authors>
<PackageIcon>icon.jpg</PackageIcon>

<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

Expand All @@ -24,12 +24,14 @@
<!-- Recommended: Embed symbols containing Source Link in the main file (exe/dll) -->
<DebugType>embedded</DebugType>

<!-- Enable Trimming Warnings to allow consumers to publish as trimmed -->
<IsTrimmable>true</IsTrimmable>

<ContinuousIntegrationBuild Condition="'$(GITHUB_ACTIONS)' == 'true'">True</ContinuousIntegrationBuild>

<AssemblyOriginatorKeyFile>../../key.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<PublicSign Condition="'$(OS)' != 'Windows_NT'">true</PublicSign>

</PropertyGroup>

<ItemGroup>
Expand Down
3 changes: 0 additions & 3 deletions src/OidcClient/ResponseProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.


using IdentityModel.Client;
using IdentityModel.OidcClient.Infrastructure;
using IdentityModel.OidcClient.Results;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Net;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;

Expand Down
19 changes: 19 additions & 0 deletions src/OidcClient/SourceGenerationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace IdentityModel.OidcClient
{
#if NET5_0_OR_GREATER
[JsonSourceGenerationOptions(
WriteIndented = false,
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(AuthorizeState))]
[JsonSerializable(typeof(Dictionary<string, JsonElement>))]
[JsonSerializable(typeof(OidcClientOptions))]
internal partial class SourceGenerationContext : JsonSerializerContext
{
}
#endif
}