Registering HMAC handler on VPK CLI

This commit is contained in:
Kevin Bost
2024-05-18 10:23:09 -07:00
parent f520c89b9e
commit b7c0058de3
3 changed files with 12 additions and 15 deletions

View File

@@ -1,26 +1,21 @@
using System; using System.Net.Http;
using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Velopack.Packaging.Flow;
namespace Velopack.Build; namespace Velopack.Packaging.Flow;
internal class HmacAuthHttpClientHandler : HttpClientHandler public class HmacAuthHttpClientHandler : DelegatingHandler
{ {
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{ {
if (request.Headers.Authorization?.Scheme == HmacHelper.HmacScheme && if (request.Headers.Authorization?.Scheme == HmacHelper.HmacScheme &&
request.Headers.Authorization.Parameter is { } authParameter && request.Headers.Authorization.Parameter is { } authParameter &&
authParameter.Split(':') is var keyParts && authParameter.Split(':') is var keyParts &&
keyParts.Length == 2) keyParts.Length == 2) {
{ var hashedId = keyParts[0];
string hashedId = keyParts[0]; var key = keyParts[1];
string key = keyParts[1]; var nonce = Guid.NewGuid().ToString();
string nonce = Guid.NewGuid().ToString();
uint secondsSinceEpoch = HmacHelper.GetSecondsSinceEpoch(); var secondsSinceEpoch = HmacHelper.GetSecondsSinceEpoch();
var signature = HmacHelper.BuildSignature(hashedId, request.Method.Method, request.RequestUri?.AbsoluteUri ?? "", secondsSinceEpoch, nonce); var signature = HmacHelper.BuildSignature(hashedId, request.Method.Method, request.RequestUri?.AbsoluteUri ?? "", secondsSinceEpoch, nonce);
var secret = HmacHelper.Calculate(Convert.FromBase64String(key), signature); var secret = HmacHelper.Calculate(Convert.FromBase64String(key), signature);
request.Headers.Authorization = BuildHeader(hashedId, secret, nonce, secondsSinceEpoch); request.Headers.Authorization = BuildHeader(hashedId, secret, nonce, secondsSinceEpoch);

View File

@@ -9,7 +9,8 @@ public class LoginCommandRunner(IVelopackFlowServiceClient Client) : ICommand<Lo
public async Task Run(LoginOptions options) public async Task Run(LoginOptions options)
{ {
await Client.LoginAsync(new() { await Client.LoginAsync(new() {
VelopackBaseUrl = options.VelopackBaseUrl VelopackBaseUrl = options.VelopackBaseUrl,
ApiKey = options.ApiKey,
}); });
} }
} }

View File

@@ -148,7 +148,8 @@ public class Program
private static void SetupVelopackService(IServiceCollection services) private static void SetupVelopackService(IServiceCollection services)
{ {
services.AddSingleton<IVelopackFlowServiceClient, VelopackFlowServiceClient>(); services.AddSingleton<IVelopackFlowServiceClient, VelopackFlowServiceClient>();
services.AddHttpClient(); services.AddSingleton<HmacAuthHttpClientHandler>();
services.AddHttpClient().ConfigureHttpClientDefaults(x => x.AddHttpMessageHandler<HmacAuthHttpClientHandler>());
} }
} }