Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit ad33a1e

Browse files
committed
Support trimming in OidcClient.DPoP
1 parent 3c71588 commit ad33a1e

File tree

7 files changed

+64
-12
lines changed

7 files changed

+64
-12
lines changed

src/DPoP/DPoP.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
<!-- Recommended: Embed symbols containing Source Link in the main file (exe/dll) -->
2727
<DebugType>embedded</DebugType>
2828

29+
<!-- Enable Trimming Warnings to allow consumers to publish as trimmed -->
30+
<IsTrimmable Condition="'$(TargetFramework)' == 'net6.0'">true</IsTrimmable>
31+
2932
<ContinuousIntegrationBuild Condition="'$(GITHUB_ACTIONS)' == 'true'">True</ContinuousIntegrationBuild>
3033

3134
<AssemblyOriginatorKeyFile>../../key.snk</AssemblyOriginatorKeyFile>

src/DPoP/DPoPProof.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33

4-
54
namespace IdentityModel.OidcClient.DPoP;
65

76
/// <summary>

src/DPoP/DPoPProofPayload.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
5+
using System.Text.Json.Serialization;
6+
7+
namespace IdentityModel.OidcClient.DPoP;
8+
9+
/// <summary>
10+
/// Internal class to aid serialization of DPoP proof token payloads. Giving
11+
/// each claim a property allows us to add this type to the source generated
12+
/// serialization
13+
/// </summary>
14+
internal class DPoPProofPayload
15+
{
16+
[JsonPropertyName(JwtClaimTypes.JwtId)]
17+
internal string JwtId { get; set; } = default!;
18+
[JsonPropertyName(JwtClaimTypes.DPoPHttpMethod)]
19+
internal string DPoPHttpMethod { get; set; } = default!;
20+
[JsonPropertyName(JwtClaimTypes.DPoPHttpUrl)]
21+
internal string DPoPHttpUrl { get; set; } = default!;
22+
[JsonPropertyName(JwtClaimTypes.IssuedAt)]
23+
internal long IssuedAt { get; set; }
24+
[JsonPropertyName(JwtClaimTypes. DPoPAccessTokenHash)]
25+
internal string? DPoPAccessTokenHash { get; set; }
26+
[JsonPropertyName(JwtClaimTypes. Nonce)]
27+
internal string? Nonce { get; set; }
28+
}

src/DPoP/DPoPProofTokenFactory.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public DPoPProof CreateProofToken(DPoPProofRequest request)
4040

4141
// jwk: representing the public key chosen by the client, in JSON Web Key (JWK) [RFC7517] format,
4242
// as defined in Section 4.1.3 of [RFC7515]. MUST NOT contain a private key.
43-
object jwk;
43+
Dictionary<string, object> jwk;
4444
if (string.Equals(jsonWebKey.Kty, JsonWebAlgorithmsKeyTypes.EllipticCurve))
4545
{
4646
jwk = new Dictionary<string, object>
@@ -71,12 +71,12 @@ public DPoPProof CreateProofToken(DPoPProofRequest request)
7171
{ JwtClaimTypes.JsonWebKey, jwk },
7272
};
7373

74-
var payload = new Dictionary<string, object>
74+
var payload = new DPoPProofPayload
7575
{
76-
{ JwtClaimTypes.JwtId, CryptoRandom.CreateUniqueId() },
77-
{ JwtClaimTypes.DPoPHttpMethod, request.Method },
78-
{ JwtClaimTypes.DPoPHttpUrl, request.Url },
79-
{ JwtClaimTypes.IssuedAt, DateTimeOffset.UtcNow.ToUnixTimeSeconds() },
76+
JwtId = CryptoRandom.CreateUniqueId(),
77+
DPoPHttpMethod = request.Method,
78+
DPoPHttpUrl = request.Url,
79+
IssuedAt = DateTimeOffset.UtcNow.ToUnixTimeSeconds()
8080
};
8181

8282
if (!string.IsNullOrWhiteSpace(request.AccessToken))
@@ -87,17 +87,17 @@ public DPoPProof CreateProofToken(DPoPProofRequest request)
8787
var hash = sha256.ComputeHash(Encoding.ASCII.GetBytes(request.AccessToken));
8888
var ath = Base64Url.Encode(hash);
8989

90-
payload.Add(JwtClaimTypes.DPoPAccessTokenHash, ath);
90+
payload.DPoPAccessTokenHash = ath;
9191
}
9292

9393
if (!string.IsNullOrEmpty(request.DPoPNonce))
9494
{
95-
payload.Add(JwtClaimTypes.Nonce, request.DPoPNonce!);
95+
payload.Nonce = request.DPoPNonce!;
9696
}
9797

9898
var handler = new JsonWebTokenHandler() { SetDefaultTimesOnTokenCreation = false };
9999
var key = new SigningCredentials(jsonWebKey, jsonWebKey.Alg);
100-
var proofToken = handler.CreateToken(JsonSerializer.Serialize(payload), key, header);
100+
var proofToken = handler.CreateToken(JsonSerializer.Serialize(payload, SourceGenerationContext.Default.DPoPProofPayload), key, header);
101101

102102
return new DPoPProof { ProofToken = proofToken! };
103103
}

src/DPoP/JsonWebKeys.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static JsonWebKey CreateRsa(string algorithm = OidcConstants.Algorithms.A
3131
/// </summary>
3232
public static string CreateRsaJson(string algorithm = OidcConstants.Algorithms.Asymmetric.PS256)
3333
{
34-
return JsonSerializer.Serialize(CreateRsa(algorithm));
34+
return JsonSerializer.Serialize(CreateRsa(algorithm), SourceGenerationContext.Default.JsonWebKey);
3535
}
3636

3737
/// <summary>
@@ -53,7 +53,7 @@ public static JsonWebKey CreateECDsa(string algorithm = OidcConstants.Algorithms
5353
/// </summary>
5454
public static string CreateECDsaJson(string algorithm = OidcConstants.Algorithms.Asymmetric.ES256)
5555
{
56-
return JsonSerializer.Serialize(CreateECDsa(algorithm));
56+
return JsonSerializer.Serialize(CreateECDsa(algorithm), SourceGenerationContext.Default.JsonWebKey);
5757
}
5858

5959
internal static string GetCurveNameFromSigningAlgorithm(string alg)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Text.Json.Serialization;
2+
using Microsoft.IdentityModel.Tokens;
3+
4+
namespace IdentityModel.OidcClient.DPoP
5+
{
6+
[JsonSourceGenerationOptions(
7+
WriteIndented = false,
8+
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
9+
GenerationMode = JsonSourceGenerationMode.Metadata,
10+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
11+
[JsonSerializable(typeof(JsonWebKey))]
12+
[JsonSerializable(typeof(DPoPProofPayload))]
13+
internal partial class SourceGenerationContext : JsonSerializerContext
14+
{
15+
}
16+
}

test/TrimmableAnalysis/TrimmableAnalysis.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515

1616
<TrimmerRootAssembly Include="IdentityModel.OidcClient.IdentityTokenValidator" />
1717
<ProjectReference Include="..\..\src\IdentityTokenValidator\IdentityTokenValidator.csproj" />
18+
19+
20+
<TrimmerRootAssembly Include="IdentityModel.OidcClient.DPoP" />
21+
<ProjectReference Include="..\..\src\DPoP\DPoP.csproj" />
22+
23+
1824
</ItemGroup>
1925

2026
</Project>

0 commit comments

Comments
 (0)