Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Trimmable Assembly #391

Merged
merged 13 commits into from
Feb 13, 2024
Merged
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ jobs:
steps:
- uses: actions/checkout@af513c7a016048ae468971c52ed77d9562c7c819

- name: Setup net6
- name: Setup dotnet
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.x'
dotnet-version: |
6.0.x
7.0.x
8.0.x

- run: dotnet --info

Expand Down
7 changes: 7 additions & 0 deletions IdentityModel.OidcClient.sln
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DPoPTests", "test\DPoPTests
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleClientWithBrowserAndDPoP", "clients\ConsoleClientWithBrowserAndDPoP\ConsoleClientWithBrowserAndDPoP.csproj", "{7DDDA872-49C0-43F0-8B88-2531BF828DDE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TrimmableAnalysis", "test\TrimmableAnalysis\TrimmableAnalysis.csproj", "{672FE4E9-9071-4C59-95FC-F265DF6B2FF5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -63,6 +65,10 @@ Global
{7DDDA872-49C0-43F0-8B88-2531BF828DDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7DDDA872-49C0-43F0-8B88-2531BF828DDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7DDDA872-49C0-43F0-8B88-2531BF828DDE}.Release|Any CPU.Build.0 = Release|Any CPU
{672FE4E9-9071-4C59-95FC-F265DF6B2FF5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{672FE4E9-9071-4C59-95FC-F265DF6B2FF5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{672FE4E9-9071-4C59-95FC-F265DF6B2FF5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{672FE4E9-9071-4C59-95FC-F265DF6B2FF5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -76,6 +82,7 @@ Global
{56EC3A58-33FE-4DCD-9D64-2A985DC58C2C} = {0B7CC363-CF34-4B40-A769-7E928A6B25AF}
{0E1807AF-4142-4A3D-925C-BBA019E4E777} = {3DEB81D4-5B40-4D20-AC50-66D1CD6EA24A}
{7DDDA872-49C0-43F0-8B88-2531BF828DDE} = {A4154BEB-4B4A-4A48-B75D-B52432304F36}
{672FE4E9-9071-4C59-95FC-F265DF6B2FF5} = {3DEB81D4-5B40-4D20-AC50-66D1CD6EA24A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {66951C2E-691F-408C-9283-F2455F390A9A}
Expand Down
10 changes: 8 additions & 2 deletions build/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ private static class Targets
public const string Pack = "pack";
public const string SignBinary = "sign-binary";
public const string SignPackage = "sign-package";
public const string TrimmableAnalysis = "trimmable-analysis";
}

static async Task Main(string[] args)
Expand Down Expand Up @@ -65,9 +66,14 @@ static async Task Main(string[] args)
SignNuGet();
});

Target("default", DependsOn(Targets.Test, Targets.Pack));
Target(Targets.TrimmableAnalysis, () =>
{
Run("dotnet", "publish test/TrimmableAnalysis -c Release -r win-x64");
});

Target("default", DependsOn(Targets.Test, Targets.Pack, Targets.TrimmableAnalysis));

Target("sign", DependsOn(Targets.Test, Targets.SignPackage));
Target("sign", DependsOn(Targets.Test, Targets.SignPackage, Targets.TrimmableAnalysis));

await RunTargetsAndExitAsync(args, ex => ex is SimpleExec.ExitCodeException || ex.Message.EndsWith(envVarMissing));
}
Expand Down
2 changes: 1 addition & 1 deletion src/DPoP/DPoP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="IdentityModel" Version="6.1.0" />
<PackageReference Include="IdentityModel" Version="6.2.0" />
<PackageReference Include="minver" Version="4.3.0" PrivateAssets="All" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.30.0" />

Expand Down
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: 7 additions & 5 deletions src/OidcClient/Infrastructure/LogSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// 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;
using System.Text.Json.Serialization.Metadata;

namespace IdentityModel.OidcClient.Infrastructure
{
Expand All @@ -14,7 +14,7 @@ public static class LogSerializer
{
/// <summary>
/// Allows log serialization to be disabled, for example, for platforms
/// that don't support serialization of arbitarary objects to JSON.
/// that don't support serialization of arbitrary objects to JSON.
/// </summary>
public static bool Enabled = true;

Expand All @@ -34,9 +34,11 @@ static LogSerializer()
/// </summary>
/// <param name="logObject">The object.</param>
/// <returns></returns>
public static string Serialize(object logObject)
public static string Serialize<T>(T logObject)
{
return Enabled ? JsonSerializer.Serialize(logObject, JsonOptions) : "Logging has been disabled";
}
return Enabled ?
JsonSerializer.Serialize(logObject, (JsonTypeInfo<T>)SourceGenerationContext.Default.GetTypeInfo(typeof(T))) :
"Logging has been disabled";
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change because the LogSerializer can now only serialize the types in the source generator context. That works fine for our internal usage of the LogSerializer, but the LogSerializer is a public type. Anyone who is using this helper to serialize anything else will get an NRE when GetTypeInfo returns null.

Options:

  • Make the LogSerializer internal.
  • Don't enable trimmable
  • Make this method private, but keep the original Serialize(object logObject) as a public overload with [RequiresUnreferencedCode]

}
}
7 changes: 3 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,9 @@ public Task<IdentityTokenValidationResult> ValidateAsync(string identityToken, O
}

var payload = Encoding.UTF8.GetString((Base64Url.Decode(parts[1])));
var values = JsonSerializer.Deserialize(
payload, SourceGenerationContext.Default.DictionaryStringJsonElement);

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

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

}
}

Expand Down
25 changes: 18 additions & 7 deletions src/OidcClient/OidcClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
<PackageId>IdentityModel.OidcClient</PackageId>
<RootNamespace>IdentityModel.OidcClient</RootNamespace>
<AssemblyName>IdentityModel.OidcClient</AssemblyName>

<TargetFramework>netstandard2.0</TargetFramework>


<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
mwasplund marked this conversation as resolved.
Show resolved Hide resolved
<LangVersion>latest</LangVersion>

<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,24 +25,34 @@
<!-- 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 Condition="'$(TargetFramework)' == 'net6.0'">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>
<None Include="../../icon.jpg" Pack="true" Visible="false" PackagePath="" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="IdentityModel" Version="6.0.0" />
<PackageReference Include="IdentityModel" Version="6.2.0" />
<PackageReference Include="minver" Version="4.3.0" PrivateAssets="All" />

<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>

<!--Conditional Package references -->
<ItemGroup Condition="'$(TargetFramework)' != 'net6.0'">
<PackageReference Include="System.Text.Json" Version="6.0.8" />

<!--Force older version to prevent incompatible references -->
<PackageReference Include="System.Text.Encodings.Web" Version="6.0.0" />
</ItemGroup>

</Project>
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
18 changes: 18 additions & 0 deletions src/OidcClient/SourceGenerationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace IdentityModel.OidcClient
{
[JsonSourceGenerationOptions(
WriteIndented = false,
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
GenerationMode = JsonSourceGenerationMode.Metadata,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(AuthorizeState))]
[JsonSerializable(typeof(Dictionary<string, JsonElement>))]
[JsonSerializable(typeof(OidcClientOptions))]
internal partial class SourceGenerationContext : JsonSerializerContext
{
}
}
2 changes: 1 addition & 1 deletion test/DPoPTests/DPoPTests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
</PropertyGroup>

<PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion test/JwtValidationTests/JwtValidationTests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
</PropertyGroup>

<PropertyGroup>
Expand Down
2 changes: 2 additions & 0 deletions test/TrimmableAnalysis/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
3 changes: 3 additions & 0 deletions test/TrimmableAnalysis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This project exists to facilitate analysis of trimmable warnings.

See https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/prepare-libraries-for-trimming
17 changes: 17 additions & 0 deletions test/TrimmableAnalysis/TrimmableAnalysis.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<PublishTrimmed>true</PublishTrimmed>
<TrimmerSingleWarn>false</TrimmerSingleWarn>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<TrimmerRootAssembly Include="IdentityModel.OidcClient" />
<ProjectReference Include="..\..\src\OidcClient\OidcClient.csproj" />
</ItemGroup>

</Project>
Loading