Skip to content

Commit

Permalink
Merge pull request #10 from dozer75/add-additional-headers-support
Browse files Browse the repository at this point in the history
Add additional headers support
  • Loading branch information
dozer75 authored Jan 14, 2025
2 parents 5fb6709 + f23ea75 commit 12b56a2
Show file tree
Hide file tree
Showing 37 changed files with 414 additions and 227 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/master-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ jobs:
steps:
- name: Checkout source
uses: actions/checkout@v4
- name: Setup .NET 8.0.x
- name: Setup .NET 9.0.x
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'
dotnet-version: '9.0.x'
- name: Restore solution
run: dotnet restore
- name: Build solution
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/master-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:
echo "VERSION=${VERSION#v}" >> $GITHUB_ENV
- name: Checkout source
uses: actions/checkout@v4
- name: Setup .NET 8.0.x
- name: Setup .NET 9.0.x
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'
dotnet-version: '9.0.x'
- name: Restore solution
run: dotnet restore
- name: Build solution (pre-release)
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/master-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ jobs:
steps:
- name: Checkout source
uses: actions/checkout@v4
- name: Setup .NET 8.0.x
- name: Setup .NET 9.0.x
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'
dotnet-version: '9.0.x'
- name: Restore solution
run: dotnet restore
- name: Build solution
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -344,5 +344,8 @@ healthchecksdb
/test/coverage.opencover.xml
/test/result.json

# POC related
/poc

# Custom exclusions
*.AssemblyAttributes
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ The package currently supports the following authentication methods:
- OAuth2
- Client credentials

> Please note: The .NET Standard 2.0 and .NET 6 targeted packages references the LTS version 6 of the dependent
NuGet packages. The reason for this is that while it is possible to run .NET 7 NuGet packages on .NET 6, it is known that some
of them may introduce unexpected behavior. It is recommended (especially from the ASP.NET Core team) limit usage of .NET 7
based NuGet packages on .NET 6 runtime.

## USAGE

Add the NuGet package `KISS.HttpClientAuthentication` to your project and whenever a
Expand Down Expand Up @@ -75,16 +70,25 @@ Authentication using OAuth2.

##### Client credentials

Using OAuth2 client credentials, all settings except `DisableTokenCache` and `Scope` is required.
Using OAuth2 client credentials, all settings except `DisableTokenCache`, `Scope` and
`TokenEndpoint`'s `Additional*Parameters` is required.

```
"<section name>": {
"AuthenticationProvider": "OAuth2",
"OAuth2": {
"AuthorizationEndpoint": "<OAuth2 token endpoint>",
"DisableTokenCache": false,
"GrantType": "ClientCredentials",
"Scope": "<Optional scopes separated by space>",
"TokenEndpoint": {
"Url": "<OAuth2 token endpoint>",
"AdditionalHeaderParameters": {
},
"AdditionalBodyParameters": {
},
"AdditionalQueryParameters": {
}
},
"ClientCredentials": {
"ClientId": "<Unique client id>",
"ClientSecret": "<Secret connected to the client id>"
Expand All @@ -93,6 +97,9 @@ Using OAuth2 client credentials, all settings except `DisableTokenCache` and `Sc
}
```

The `Additional*Parameters` configuration is dynamic, any configuration in these will
be added to their respective parts of the request accordingly. Please note that the
`AdditionalQueryParameters` will be url encoded.

### Examples

Expand Down
2 changes: 1 addition & 1 deletion src/HttpClientAuthentication/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2024 Rune Gulbrandsen.
// Copyright © 2025 Rune Gulbrandsen.
// All rights reserved. Licensed under the MIT License; see LICENSE.txt.

using System.Runtime.CompilerServices;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2024 Rune Gulbrandsen.
// Copyright © 2025 Rune Gulbrandsen.
// All rights reserved. Licensed under the MIT License; see LICENSE.txt.

namespace KISS.HttpClientAuthentication.Configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2024 Rune Gulbrandsen.
// Copyright © 2025 Rune Gulbrandsen.
// All rights reserved. Licensed under the MIT License; see LICENSE.txt.

namespace KISS.HttpClientAuthentication.Configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2024 Rune Gulbrandsen.
// Copyright © 2025 Rune Gulbrandsen.
// All rights reserved. Licensed under the MIT License; see LICENSE.txt.

namespace KISS.HttpClientAuthentication.Configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2024 Rune Gulbrandsen.
// Copyright © 2025 Rune Gulbrandsen.
// All rights reserved. Licensed under the MIT License; see LICENSE.txt.

using KISS.HttpClientAuthentication.Constants;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2024 Rune Gulbrandsen.
// Copyright © 2025 Rune Gulbrandsen.
// All rights reserved. Licensed under the MIT License; see LICENSE.txt.

using KISS.HttpClientAuthentication.Constants;
Expand All @@ -10,12 +10,6 @@ namespace KISS.HttpClientAuthentication.Configuration
/// </summary>
public sealed class OAuth2Configuration
{
/// <summary>
/// Gets or sets the authorization endpoint used by some <see cref="AuthenticationProvider"/>
/// configuration.
/// </summary>
public Uri AuthorizationEndpoint { get; set; } = default!;

/// <summary>
/// Gets or sets the authorization scheme to use if <see cref="AuthenticationHeader"/> is
/// Authorization or the selected <see cref="AuthenticationProvider"/> uses Authorization as default header.
Expand Down Expand Up @@ -47,5 +41,13 @@ public sealed class OAuth2Configuration
/// Scopes must be separated with a space.
/// </remarks>
public string? Scope { get; set; }

/// <summary>
/// Gets or sets the token endpoint.
/// </summary>
/// <remarks>
/// Replaces <see cref="AuthorizationEndpoint"/>.
/// </remarks>
public OAuth2Endpoint TokenEndpoint { get; set; } = default!;
}
}
40 changes: 40 additions & 0 deletions src/HttpClientAuthentication/Configuration/OAuth2Endpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright © 2025 Rune Gulbrandsen.
// All rights reserved. Licensed under the MIT License; see LICENSE.txt.

namespace KISS.HttpClientAuthentication.Configuration
{
/// <summary>
/// Endpoint configuration for OAuth2 endpoints
/// </summary>
public sealed partial class OAuth2Endpoint
{
/// <summary>
/// Gets or sets the Url to the OAuth2 endpoint.
/// </summary>
public Uri Url { get; set; } = default!;

/// <summary>
/// Gets a dictionary that can contain additional headers that will be
/// supplied when requesting <see cref="Url"/>.
/// </summary>
public Dictionary<string, string> AdditionalHeaderParameters { get; } = [];


/// <summary>
/// Gets a collection of additional form body parameters that will be
/// supplied when requesting <see cref="Url"/>.
/// </summary>
public Dictionary<string, string> AdditionalBodyParameters { get; } = [];

/// <summary>
/// Gets a collection of additional query string parameters that will
/// be supplied when requesting <see cref="Url"/>.
/// </summary>
public Dictionary<string, string> AdditionalQueryParameters { get; } = [];

public override string ToString()
{
return Url.ToString();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2024 Rune Gulbrandsen.
// Copyright © 2025 Rune Gulbrandsen.
// All rights reserved. Licensed under the MIT License; see LICENSE.txt.

namespace KISS.HttpClientAuthentication.Constants
Expand Down
2 changes: 1 addition & 1 deletion src/HttpClientAuthentication/Constants/OAuth2GrantType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2024 Rune Gulbrandsen.
// Copyright © 2025 Rune Gulbrandsen.
// All rights reserved. Licensed under the MIT License; see LICENSE.txt.

namespace KISS.HttpClientAuthentication.Constants
Expand Down
2 changes: 1 addition & 1 deletion src/HttpClientAuthentication/Constants/OAuth2Keyword.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2024 Rune Gulbrandsen.
// Copyright © 2025 Rune Gulbrandsen.
// All rights reserved. Licensed under the MIT License; see LICENSE.txt.

namespace KISS.HttpClientAuthentication.Constants
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2024 Rune Gulbrandsen.
// Copyright © 2025 Rune Gulbrandsen.
// All rights reserved. Licensed under the MIT License; see LICENSE.txt.

using KISS.HttpClientAuthentication.Configuration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2024 Rune Gulbrandsen.
// Copyright © 2025 Rune Gulbrandsen.
// All rights reserved. Licensed under the MIT License; see LICENSE.txt.

namespace KISS.HttpClientAuthentication.Handlers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2024 Rune Gulbrandsen.
// Copyright © 2025 Rune Gulbrandsen.
// All rights reserved. Licensed under the MIT License; see LICENSE.txt.

using System.Net.Http.Headers;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2024 Rune Gulbrandsen.
// Copyright © 2025 Rune Gulbrandsen.
// All rights reserved. Licensed under the MIT License; see LICENSE.txt.

namespace KISS.HttpClientAuthentication.Handlers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2024 Rune Gulbrandsen.
// Copyright © 2025 Rune Gulbrandsen.
// All rights reserved. Licensed under the MIT License; see LICENSE.txt.

using System.Net.Http.Headers;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2024 Rune Gulbrandsen.
// Copyright © 2025 Rune Gulbrandsen.
// All rights reserved. Licensed under the MIT License; see LICENSE.txt.

using System.Text.Json.Serialization;
Expand Down
2 changes: 1 addition & 1 deletion src/HttpClientAuthentication/Helpers/ErrorResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2024 Rune Gulbrandsen.
// Copyright © 2025 Rune Gulbrandsen.
// All rights reserved. Licensed under the MIT License; see LICENSE.txt.

using System.Text.Json.Serialization;
Expand Down
2 changes: 1 addition & 1 deletion src/HttpClientAuthentication/Helpers/IOAuth2Provider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2024 Rune Gulbrandsen.
// Copyright © 2025 Rune Gulbrandsen.
// All rights reserved. Licensed under the MIT License; see LICENSE.txt.

using KISS.HttpClientAuthentication.Configuration;
Expand Down
Loading

0 comments on commit 12b56a2

Please sign in to comment.