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:
@@ -4,6 +4,9 @@ using ReC.Application.Common;
|
|||||||
using ReC.Application.Common.Dto;
|
using ReC.Application.Common.Dto;
|
||||||
using ReC.Application.Common.Exceptions;
|
using ReC.Application.Common.Exceptions;
|
||||||
using ReC.Application.OutResults.Commands;
|
using ReC.Application.OutResults.Commands;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Text;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace ReC.Application.RecActions.Commands;
|
namespace ReC.Application.RecActions.Commands;
|
||||||
@@ -27,7 +30,17 @@ public class InvokeRecActionCommandHandler(
|
|||||||
public async Task<bool> Handle(InvokeRecActionCommand request, CancellationToken cancel)
|
public async Task<bool> Handle(InvokeRecActionCommand request, CancellationToken cancel)
|
||||||
{
|
{
|
||||||
var action = request.Action;
|
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)
|
if (action.RestType is null)
|
||||||
throw new DataIntegrityException(
|
throw new DataIntegrityException(
|
||||||
@@ -54,23 +67,51 @@ public class InvokeRecActionCommandHandler(
|
|||||||
{
|
{
|
||||||
case "No Auth":
|
case "No Auth":
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "API Key":
|
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;
|
break;
|
||||||
|
|
||||||
case "Bearer Token":
|
case "Bearer Token":
|
||||||
break;
|
|
||||||
case "JWT Bearer":
|
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;
|
break;
|
||||||
|
|
||||||
case "Basic Auth":
|
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;
|
break;
|
||||||
case "Digest Auth":
|
|
||||||
break;
|
|
||||||
case "OAuth 1.0":
|
|
||||||
break;
|
|
||||||
case "OAuth 2.0":
|
|
||||||
break;
|
|
||||||
case "AWS Signature":
|
|
||||||
break;
|
|
||||||
case "NTLM Auth":
|
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;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user