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

Add a specific exception when the clientId is empty #73

Merged
merged 5 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,24 @@ public virtual async Task<ClientCredentialsToken> RequestToken(
{
var client = _options.Get(clientName);

if (string.IsNullOrWhiteSpace(client.TokenEndpoint) || string.IsNullOrEmpty(client.ClientId))
var clientIdMissing = string.IsNullOrWhiteSpace(client.ClientId);
var tokenEndpointMissing = string.IsNullOrWhiteSpace(client.TokenEndpoint);

// If both are missing, we infer that this client is just not set up at all
if (clientIdMissing && tokenEndpointMissing)
{
throw new InvalidOperationException($"Unknown client {clientName}");
}

// Otherwise, if we don't have a specific value that is required, throw an appropriate exception
if (string.IsNullOrWhiteSpace(client.ClientId))
{
throw new InvalidOperationException($"No ClientId configured for client {clientName}");
}

if (string.IsNullOrWhiteSpace(client.TokenEndpoint))
{
throw new InvalidOperationException("unknown client");
throw new InvalidOperationException($"No TokenEndpoint configured for client {clientName}");
}

var request = new ClientCredentialsTokenRequest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
// 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;
using Duende.AccessTokenManagement;

namespace Microsoft.Extensions.DependencyInjection;

/// <summary>
/// Builder for client credential clients
/// </summary>
public class ClientCredentialsTokenManagementBuilder
{
private readonly IServiceCollection _services;

/// <summary>
/// ctor
/// </summary>
/// <param name="services"></param>
public ClientCredentialsTokenManagementBuilder(IServiceCollection services)
{
_services = services;
}

/// <summary>
/// Adds a client credentials client to the token management system
/// </summary>
/// <param name="name"></param>
/// <param name="configureOptions"></param>
/// <returns></returns>
public ClientCredentialsTokenManagementBuilder AddClient(string name, Action<ClientCredentialsClient> configureOptions)
{
_services.Configure(name, configureOptions);
return this;
}
// 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;
using Duende.AccessTokenManagement;
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// Builder for client credential clients
/// </summary>
public class ClientCredentialsTokenManagementBuilder
{
private readonly IServiceCollection _services;
/// <summary>
/// ctor
/// </summary>
/// <param name="services"></param>
public ClientCredentialsTokenManagementBuilder(IServiceCollection services)
{
_services = services;
}
/// <summary>
/// Adds a client credentials client to the token management system
/// </summary>
/// <param name="name"></param>
/// <param name="configureOptions"></param>
/// <returns></returns>
public ClientCredentialsTokenManagementBuilder AddClient(string name, Action<ClientCredentialsClient> configureOptions)
{
_services.Configure(name, configureOptions);
return this;
}
}
53 changes: 48 additions & 5 deletions test/Tests/ClientTokenManagementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,55 @@ public async Task Unknown_client_should_throw_exception()
var provider = services.BuildServiceProvider();
var sut = provider.GetRequiredService<IClientCredentialsTokenManagementService>();

async Task action()
{
var token = await sut.GetAccessTokenAsync("unknown");
}
var action = async () => await sut.GetAccessTokenAsync("unknown");

(await Should.ThrowAsync<InvalidOperationException>(action))
.Message.ShouldBe("Unknown client unknown");
}

[Fact]
public async Task Missing_client_id_throw_exception()
{
var services = new ServiceCollection();

services.AddDistributedMemoryCache();
services.AddClientCredentialsTokenManagement()
.AddClient("test", client =>
{
client.TokenEndpoint = "https://as/connect/token";
client.ClientId = null;
});

var provider = services.BuildServiceProvider();
var sut = provider.GetRequiredService<IClientCredentialsTokenManagementService>();

var action = async () => await sut.GetAccessTokenAsync("test");

(await Should.ThrowAsync<InvalidOperationException>(action))
.Message.ShouldBe("No ClientId configured for client test");
}


[Fact]
public async Task Missing_tokenEndpoint_throw_exception()
{
var services = new ServiceCollection();

services.AddDistributedMemoryCache();
services.AddClientCredentialsTokenManagement()
.AddClient("test", client =>
{
client.TokenEndpoint = null;
client.ClientId = "test";
});

var provider = services.BuildServiceProvider();
var sut = provider.GetRequiredService<IClientCredentialsTokenManagementService>();

var action = async () => await sut.GetAccessTokenAsync("test");

await Should.ThrowAsync<InvalidOperationException>(action);
(await Should.ThrowAsync<InvalidOperationException>(action))
.Message.ShouldBe("No TokenEndpoint configured for client test");
}

[Theory]
Expand Down
Loading