-
Notifications
You must be signed in to change notification settings - Fork 68
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
Add Authority to ClientCredentials options #102
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 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 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 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 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 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 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
14 changes: 14 additions & 0 deletions
14
src/Duende.AccessTokenManagement/Interfaces/ITokenEndpointRetriever.cs
This file contains 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,14 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace Duende.AccessTokenManagement; | ||
|
||
/// <summary> | ||
/// Retrieves the token endpoint either using discovery or static configuration | ||
/// </summary> | ||
public interface ITokenEndpointRetriever | ||
{ | ||
/// <summary> | ||
/// Gets the token endpoint | ||
/// </summary> | ||
Task<string> GetAsync(ClientCredentialsClient client); | ||
} |
This file contains 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,24 @@ | ||
// 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.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Duende.AccessTokenManagement; | ||
|
||
// Note that this is duplicated in Duende.AccessTokenManagement.OpenIdConnect, | ||
// but we can't share the code because it is internal. | ||
internal static class StringExtensions | ||
{ | ||
[DebuggerStepThrough] | ||
public static bool IsMissing([NotNullWhen(false)]this string? value) | ||
{ | ||
return string.IsNullOrWhiteSpace(value); | ||
} | ||
|
||
[DebuggerStepThrough] | ||
public static bool IsPresent([NotNullWhen(true)]this string? value) | ||
{ | ||
return !string.IsNullOrWhiteSpace(value); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/Duende.AccessTokenManagement/TokenEndpointRetriever.cs
This file contains 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,45 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using IdentityModel.Client; | ||
|
||
namespace Duende.AccessTokenManagement; | ||
|
||
/// <inheritdoc/> | ||
public class TokenEndpointRetriever : ITokenEndpointRetriever | ||
{ | ||
private readonly Dictionary<string, DiscoveryCache> _caches = new(); | ||
|
||
private DiscoveryCache GetDiscoCache(string authority) | ||
{ | ||
if (!_caches.ContainsKey(authority)) | ||
{ | ||
_caches[authority] = new DiscoveryCache(authority); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should support DiscoveryPolicy and Timeout here. |
||
} | ||
return _caches[authority]; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task<string> GetAsync(ClientCredentialsClient client) | ||
{ | ||
if (client.Authority.IsPresent()) | ||
{ | ||
var discoCache = GetDiscoCache(client.Authority); | ||
var disco = await discoCache.GetAsync(); | ||
if(disco.IsError) | ||
{ | ||
throw new InvalidOperationException("Failed to retrieve disco"); | ||
} | ||
return disco.TokenEndpoint ?? throw new InvalidOperationException("Disco does not contain token endpoint"); | ||
} | ||
else if (client.TokenEndpoint.IsPresent()) | ||
{ | ||
return client.TokenEndpoint; | ||
} | ||
else | ||
{ | ||
throw new InvalidOperationException("No token endpoint or authority configured"); | ||
} | ||
|
||
} | ||
} |
This file contains 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 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,12 @@ | ||
// Copyright (c) Duende Software. All rights reserved. | ||
// See LICENSE in the project root for license information. | ||
|
||
namespace Duende.AccessTokenManagement.Tests; | ||
|
||
public class TestTokenEndpointRetriever(string tokenEndpoint = "https://identityserver/connect/token") : ITokenEndpointRetriever | ||
{ | ||
public Task<string> GetAsync(ClientCredentialsClient client) | ||
{ | ||
return Task.FromResult(tokenEndpoint); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For most usage, you just set the authority and let discovery happen in the background. But sometimes you do need to know what the token endpoint is - for example, here when we make a client assertion, we need to use the token endpoint as the audience. This is slightly more complex than just reading the option as we used to - notably this uses a new service and is an async operation. I don't think we can really avoid this though, because invoking the discovery endpoint *is async. At one point, we talked about doing this in a PostConfigureOptions, but that doesn't support asynchronicity.