Merge 44a081b1b43eabb0333c16c2da7fdb511cc26d72 into 06d5164532902a076813894c965d6c99122d39f2
This commit is contained in:
commit
bbffc83793
@ -1,4 +1,10 @@
|
|||||||
namespace WebhookClient.Controllers;
|
using System;
|
||||||
|
using System.Security.Policy;
|
||||||
|
using System.Text;
|
||||||
|
using Azure;
|
||||||
|
using Microsoft.CodeAnalysis.VisualBasic.Syntax;
|
||||||
|
|
||||||
|
namespace WebhookClient.Controllers;
|
||||||
|
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("webhook-received")]
|
[Route("webhook-received")]
|
||||||
@ -8,11 +14,17 @@ public class WebhooksReceivedController : Controller
|
|||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly IHooksRepository _hooksRepository;
|
private readonly IHooksRepository _hooksRepository;
|
||||||
|
|
||||||
public WebhooksReceivedController(IOptions<WebhookClientOptions> options, ILogger<WebhooksReceivedController> logger, IHooksRepository hooksRepository)
|
private readonly IConfiguration _configuration;
|
||||||
|
|
||||||
|
public WebhooksReceivedController(IOptions<WebhookClientOptions> options,
|
||||||
|
ILogger<WebhooksReceivedController> logger,
|
||||||
|
IHooksRepository hooksRepository,
|
||||||
|
IConfiguration configuration)
|
||||||
{
|
{
|
||||||
_options = options.Value;
|
_options = options.Value;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_hooksRepository = hooksRepository;
|
_hooksRepository = hooksRepository;
|
||||||
|
_configuration = configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
@ -31,6 +43,9 @@ public class WebhooksReceivedController : Controller
|
|||||||
When = hook.When,
|
When = hook.When,
|
||||||
Token = token
|
Token = token
|
||||||
};
|
};
|
||||||
|
|
||||||
|
sendMessageToServiceBus(hook.Payload);
|
||||||
|
|
||||||
await _hooksRepository.AddNew(newHook);
|
await _hooksRepository.AddNew(newHook);
|
||||||
_logger.LogInformation("Received hook was processed.");
|
_logger.LogInformation("Received hook was processed.");
|
||||||
return Ok(newHook);
|
return Ok(newHook);
|
||||||
@ -39,4 +54,49 @@ public class WebhooksReceivedController : Controller
|
|||||||
_logger.LogInformation("Received hook is NOT processed - Bad Request returned.");
|
_logger.LogInformation("Received hook is NOT processed - Bad Request returned.");
|
||||||
return BadRequest();
|
return BadRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//function to send the message to Service Bus
|
||||||
|
public void sendMessageToServiceBus(string data)
|
||||||
|
{
|
||||||
|
//get the sas key from app config
|
||||||
|
var saskey = _configuration["ApimWAF:SASKey"];
|
||||||
|
var url = _configuration["ApimWAF:Url"];
|
||||||
|
var apimKey = _configuration["ApimWAF:apimKey"];
|
||||||
|
|
||||||
|
if (saskey == null || url == null || apimKey == null)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Skiping sending APIM Request - missing configuration");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
|
var handler = new HttpClientHandler();
|
||||||
|
|
||||||
|
//ignore the certificate errors
|
||||||
|
handler.ServerCertificateCustomValidationCallback =
|
||||||
|
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
|
||||||
|
|
||||||
|
System.Net.ServicePointManager.ServerCertificateValidationCallback +=
|
||||||
|
(s, cert, chain, sslPolicyErrors) => true;
|
||||||
|
|
||||||
|
using (var client = new HttpClient(handler))
|
||||||
|
{
|
||||||
|
var msg = new HttpRequestMessage(HttpMethod.Post, url);
|
||||||
|
msg.Headers.Add("Authorization", saskey);
|
||||||
|
msg.Headers.Add("Ocp-Apim-Subscription-Key", apimKey);
|
||||||
|
|
||||||
|
msg.Content = new StringContent(data, Encoding.UTF8, "application/json");
|
||||||
|
|
||||||
|
var response = client.Send(msg);
|
||||||
|
|
||||||
|
_logger.LogInformation("Sending the paid message to {Url} with \"{Data}\" result: \"{response}\"", url, data ?? string.Empty, response.Content.ReadAsStringAsync().GetAwaiter().GetResult());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Error sending APIM Request {}", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,37 @@ builder.Services.AddControllers();
|
|||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
app.UseServiceDefaults();
|
app.UseServiceDefaults();
|
||||||
|
|
||||||
|
app.Map("/check", capp =>
|
||||||
|
{
|
||||||
|
capp.Run(async (context) =>
|
||||||
|
{
|
||||||
|
if ("OPTIONS".Equals(context.Request.Method, StringComparison.InvariantCultureIgnoreCase))
|
||||||
|
{
|
||||||
|
var validateToken = bool.TrueString.Equals(builder.Configuration["ValidateToken"], StringComparison.InvariantCultureIgnoreCase);
|
||||||
|
var header = context.Request.Headers[HeaderNames.WebHookCheckHeader];
|
||||||
|
var value = header.FirstOrDefault();
|
||||||
|
var tokenToValidate = builder.Configuration["Token"];
|
||||||
|
if (!validateToken || value == tokenToValidate)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrWhiteSpace(tokenToValidate))
|
||||||
|
{
|
||||||
|
context.Response.Headers.Add(HeaderNames.WebHookCheckHeader, tokenToValidate);
|
||||||
|
}
|
||||||
|
context.Response.StatusCode = (int)HttpStatusCode.OK;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await context.Response.WriteAsync("Invalid token");
|
||||||
|
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// Fix samesite issue when running eShop from docker-compose locally as by default http protocol is being used
|
// Fix samesite issue when running eShop from docker-compose locally as by default http protocol is being used
|
||||||
// Refer to https://github.com/dotnet-architecture/eShopOnContainers/issues/1391
|
// Refer to https://github.com/dotnet-architecture/eShopOnContainers/issues/1391
|
||||||
app.UseCookiePolicy(new CookiePolicyOptions { MinimumSameSitePolicy = SameSiteMode.Lax });
|
app.UseCookiePolicy(new CookiePolicyOptions { MinimumSameSitePolicy = SameSiteMode.Lax });
|
||||||
|
Loading…
x
Reference in New Issue
Block a user