Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
More code sharing and clean up WebHooks client
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfowl authored and ReubenBond committed May 9, 2023
1 parent e4b94de commit df4eb4c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 181 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
namespace WebMVC.Infrastructure;
using System.Net.Http.Headers;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;

namespace Services.Common;

public class HttpClientAuthorizationDelegatingHandler
: DelegatingHandler
Expand All @@ -12,11 +16,14 @@ public HttpClientAuthorizationDelegatingHandler(IHttpContextAccessor httpContext

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var accessToken = await _httpContextAccessor.HttpContext.GetTokenAsync("access_token");

if (accessToken is not null)
if (_httpContextAccessor.HttpContext is HttpContext context)
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var accessToken = await context.GetTokenAsync("access_token");

if (accessToken is not null)
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
}
}

return await base.SendAsync(request, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public WebhooksReceivedController(IOptions<WebhookClientOptions> options, ILogge
[HttpPost]
public async Task<IActionResult> NewWebhook(WebhookData hook)
{
var header = Request.Headers[HeaderNames.WebHookCheckHeader];
var token = header.FirstOrDefault();
string token = Request.Headers[HeaderNames.WebHookCheckHeader];

_logger.LogInformation("Received hook with token {Token}. My token is {MyToken}. Token validation is set to {ValidateToken}", token, _options.Token, _options.ValidateToken);

Expand Down
34 changes: 16 additions & 18 deletions src/Web/WebhookClient/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
global using Microsoft.AspNetCore.Authentication;
global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Net;
global using System.Net.Http;
global using System.Text.Json;
global using System.Threading.Tasks;
global using Microsoft.AspNetCore.Authentication;
global using Microsoft.AspNetCore.Authentication.Cookies;
global using Microsoft.AspNetCore.Authentication.OpenIdConnect;
global using Microsoft.AspNetCore.Authorization;
global using Microsoft.AspNetCore.Builder;
global using Microsoft.AspNetCore.Hosting;
global using Microsoft.AspNetCore.Http;
global using Microsoft.AspNetCore.Mvc;
global using System.Threading.Tasks;
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Hosting;
global using Microsoft.Extensions.Logging;
global using Microsoft.Extensions.Options;
global using System.Linq;
global using WebhookClient.Models;
global using WebhookClient.Services;
global using System;
global using System.Collections.Generic;
global using System.Net.Http;
global using System.Text.Json;
global using Microsoft.AspNetCore.Http;
global using System.Net.Http.Headers;
global using System.Threading;
global using Services.Common;
global using Microsoft.AspNetCore.Hosting;
global using WebhookClient;
global using Microsoft.AspNetCore.Builder;
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Hosting;
global using System.Net;
global using WebhookClient.Models;
global using WebhookClient.Services;
30 changes: 0 additions & 30 deletions src/Web/WebhookClient/HttpClientAuthorizationDelegatingHandler.cs

This file was deleted.

52 changes: 10 additions & 42 deletions src/Web/WebhookClient/Program.cs
Original file line number Diff line number Diff line change
@@ -1,57 +1,25 @@
var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();
builder.Services.AddSession(opt =>
{
opt.Cookie.Name = ".eShopWebhooks.Session";
})
.Configure<WebhookClientOptions>(builder.Configuration)
.AddHttpClientServices(builder.Configuration)
.AddCustomAuthentication(builder.Configuration)
.AddTransient<IWebhooksClient, WebhooksClient>()
.AddSingleton<IHooksRepository, InMemoryHooksRepository>()
.AddMvc();

builder.Services.Configure<WebhookClientOptions>(builder.Configuration);
builder.Services.AddHttpClientServices(builder.Configuration);
builder.Services.AddCustomAuthentication(builder.Configuration);
builder.Services.AddTransient<IWebhooksClient, WebhooksClient>();
builder.Services.AddSingleton<IHooksRepository, InMemoryHooksRepository>();

builder.Services.AddRazorPages();
builder.Services.AddControllers();

var app = builder.Build();
app.UseServiceDefaults();

app.Map("/check", capp =>

This comment has been minimized.

Copy link
@RobertoBorges

RobertoBorges Jun 15, 2023

Why you removed this ?????

{
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
// Refer to https://github.com/dotnet-architecture/eShopOnContainers/issues/1391
app.UseCookiePolicy(new CookiePolicyOptions { MinimumSameSitePolicy = SameSiteMode.Lax });

app.UseStaticFiles();
app.UseSession();

app.UseAuthentication();
app.UseAuthorization();
app.MapDefaultControllerRoute();
Expand Down

0 comments on commit df4eb4c

Please sign in to comment.