Repository navigation
feat: add MetadataAddress support to ClientCredentialsClient for OIDC discovery - #358
Conversation
f9fe6ef to
b8719d9
Compare
|
Hi @paulomorgado, thanks for this PR. Discovery support for client credentials is something we'd like to have, and we've spent some time as a team looking at how it should fit into the library. We'd like to take it in a slightly different direction, mainly so it matches how discovery already works elsewhere in this repo. The introspection handler ( Concretely, that means:
Your tests are a good base. Most of the scenarios (precedence, missing token endpoint, failed request, endpoint reuse) still apply, and we'd add a couple for concurrent first requests and issuer validation. Would you like to rework the PR along these lines? If you'd rather not, no problem at all. Just let us know and we'll pick it up from here and credit you in our PR. |
|
Hi @bhazen, Let me look into this, but I think I can handle it, |
|
Hi @bhazen, Check out the latest commit. |
bhazen
left a comment
There was a problem hiding this comment.
Thanks @paulomorgado for making the changes based on the direction we discussed
A few things about this round of changes:
-
CI failure.
ConventionTests.All_async_methods_should_end_with_Async_and_have_cancellation_token_as_last_parameterfails onAsyncLazy<T>.Value. Details inline. -
Discovery isn't set up if
Authorityis configured late. Details inline onPostConfigureClientCredentialsClient. -
Test organization. The discovery tests added to
ClientTokenManagementTests.cswould sit better inClientCredentialsDiscoveryTests.cs, where the existing helpers would cut most of their setup. Could you move them over, apart fromMissing_tokenEndpoint_and_authority_throw_exceptionandTokenEndpoint_should_take_precedence_over_authority, which fit where they are? -
One missing test. Nothing covers DPoP with
Authority. A test that the proof'shtuis the discovered token endpoint (including the nonce retry, if it's easy to include) would close that gap. The code already does the right thing, so this is just about locking it in. -
Merge conflict with
main. There's a conflict inServiceCollectionExtensions.cssince main added a validator registration on the same line. Keeping both lines resolves it:services.TryAddEnumerable(ServiceDescriptor.Singleton<IValidateOptions<ClientCredentialsTokenManagementOptions>, ClientCredentialsTokenManagementOptions.Validator>()); services.TryAddEnumerable(ServiceDescriptor.Singleton<IPostConfigureOptions<ClientCredentialsClient>, PostConfigureClientCredentialsClient>());
There are also a couple of small nits inline. Thanks again for turning this around so quickly.
Added MetadataAddress to ClientCredentialsClient for OIDC discovery. Validation now requires TokenEndpoint or MetadataAddress. ClientCredentialsTokenClient resolves token endpoint from metadata if needed, with improved error handling. Tests updated for new scenarios and API changes.
Require an explicit metadata document URI, fetch metadata JSON directly, validate only the discovered token endpoint, enforce HTTPS except for localhost/127.0.0.1 loopback HTTP, reject malformed or cross-authority endpoints, skip JWKS/signing-key retrieval, and expand tests for explicit URLs, ignored fields, malformed JSON, loopback exceptions, and endpoint validation.
Replace MetadataAddress with Authority and expose DiscoveryPolicy. Use GetDiscoveryDocumentAsync for standard discovery and validation. Initialize shared, retryable discovery lazily during post-configuration, retaining the resolved endpoint for the options lifetime without mutating TokenEndpoint. Isolate caller cancellation from shared discovery. Preserve explicit TokenEndpoint precedence and configured backchannels. Map discovery failures to InvalidOperationException consistently with introspection, preserving underlying exceptions.
Create lazy discovery whenever TokenEndpoint is absent and defer Authority validation until discovery runs, allowing later PostConfigure callbacks. Use configured client names in discovery errors and move shared HTTP client selection onto ClientCredentialsClient. Consolidate discovery tests and cover late configuration, explicit endpoint precedence, and DPoP endpoint binding including nonce retry. Exempt AsyncLazy from async conventions while preserving its Value API.
ae20782 to
176ab0d
Compare
paulomorgado
left a comment
There was a problem hiding this comment.
Addressed these points, including the inline comments. I moved the discovery tests into ClientCredentialsDiscoveryTests and reused its setup helpers, leaving Missing_tokenEndpoint_and_authority_throw_exception and TokenEndpoint_should_take_precedence_over_authority in ClientTokenManagementTests.
Added coverage using real DPoP proofs to check that htu is the discovered token endpoint on both the initial request and the nonce retry, including checking the retry’s nonce.
Rebased onto main and resolved the registration conflict by keeping both the management-options validator and the discovery post-configurer. All 125 targeted tests pass locally, including the convention test.
|
Hi @bhazen, is anything missing from my part? |
|
@paulomorgado Thank you for providing this PR. We currently have this scheduled to be released in ATM 4.4, which will hopefully land soon after the .Net 11 release. |


Summary
Adds OpenID Connect discovery to client credentials token acquisition.
ClientCredentialsClientcan now be configured with anAuthorityinstead of a hardcodedTokenEndpoint, and the token endpoint is resolved from the discovery document.This follows the discovery model already used by
OAuth2IntrospectionOptionsandOidcClient, so discovery is configured the same way across the libraries.What changed
Public API
ClientCredentialsClient.Authority(string?): the authority to run discovery against. A full discovery URL is also accepted when it ends inDiscoveryPolicy.DiscoveryDocumentPath(by default.well-known/openid-configuration).ClientCredentialsClient.DiscoveryPolicy(DiscoveryPolicy): the policy used to validate the discovery document. Defaults tonew(), the same asOAuth2IntrospectionOptions.DiscoveryPolicy.Validation
A client needs a
ClientIdand either aTokenEndpointor anAuthority.Token endpoint resolution
TokenEndpointis set, it's used directly and no discovery happens.GetDiscoveryDocumentAsyncand the configuredDiscoveryPolicy. This gives issuer validation, endpoint validation and the HTTPS requirement (with the loopback exception).AsyncLazycreated in a post-configurer. Concurrent first requests share one discovery call. The result is kept for the lifetime of the options, andTokenEndpointis never modified.AuthorityandTokenEndpointare read after all post-configurers have run, so apps can set or clear them in their ownPostConfigure.HttpClientas the token request: an explicitHttpClient, then the named client, then the default backchannel.htu, including on the nonce retry.Errors
Discovery failures throw
InvalidOperationException. The message depends onResponseErrorType, as inPostConfigureOAuth2IntrospectionOptions, and the underlying exception is kept as the inner exception. A discovery document without atoken_endpointgets its own message, which names the client.Tests
ClientCredentialsDiscoveryTestscovers resolution, policy enforcement (issuer, HTTPS, key set, additional endpoint hosts), concurrency and cancellation, retry after failure, late configuration throughPostConfigure, choice ofHttpClient, DPoP, and the error paths.ClientTokenManagementTestscovers validation andTokenEndpointprecedence.