This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Authority to ClientCredentials options
If authority is set, we use it to retrieve the discovery document, and use that to configure the token endpoint. Because this is an async operation, we have a new abstraction for retrieval of the token endpoint
- Loading branch information
1 parent
5d114aa
commit cc00081
Showing
10 changed files
with
118 additions
and
11 deletions.
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
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); | ||
} | ||
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); | ||
} | ||
} |