Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
Update client credentials samples to use authority
Browse files Browse the repository at this point in the history
  • Loading branch information
josephdecock committed May 3, 2024
1 parent cc00081 commit 6584491
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
14 changes: 8 additions & 6 deletions samples/Worker/ClientAssertionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace WorkerService;

public class ClientAssertionService : IClientAssertionService
{
private readonly ITokenEndpointRetriever _tokenEndpointRetriever;
private readonly IOptionsMonitor<ClientCredentialsClient> _options;

private static string RsaKey =
Expand All @@ -35,12 +36,13 @@ public class ClientAssertionService : IClientAssertionService

private static SigningCredentials Credential = new (new JsonWebKey(RsaKey), "RS256");

public ClientAssertionService(IOptionsMonitor<ClientCredentialsClient> options)
public ClientAssertionService(ITokenEndpointRetriever tokenEndpointRetriever, IOptionsMonitor<ClientCredentialsClient> options)
{
_tokenEndpointRetriever = tokenEndpointRetriever;
_options = options;
}

public Task<ClientAssertion?> GetClientAssertionAsync(string? clientName = null, TokenRequestParameters? parameters = null)
public async Task<ClientAssertion?> GetClientAssertionAsync(string? clientName = null, TokenRequestParameters? parameters = null)
{
if (clientName == "demo.jwt")
{
Expand All @@ -49,7 +51,7 @@ public ClientAssertionService(IOptionsMonitor<ClientCredentialsClient> options)
var descriptor = new SecurityTokenDescriptor
{
Issuer = options.ClientId,
Audience = options.TokenEndpoint,
Audience = await _tokenEndpointRetriever.GetAsync(options),
Expires = DateTime.UtcNow.AddMinutes(1),
SigningCredentials = Credential,

Expand All @@ -64,13 +66,13 @@ public ClientAssertionService(IOptionsMonitor<ClientCredentialsClient> options)
var handler = new JsonWebTokenHandler();
var jwt = handler.CreateToken(descriptor);

return Task.FromResult<ClientAssertion?>(new ClientAssertion
return new ClientAssertion
{
Type = OidcConstants.ClientAssertionTypes.JwtBearer,
Value = jwt
});
};
}

return Task.FromResult<ClientAssertion?>(null);
return null;
}
}
7 changes: 3 additions & 4 deletions samples/Worker/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static IHostBuilder CreateHostBuilder(string[] args)
services.AddClientCredentialsTokenManagement()
.AddClient("demo", client =>
{
client.TokenEndpoint = "https://demo.duendesoftware.com/connect/token";
client.Authority = "https://demo.duendesoftware.com/";

client.ClientId = "m2m.short";
client.ClientSecret = "secret";
Expand All @@ -43,8 +43,7 @@ public static IHostBuilder CreateHostBuilder(string[] args)
})
.AddClient("demo.dpop", client =>
{
client.TokenEndpoint = "https://demo.duendesoftware.com/connect/token";
//client.TokenEndpoint = "https://localhost:5001/connect/token";
client.Authority = "https://demo.duendesoftware.com/";

client.ClientId = "m2m.dpop";
//client.ClientId = "m2m.dpop.nonce";
Expand All @@ -55,7 +54,7 @@ public static IHostBuilder CreateHostBuilder(string[] args)
})
.AddClient("demo.jwt", client =>
{
client.TokenEndpoint = "https://demo.duendesoftware.com/connect/token";
client.Authority = "https://demo.duendesoftware.com";
client.ClientId = "m2m.short.jwt";

client.Scope = "api";
Expand Down
16 changes: 9 additions & 7 deletions samples/WorkerDI/ClientAssertionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace WorkerService;

public class ClientAssertionService : IClientAssertionService
{
private readonly ITokenEndpointRetriever _tokenEndpoint;
private readonly IOptionsMonitor<ClientCredentialsClient> _options;

private static string RsaKey =
Expand All @@ -35,21 +36,22 @@ public class ClientAssertionService : IClientAssertionService

private static SigningCredentials Credential = new (new JsonWebKey(RsaKey), "RS256");

public ClientAssertionService(IOptionsMonitor<ClientCredentialsClient> options)
public ClientAssertionService(ITokenEndpointRetriever tokenEndpoint, IOptionsMonitor<ClientCredentialsClient> options)
{
_tokenEndpoint = tokenEndpoint;
_options = options;
}

public Task<ClientAssertion?> GetClientAssertionAsync(string? clientName = null, TokenRequestParameters? parameters = null)
public async Task<ClientAssertion?> GetClientAssertionAsync(string? clientName = null, TokenRequestParameters? parameters = null)
{
if (clientName == "demo.jwt")
{
var options = _options.Get(clientName);

var descriptor = new SecurityTokenDescriptor
{
Issuer = options.ClientId,
Audience = options.TokenEndpoint,
Audience = await _tokenEndpoint.GetAsync(options),
Expires = DateTime.UtcNow.AddMinutes(1),
SigningCredentials = Credential,

Expand All @@ -64,13 +66,13 @@ public ClientAssertionService(IOptionsMonitor<ClientCredentialsClient> options)
var handler = new JsonWebTokenHandler();
var jwt = handler.CreateToken(descriptor);

return Task.FromResult<ClientAssertion?>(new ClientAssertion
return new ClientAssertion
{
Type = OidcConstants.ClientAssertionTypes.JwtBearer,
Value = jwt
});
};
}

return Task.FromResult<ClientAssertion?>(null);
return null;
}
}
4 changes: 1 addition & 3 deletions samples/WorkerDI/ClientCredentialsClientConfigureOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ public void Configure(string? name, ClientCredentialsClient options)
{
if (name == "demo.jwt")
{
var disco = _cache.GetAsync().GetAwaiter().GetResult();

options.TokenEndpoint = disco.TokenEndpoint;
options.Authority = "https://demo.duendesoftware.com";
options.ClientId = "m2m.short.jwt";
options.Scope = "api";
}
Expand Down
3 changes: 2 additions & 1 deletion samples/WorkerDI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public static IHostBuilder CreateHostBuilder(string[] args)
// alternative way to add a client
services.Configure<ClientCredentialsClient>("demo", client =>
{
client.TokenEndpoint = "https://demo.duendesoftware.com/connect/token";
client.Authority = "https://demo.duendesoftware.com/";
// client.TokenEndpoint = "https://demo.duendesoftware.com/connect/token";

client.ClientId = "m2m.short";
client.ClientSecret = "secret";
Expand Down

0 comments on commit 6584491

Please sign in to comment.