Skip to content

Commit 8362428

Browse files
committed
Merge branch 'main' of https://github.com/loresoft/HashGate
2 parents 192b7c8 + e8a87f5 commit 8362428

File tree

4 files changed

+113
-3
lines changed

4 files changed

+113
-3
lines changed

.github/workflows/dotnet.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
name: Build
2+
permissions:
3+
contents: read
24

35
env:
46
DOTNET_NOLOGO: true
@@ -57,7 +59,7 @@ jobs:
5759

5860
- name: Upload Packages
5961
if: success() && github.event_name != 'pull_request'
60-
uses: actions/upload-artifact@v4
62+
uses: actions/upload-artifact@v5
6163
with:
6264
name: packages
6365
path: "${{env.BUILD_PATH}}"
@@ -66,10 +68,13 @@ jobs:
6668
runs-on: ubuntu-latest
6769
needs: build
6870
if: success() && github.event_name != 'pull_request' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v'))
71+
permissions:
72+
contents: read
73+
packages: write
6974

7075
steps:
7176
- name: Download Artifact
72-
uses: actions/download-artifact@v5
77+
uses: actions/download-artifact@v6
7378
with:
7479
name: packages
7580

src/HashGate.AspNetCore/HmacAuthenticationShared.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
using System.Security.Cryptography;
22
using System.Text;
33

4-
namespace HashGate;
4+
#if HTTP_CLIENT
5+
namespace HashGate.HttpClient;
6+
#else
7+
namespace HashGate.AspNetCore;
8+
#endif
59

610
/// <summary>
711
/// Provides shared utilities and constants for HMAC authentication implementation.

src/HashGate.HttpClient/HashGate.HttpClient.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
<PackageTags>aspnetcore;hmac;authentication;http;client;security</PackageTags>
99
</PropertyGroup>
1010

11+
<PropertyGroup>
12+
<DefineConstants>$(DefineConstants);HTTP_CLIENT</DefineConstants>
13+
</PropertyGroup>
14+
1115
<ItemGroup>
1216
<Compile Include="..\HashGate.AspNetCore\HmacAuthenticationShared.cs" Link="HmacAuthenticationShared.cs" />
1317
</ItemGroup>
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using Microsoft.Extensions.Options;
2+
3+
namespace HashGate.HttpClient;
4+
5+
/// <summary>
6+
/// Provides a fluent API for building and configuring <see cref="System.Net.Http.HttpClient"/> instances
7+
/// with custom message handlers and HMAC authentication.
8+
/// </summary>
9+
/// <example>
10+
/// <code>
11+
/// var httpClient = new HttpClientBuilder()
12+
/// .AddHmacAuthentication("myClientId", "mySecretKey")
13+
/// .Configure(client =>
14+
/// {
15+
/// client.BaseAddress = new Uri("https://api.example.com");
16+
/// client.Timeout = TimeSpan.FromSeconds(30);
17+
/// })
18+
/// .Build();
19+
///
20+
/// var response = await httpClient.GetAsync("/api/endpoint");
21+
/// </code>
22+
/// </example>
23+
public class HttpClientBuilder
24+
{
25+
private readonly List<DelegatingHandler> _handlers = [];
26+
private Action<System.Net.Http.HttpClient>? _clientConfigurator;
27+
28+
/// <summary>
29+
/// Adds a custom <see cref="DelegatingHandler"/> to the HTTP client pipeline.
30+
/// </summary>
31+
/// <param name="handler">The delegating handler to add to the pipeline.</param>
32+
/// <returns>The current <see cref="HttpClientBuilder"/> instance for method chaining.</returns>
33+
public HttpClientBuilder AddHandler(DelegatingHandler handler)
34+
{
35+
_handlers.Add(handler);
36+
return this;
37+
}
38+
39+
/// <summary>
40+
/// Adds HMAC authentication to the HTTP client pipeline using the specified client ID and secret.
41+
/// </summary>
42+
/// <param name="client">The client identifier used for HMAC authentication.</param>
43+
/// <param name="secret">The secret key used for HMAC signature generation.</param>
44+
/// <param name="signedHeaders">Optional list of HTTP headers to include in the HMAC signature.
45+
/// If not specified, default headers will be used.</param>
46+
/// <returns>The current <see cref="HttpClientBuilder"/> instance for method chaining.</returns>
47+
public HttpClientBuilder AddHmacAuthentication(string client, string secret, IReadOnlyList<string>? signedHeaders = null)
48+
{
49+
var options = new HmacAuthenticationOptions
50+
{
51+
Client = client,
52+
Secret = secret,
53+
SignedHeaders = signedHeaders ?? HmacAuthenticationShared.DefaultSignedHeaders
54+
};
55+
56+
var wrapper = new OptionsWrapper<HmacAuthenticationOptions>(options);
57+
var handler = new HmacAuthenticationHttpHandler(wrapper);
58+
59+
_handlers.Add(handler);
60+
61+
return this;
62+
}
63+
64+
/// <summary>
65+
/// Configures the <see cref="System.Net.Http.HttpClient"/> instance with additional settings.
66+
/// </summary>
67+
/// <param name="configure">An action to configure the HTTP client (e.g., setting base address, timeout, default headers).</param>
68+
/// <returns>The current <see cref="HttpClientBuilder"/> instance for method chaining.</returns>
69+
public HttpClientBuilder Configure(Action<System.Net.Http.HttpClient> configure)
70+
{
71+
_clientConfigurator = configure;
72+
return this;
73+
}
74+
75+
/// <summary>
76+
/// Builds and returns a configured <see cref="System.Net.Http.HttpClient"/> instance
77+
/// with all registered handlers and configurations applied.
78+
/// </summary>
79+
/// <returns>A fully configured <see cref="System.Net.Http.HttpClient"/> instance.</returns>
80+
public System.Net.Http.HttpClient Build()
81+
{
82+
HttpMessageHandler pipeline = new HttpClientHandler();
83+
84+
// Chain handlers in reverse order
85+
for (int i = _handlers.Count - 1; i >= 0; i--)
86+
{
87+
_handlers[i].InnerHandler = pipeline;
88+
pipeline = _handlers[i];
89+
}
90+
91+
var client = new System.Net.Http.HttpClient(pipeline);
92+
93+
_clientConfigurator?.Invoke(client);
94+
95+
return client;
96+
}
97+
}

0 commit comments

Comments
 (0)