|
| 1 | +--- |
| 2 | +title: Client Assertions |
| 3 | +description: Learn how to use client assertions instead of shared secrets for token client authentication in Duende.AccessTokenManagement. |
| 4 | +sidebar: |
| 5 | + label: Client Assertions |
| 6 | + order: 30 |
| 7 | +redirect_from: |
| 8 | + - /foss/accesstokenmanagement/advanced/client_assertions/ |
| 9 | +--- |
| 10 | +import { Tabs, TabItem } from "@astrojs/starlight/components"; |
| 11 | + |
| 12 | +If your token client is using a client assertion instead of a shared secret, you can provide the assertion in two ways: |
| 13 | + |
| 14 | +* Use the request parameter mechanism to pass a client assertion to the management |
| 15 | +* Implement the `IClientAssertionService` interface to centralize client assertion creation |
| 16 | + |
| 17 | +Here's a sample client assertion service using the Microsoft JWT library: |
| 18 | + |
| 19 | +{/* prettier-ignore */} |
| 20 | +<Tabs syncKey="atm"> |
| 21 | + {/* prettier-ignore */} |
| 22 | + <TabItem label="V4"> |
| 23 | + ```csharp |
| 24 | + // ClientAssertionService.cs |
| 25 | + using Duende.AccessTokenManagement; |
| 26 | + using Duende.IdentityModel; |
| 27 | + using Duende.IdentityModel.Client; |
| 28 | + using Microsoft.Extensions.Options; |
| 29 | + using Microsoft.IdentityModel.JsonWebTokens; |
| 30 | + using Microsoft.IdentityModel.Tokens; |
| 31 | + |
| 32 | + public class ClientAssertionService(IOptionsSnapshot<ClientCredentialsClient> options) |
| 33 | + : IClientAssertionService |
| 34 | + { |
| 35 | + public Task<ClientAssertion?> GetClientAssertionAsync( |
| 36 | + ClientCredentialsClientName? clientName = null, TokenRequestParameters? parameters = null) |
| 37 | + { |
| 38 | + if (clientName == "invoice") |
| 39 | + { |
| 40 | + var options1 = options.Get(clientName); |
| 41 | + |
| 42 | + var descriptor = new SecurityTokenDescriptor |
| 43 | + { |
| 44 | + Issuer = options1.ClientId, |
| 45 | + Audience = options1.TokenEndpoint, |
| 46 | + Expires = DateTime.UtcNow.AddMinutes(1), |
| 47 | + SigningCredentials = GetSigningCredential(), |
| 48 | + |
| 49 | + Claims = new Dictionary<string, object> |
| 50 | + { |
| 51 | + { JwtClaimTypes.JwtId, Guid.NewGuid().ToString() }, |
| 52 | + { JwtClaimTypes.Subject, options1.ClientId! }, |
| 53 | + { JwtClaimTypes.IssuedAt, DateTime.UtcNow.ToEpochTime() } |
| 54 | + }, |
| 55 | + |
| 56 | + AdditionalHeaderClaims = new Dictionary<string, object> |
| 57 | + { |
| 58 | + { JwtClaimTypes.TokenType, "client-authentication+jwt" } |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + var handler = new JsonWebTokenHandler(); |
| 63 | + var jwt = handler.CreateToken(descriptor); |
| 64 | + |
| 65 | + return Task.FromResult<ClientAssertion?>(new ClientAssertion |
| 66 | + { |
| 67 | + Type = OidcConstants.ClientAssertionTypes.JwtBearer, |
| 68 | + Value = jwt |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + return Task.FromResult<ClientAssertion?>(null); |
| 73 | + } |
| 74 | + |
| 75 | + private SigningCredentials GetSigningCredential() |
| 76 | + { |
| 77 | + throw new NotImplementedException(); |
| 78 | + } |
| 79 | + } |
| 80 | + ``` |
| 81 | + </TabItem> |
| 82 | + <TabItem label="V3"> |
| 83 | + |
| 84 | + ```csharp |
| 85 | + // ClientAssertionService.cs |
| 86 | + using Duende.AccessTokenManagement; |
| 87 | + using Duende.IdentityModel; |
| 88 | + using Duende.IdentityModel.Client; |
| 89 | + using Microsoft.Extensions.Options; |
| 90 | + using Microsoft.IdentityModel.JsonWebTokens; |
| 91 | + using Microsoft.IdentityModel.Tokens; |
| 92 | + |
| 93 | + public class ClientAssertionService(IOptionsSnapshot<ClientCredentialsClient> options) |
| 94 | + : IClientAssertionService |
| 95 | + { |
| 96 | + public Task<ClientAssertion?> GetClientAssertionAsync( |
| 97 | + string? clientName = null, TokenRequestParameters? parameters = null) |
| 98 | + { |
| 99 | + if (clientName == "invoice") |
| 100 | + { |
| 101 | + var options1 = options.Get(clientName); |
| 102 | + |
| 103 | + var descriptor = new SecurityTokenDescriptor |
| 104 | + { |
| 105 | + Issuer = options1.ClientId, |
| 106 | + Audience = options1.TokenEndpoint, |
| 107 | + Expires = DateTime.UtcNow.AddMinutes(1), |
| 108 | + SigningCredentials = GetSigningCredential(), |
| 109 | + |
| 110 | + Claims = new Dictionary<string, object> |
| 111 | + { |
| 112 | + { JwtClaimTypes.JwtId, Guid.NewGuid().ToString() }, |
| 113 | + { JwtClaimTypes.Subject, options1.ClientId! }, |
| 114 | + { JwtClaimTypes.IssuedAt, DateTime.UtcNow.ToEpochTime() } |
| 115 | + }, |
| 116 | + |
| 117 | + AdditionalHeaderClaims = new Dictionary<string, object> |
| 118 | + { |
| 119 | + { JwtClaimTypes.TokenType, "client-authentication+jwt" } |
| 120 | + } |
| 121 | + }; |
| 122 | + |
| 123 | + var handler = new JsonWebTokenHandler(); |
| 124 | + var jwt = handler.CreateToken(descriptor); |
| 125 | + |
| 126 | + return Task.FromResult<ClientAssertion?>(new ClientAssertion |
| 127 | + { |
| 128 | + Type = OidcConstants.ClientAssertionTypes.JwtBearer, |
| 129 | + Value = jwt |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + return Task.FromResult<ClientAssertion?>(null); |
| 134 | + } |
| 135 | + |
| 136 | + private SigningCredentials GetSigningCredential() |
| 137 | + { |
| 138 | + throw new NotImplementedException(); |
| 139 | + } |
| 140 | + } |
| 141 | + ``` |
| 142 | + </TabItem> |
| 143 | +</Tabs> |
0 commit comments