-
Notifications
You must be signed in to change notification settings - Fork 45
Introduce ClientAssertionFactory to allow refreshing of Client Assertions during retries #343
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
Merged
Erwinvandervalk
merged 1 commit into
main
from
fix/dpop-client-assertion-identity-model
Mar 16, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
identity-model/samples/ClientAssertions/ClientAssertionService.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Copyright (c) Duende Software. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. | ||
|
|
||
| using Duende.IdentityModel; | ||
| using Duende.IdentityModel.Client; | ||
| using Microsoft.IdentityModel.JsonWebTokens; | ||
| using Microsoft.IdentityModel.Tokens; | ||
|
|
||
| namespace ClientAssertions; | ||
|
|
||
| /// <summary> | ||
| /// Creates signed client assertion JWTs (RFC 7523 / private_key_jwt). | ||
| /// Each call to <see cref="CreateAssertionAsync"/> produces a JWT with a fresh | ||
| /// <c>jti</c> and <c>iat</c>, which is critical when retries (e.g. DPoP nonce | ||
| /// challenges) require a new assertion to avoid replay rejection. | ||
| /// </summary> | ||
| public class ClientAssertionService | ||
| { | ||
| private readonly string _clientId; | ||
| private readonly string _audience; | ||
| private readonly SigningCredentials _signingCredentials; | ||
|
|
||
| public ClientAssertionService(string clientId, string audience, SigningCredentials signingCredentials) | ||
| { | ||
| _clientId = clientId ?? throw new ArgumentNullException(nameof(clientId)); | ||
| _audience = audience ?? throw new ArgumentNullException(nameof(audience)); | ||
| _signingCredentials = signingCredentials ?? throw new ArgumentNullException(nameof(signingCredentials)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates a fresh <see cref="ClientAssertion"/> with a unique <c>jti</c>. | ||
| /// </summary> | ||
| public Task<ClientAssertion> CreateAssertionAsync() | ||
| { | ||
| var now = DateTime.UtcNow; | ||
|
|
||
| var descriptor = new SecurityTokenDescriptor | ||
| { | ||
| Issuer = _clientId, | ||
| Audience = _audience, | ||
| IssuedAt = now, | ||
| NotBefore = now, | ||
| Expires = now.AddMinutes(1), | ||
| SigningCredentials = _signingCredentials, | ||
| AdditionalHeaderClaims = new Dictionary<string, object> | ||
| { | ||
| { "typ", "client-authentication+jwt" } | ||
| }, | ||
| Claims = new Dictionary<string, object> | ||
| { | ||
| { JwtClaimTypes.JwtId, Guid.NewGuid().ToString() }, | ||
| { JwtClaimTypes.Subject, _clientId }, | ||
| } | ||
| }; | ||
|
|
||
| var handler = new JsonWebTokenHandler(); | ||
| var jwt = handler.CreateToken(descriptor); | ||
|
|
||
| return Task.FromResult(new ClientAssertion | ||
| { | ||
| Type = OidcConstants.ClientAssertionTypes.JwtBearer, | ||
| Value = jwt | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates signing credentials from the RSA key pair that matches the public | ||
| /// key registered for the "m2m.jwt" client at demo.duendesoftware.com. | ||
| /// In production, load the private key from a secure store (e.g. Azure Key Vault). | ||
| /// </summary> | ||
| public static SigningCredentials CreateSigningCredentials() | ||
| { | ||
| // This JWK contains the private key that corresponds to the public key | ||
| // registered at the demo IdentityServer for the "m2m.jwt" client. | ||
| var jwk = """ | ||
| { | ||
| "d":"Gm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6n3W4wUp19UvzB1lGtXcJKLLkqB6YDiZNu16OSiSprfmrRXvYmvD8m6Fnl5aetgKw", | ||
| "p":"7enorp9Pm9XSHaCvQyENcvdU99WCPbnp8vc0KnY_0g9UdX4ZDH07JwKu6DQEwfmUA1qspC-e_KFWTl3x0-I2eJRnHjLOoLrTjrVSBRhBMGEH5PvtZTTThnIY2LReH-6EhceGvcsJ_MhNDUEZLykiH1OnKhmRuvSdhi8oiETqtPE", | ||
| "q":"0CBLGi_kRPLqI8yfVkpBbA9zkCAshgrWWn9hsq6a7Zl2LcLaLBRUxH0q1jWnXgeJh9o5v8sYGXwhbrmuypw7kJ0uA3OgEzSsNvX5Ay3R9sNel-3Mqm8Me5OfWWvmTEBOci8RwHstdR-7b9ZT13jk-dsZI7OlV_uBja1ny9Nz9ts", | ||
| "qi":"pG6J4dcUDrDndMxa-ee1yG4KjZqqyCQcmPAfqklI2LmnpRIjcK78scclvpboI3JQyg6RCEKVMwAhVtQM6cBcIO3JrHgqeYDblp5wXHjto70HVW6Z8kBruNx1AH9E8LzNvSRL-JVTFzBkJuNgzKQfD0G77tQRgJ-Ri7qu3_9o1M4" | ||
| } | ||
| """; | ||
|
|
||
| var key = new JsonWebKey(jwk); | ||
| return new SigningCredentials(key, SecurityAlgorithms.RsaSha256); | ||
| } | ||
| } | ||
15 changes: 15 additions & 0 deletions
15
identity-model/samples/ClientAssertions/ClientAssertions.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net10.0</TargetFramework> | ||
| <OutputType>Exe</OutputType> | ||
| <ImplicitUsings>true</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.15.0" /> | ||
| <ProjectReference Include="..\..\src\IdentityModel\IdentityModel.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // Copyright (c) Duende Software. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. | ||
|
|
||
| // | ||
| // This sample demonstrates how to use a ClientAssertionFactory so that token | ||
| // requests (including DPoP nonce retries) automatically produce a fresh | ||
| // client_assertion JWT with a unique jti on each attempt. | ||
| // | ||
| // Background | ||
| // ---------- | ||
| // When a token request carries a client_assertion (RFC 7521 / private_key_jwt), | ||
| // the server may reject retries that reuse the same assertion because the jti | ||
| // has already been seen. Setting ClientAssertionFactory on the ProtocolRequest | ||
| // ensures that: | ||
| // 1. The initial request gets a freshly-minted assertion. | ||
| // 2. The factory is stored on HttpRequestMessage.Options so that downstream | ||
| // handlers (e.g. DPoP proof-token handlers) can invoke it on retries. | ||
| // | ||
|
|
||
| using Duende.IdentityModel.Client; | ||
|
|
||
| namespace ClientAssertions; | ||
|
|
||
| public class Program | ||
| { | ||
| private const string Authority = "https://demo.duendesoftware.com"; | ||
| private const string TokenEndpoint = $"{Authority}/connect/token"; | ||
| private const string ClientId = "m2m.jwt"; | ||
| private const string Scope = "api"; | ||
|
|
||
| public static async Task Main() | ||
| { | ||
| Console.WriteLine("+------------------------------------------+"); | ||
| Console.WriteLine("| Client Assertions Sample |"); | ||
| Console.WriteLine("+------------------------------------------+"); | ||
| Console.WriteLine(); | ||
|
|
||
| // 1. Create a signing key for client_assertion JWTs. | ||
| // In production this would be the key registered with the identity | ||
| // provider for private_key_jwt authentication. | ||
| var signingCredentials = ClientAssertionService.CreateSigningCredentials(); | ||
|
|
||
| // 2. Create the assertion service. | ||
| var assertionService = new ClientAssertionService(ClientId, Authority, signingCredentials); | ||
|
|
||
| // 3. Build the HTTP client. In a real DPoP scenario you would wrap this | ||
| // with a ProofTokenMessageHandler, but this sample focuses on the | ||
| // client assertion factory pattern alone. | ||
| var client = new HttpClient(); | ||
|
|
||
| // 4. Build the token request with a ClientAssertionFactory. | ||
| // The factory is called once for the initial request and stored on | ||
| // HttpRequestMessage.Options so that retry handlers can call it again. | ||
| var tokenRequest = new ClientCredentialsTokenRequest | ||
| { | ||
| Address = TokenEndpoint, | ||
| ClientId = ClientId, | ||
| ClientCredentialStyle = ClientCredentialStyle.PostBody, | ||
| Scope = Scope, | ||
|
|
||
| // The key feature: a factory that produces a fresh assertion each time. | ||
| ClientAssertionFactory = assertionService.CreateAssertionAsync, | ||
| }; | ||
|
|
||
| Console.WriteLine("Requesting token with client_assertion..."); | ||
| Console.WriteLine($" Endpoint : {TokenEndpoint}"); | ||
| Console.WriteLine($" ClientId : {ClientId}"); | ||
| Console.WriteLine(); | ||
|
|
||
| // 5. Send the token request. | ||
| var response = await client.RequestClientCredentialsTokenAsync(tokenRequest); | ||
|
|
||
| // 6. Display the result. | ||
| if (response.IsError) | ||
| { | ||
| Console.WriteLine($"Error: {response.Error}"); | ||
| Console.WriteLine($"Description: {response.ErrorDescription}"); | ||
|
|
||
| if (response.HttpStatusCode != 0) | ||
| { | ||
| Console.WriteLine($"HTTP {(int)response.HttpStatusCode}"); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| Console.WriteLine("Success!"); | ||
| Console.WriteLine($" Token type : {response.TokenType}"); | ||
| Console.WriteLine($" Expires in : {response.ExpiresIn}s"); | ||
| Console.WriteLine($" Access token : {response.AccessToken}"); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.