Skip to content
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

Provide option to set AuthorizationHeaderStyle #65

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -14,12 +14,12 @@ public class ClientCredentialsClient
/// The address of the token endpoint
/// </summary>
public string? TokenEndpoint { get; set; }

/// <summary>
/// The client ID
/// </summary>
public string? ClientId { get; set; }

/// <summary>
/// The static (shared) client secret
/// </summary>
Expand All @@ -30,11 +30,19 @@ public class ClientCredentialsClient
/// </summary>
public ClientCredentialStyle ClientCredentialStyle { get; set; }

/// <summary>
/// Gets or sets the basic authentication header style (classic HTTP vs OAuth 2).
/// </summary>
/// <value>
/// The basic authentication header style.
/// </value>
public BasicAuthenticationHeaderStyle AuthorizationHeaderStyle { get; set; } = BasicAuthenticationHeaderStyle.Rfc6749;

/// <summary>
/// The scope
/// </summary>
public string? Scope { get; set; }

/// <summary>
/// The resource
/// </summary>
Expand All @@ -49,7 +57,7 @@ public class ClientCredentialsClient
/// Additional parameters to send with token requests.
/// </summary>
public Parameters Parameters { get; set; } = new Parameters();

/// <summary>
/// The HTTP client instance to use for the back-channel operations, will override the HTTP client name if set
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,18 @@ public virtual async Task<ClientCredentialsToken> RequestToken(
ClientId = client.ClientId,
ClientSecret = client.ClientSecret,
ClientCredentialStyle = client.ClientCredentialStyle,
AuthorizationHeaderStyle = client.AuthorizationHeaderStyle
};

request.Parameters.AddRange(client.Parameters);

parameters ??= new TokenRequestParameters();

if (!string.IsNullOrWhiteSpace(parameters.Scope))
{
request.Scope = parameters.Scope;
}

if (!string.IsNullOrWhiteSpace(parameters.Resource))
{
request.Resource.Clear();
Expand All @@ -103,14 +104,14 @@ public virtual async Task<ClientCredentialsToken> RequestToken(
else
{
var assertion = await _clientAssertionService.GetClientAssertionAsync(clientName).ConfigureAwait(false);

if (assertion != null)
{
request.ClientAssertion = assertion;
request.ClientCredentialStyle = ClientCredentialStyle.PostBody;
}
}

request.Options.TryAdd(ClientCredentialsTokenManagementDefaults.TokenRequestParametersOptionsName, parameters);

var key = await _dPoPKeyMaterialService.GetKeyAsync(clientName);
Expand All @@ -134,19 +135,19 @@ public virtual async Task<ClientCredentialsToken> RequestToken(
}
else if (!string.IsNullOrWhiteSpace(client.HttpClientName))
{
httpClient = _httpClientFactory.CreateClient(client.HttpClientName);
httpClient = _httpClientFactory.CreateClient(client.HttpClientName);
}
else
{
httpClient = _httpClientFactory.CreateClient(ClientCredentialsTokenManagementDefaults.BackChannelHttpClientName);
httpClient = _httpClientFactory.CreateClient(ClientCredentialsTokenManagementDefaults.BackChannelHttpClientName);
}

_logger.LogDebug("Requesting client credentials access token at endpoint: {endpoint}", request.Address);
var response = await httpClient.RequestClientCredentialsTokenAsync(request, cancellationToken).ConfigureAwait(false);

if (response.IsError &&
(response.Error == OidcConstants.TokenErrors.UseDPoPNonce || response.Error == OidcConstants.TokenErrors.InvalidDPoPProof) &&
key != null &&
if (response.IsError &&
(response.Error == OidcConstants.TokenErrors.UseDPoPNonce || response.Error == OidcConstants.TokenErrors.InvalidDPoPProof) &&
key != null &&
response.DPoPNonce != null)
{
_logger.LogDebug("Token request failed with DPoP nonce error. Retrying with new nonce.");
Expand All @@ -173,7 +174,7 @@ public virtual async Task<ClientCredentialsToken> RequestToken(
Error = response.Error
};
}

return new ClientCredentialsToken
{
AccessToken = response.AccessToken,
Expand Down