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

Commit 5c41c64

Browse files
authored
Merge pull request #73 from kallayj/clientid-null-exception
Adds a specific exception when the clientId is empty.
2 parents f398940 + 4a96376 commit 5c41c64

File tree

3 files changed

+100
-42
lines changed

3 files changed

+100
-42
lines changed

src/Duende.AccessTokenManagement/ClientCredentialsTokenEndpointService.cs

+17-2
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,24 @@ public virtual async Task<ClientCredentialsToken> RequestToken(
5858
{
5959
var client = _options.Get(clientName);
6060

61-
if (string.IsNullOrWhiteSpace(client.TokenEndpoint) || string.IsNullOrEmpty(client.ClientId))
61+
var clientIdMissing = string.IsNullOrWhiteSpace(client.ClientId);
62+
var tokenEndpointMissing = string.IsNullOrWhiteSpace(client.TokenEndpoint);
63+
64+
// If both are missing, we infer that this client is just not set up at all
65+
if (clientIdMissing && tokenEndpointMissing)
66+
{
67+
throw new InvalidOperationException($"Unknown client {clientName}");
68+
}
69+
70+
// Otherwise, if we don't have a specific value that is required, throw an appropriate exception
71+
if (string.IsNullOrWhiteSpace(client.ClientId))
72+
{
73+
throw new InvalidOperationException($"No ClientId configured for client {clientName}");
74+
}
75+
76+
if (string.IsNullOrWhiteSpace(client.TokenEndpoint))
6277
{
63-
throw new InvalidOperationException("unknown client");
78+
throw new InvalidOperationException($"No TokenEndpoint configured for client {clientName}");
6479
}
6580

6681
var request = new ClientCredentialsTokenRequest
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
2-
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3-
4-
using System;
5-
using Duende.AccessTokenManagement;
6-
7-
namespace Microsoft.Extensions.DependencyInjection;
8-
9-
/// <summary>
10-
/// Builder for client credential clients
11-
/// </summary>
12-
public class ClientCredentialsTokenManagementBuilder
13-
{
14-
private readonly IServiceCollection _services;
15-
16-
/// <summary>
17-
/// ctor
18-
/// </summary>
19-
/// <param name="services"></param>
20-
public ClientCredentialsTokenManagementBuilder(IServiceCollection services)
21-
{
22-
_services = services;
23-
}
24-
25-
/// <summary>
26-
/// Adds a client credentials client to the token management system
27-
/// </summary>
28-
/// <param name="name"></param>
29-
/// <param name="configureOptions"></param>
30-
/// <returns></returns>
31-
public ClientCredentialsTokenManagementBuilder AddClient(string name, Action<ClientCredentialsClient> configureOptions)
32-
{
33-
_services.Configure(name, configureOptions);
34-
return this;
35-
}
1+
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
using System;
5+
using Duende.AccessTokenManagement;
6+
7+
namespace Microsoft.Extensions.DependencyInjection;
8+
9+
/// <summary>
10+
/// Builder for client credential clients
11+
/// </summary>
12+
public class ClientCredentialsTokenManagementBuilder
13+
{
14+
private readonly IServiceCollection _services;
15+
16+
/// <summary>
17+
/// ctor
18+
/// </summary>
19+
/// <param name="services"></param>
20+
public ClientCredentialsTokenManagementBuilder(IServiceCollection services)
21+
{
22+
_services = services;
23+
}
24+
25+
/// <summary>
26+
/// Adds a client credentials client to the token management system
27+
/// </summary>
28+
/// <param name="name"></param>
29+
/// <param name="configureOptions"></param>
30+
/// <returns></returns>
31+
public ClientCredentialsTokenManagementBuilder AddClient(string name, Action<ClientCredentialsClient> configureOptions)
32+
{
33+
_services.Configure(name, configureOptions);
34+
return this;
35+
}
3636
}

test/Tests/ClientTokenManagementTests.cs

+48-5
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,55 @@ public async Task Unknown_client_should_throw_exception()
2424
var provider = services.BuildServiceProvider();
2525
var sut = provider.GetRequiredService<IClientCredentialsTokenManagementService>();
2626

27-
async Task action()
28-
{
29-
var token = await sut.GetAccessTokenAsync("unknown");
30-
}
27+
var action = async () => await sut.GetAccessTokenAsync("unknown");
28+
29+
(await Should.ThrowAsync<InvalidOperationException>(action))
30+
.Message.ShouldBe("Unknown client unknown");
31+
}
32+
33+
[Fact]
34+
public async Task Missing_client_id_throw_exception()
35+
{
36+
var services = new ServiceCollection();
37+
38+
services.AddDistributedMemoryCache();
39+
services.AddClientCredentialsTokenManagement()
40+
.AddClient("test", client =>
41+
{
42+
client.TokenEndpoint = "https://as/connect/token";
43+
client.ClientId = null;
44+
});
45+
46+
var provider = services.BuildServiceProvider();
47+
var sut = provider.GetRequiredService<IClientCredentialsTokenManagementService>();
48+
49+
var action = async () => await sut.GetAccessTokenAsync("test");
50+
51+
(await Should.ThrowAsync<InvalidOperationException>(action))
52+
.Message.ShouldBe("No ClientId configured for client test");
53+
}
54+
55+
56+
[Fact]
57+
public async Task Missing_tokenEndpoint_throw_exception()
58+
{
59+
var services = new ServiceCollection();
60+
61+
services.AddDistributedMemoryCache();
62+
services.AddClientCredentialsTokenManagement()
63+
.AddClient("test", client =>
64+
{
65+
client.TokenEndpoint = null;
66+
client.ClientId = "test";
67+
});
68+
69+
var provider = services.BuildServiceProvider();
70+
var sut = provider.GetRequiredService<IClientCredentialsTokenManagementService>();
71+
72+
var action = async () => await sut.GetAccessTokenAsync("test");
3173

32-
await Should.ThrowAsync<InvalidOperationException>(action);
74+
(await Should.ThrowAsync<InvalidOperationException>(action))
75+
.Message.ShouldBe("No TokenEndpoint configured for client test");
3376
}
3477

3578
[Theory]

0 commit comments

Comments
 (0)