mirror of
https://github.com/velopack/velopack.git
synced 2025-10-25 15:19:22 +00:00
Registering HMAC handler on VPK CLI
This commit is contained in:
@@ -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);
|
||||||
@@ -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,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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>());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user