Add support for multiple HTTP auth methods in REST actions

Expanded InvokeRecActionCommandHandler to support API Key, Bearer/JWT/OAuth2, Basic, and NTLM authentication schemes. Added necessary imports and logic for header/query manipulation and credential handling. Left placeholders for Digest, OAuth 1.0, and AWS Signature. Improves flexibility and robustness of outgoing HTTP requests.
This commit is contained in:
2025-12-12 11:20:02 +01:00
parent 0583d07f26
commit 8c79b3a156

View File

@@ -4,6 +4,9 @@ using ReC.Application.Common;
using ReC.Application.Common.Dto;
using ReC.Application.Common.Exceptions;
using ReC.Application.OutResults.Commands;
using System.Net;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
namespace ReC.Application.RecActions.Commands;
@@ -27,7 +30,17 @@ public class InvokeRecActionCommandHandler(
public async Task<bool> Handle(InvokeRecActionCommand request, CancellationToken cancel)
{
var action = request.Action;
using var http = clientFactory.CreateClient();
var handler = new HttpClientHandler();
if (action.EndpointAuthType == "NTLM Auth" && !string.IsNullOrWhiteSpace(action.EndpointAuthUsername))
{
handler.Credentials = new NetworkCredential(
action.EndpointAuthUsername,
action.EndpointAuthPassword,
action.EndpointAuthDomain);
}
using var http = new HttpClient(handler);
if (action.RestType is null)
throw new DataIntegrityException(
@@ -54,23 +67,51 @@ public class InvokeRecActionCommandHandler(
{
case "No Auth":
break;
case "API Key":
if (action.EndpointAuthApiKey is string apiKey && action.EndpointAuthApiValue is string apiValue)
{
if (action.EndpointAuthApiKeyAddTo == "Header")
{
httpReq.Headers.Add(apiKey, apiValue);
}
else // Defaults to Query String
{
var uriBuilder = new UriBuilder(httpReq.RequestUri!);
var query = System.Web.HttpUtility.ParseQueryString(uriBuilder.Query);
query[apiKey] = apiValue;
uriBuilder.Query = query.ToString();
httpReq.RequestUri = uriBuilder.Uri;
}
}
break;
case "Bearer Token":
break;
case "JWT Bearer":
case "OAuth 2.0": // OAuth 2.0 uses Bearer tokens for authenticated requests
if (action.EndpointAuthToken is string authToken)
httpReq.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authToken);
break;
case "Basic Auth":
if (action.EndpointAuthUsername is string authUsername && action.EndpointAuthPassword is string authPassword)
{
var basicAuth = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{authUsername}:{authPassword}"));
httpReq.Headers.Authorization = new AuthenticationHeaderValue("Basic", basicAuth);
}
break;
case "Digest Auth":
break;
case "OAuth 1.0":
break;
case "OAuth 2.0":
break;
case "AWS Signature":
break;
case "NTLM Auth":
// NTLM authentication is configured on the HttpClientHandler before creating the HttpClient.
// No additional action is needed here.
break;
case "Digest Auth":
case "OAuth 1.0":
case "AWS Signature":
// These authentication methods require more complex implementations,
// often involving multi-step handshakes or specialized libraries.
// They are left as placeholders for future implementation.
break;
}