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

Commit df4eb4c

Browse files
davidfowlReubenBond
authored andcommitted
More code sharing and clean up WebHooks client
1 parent e4b94de commit df4eb4c

File tree

7 files changed

+39
-181
lines changed

7 files changed

+39
-181
lines changed

src/ApiGateways/Mobile.Bff.Shopping/aggregator/Infrastructure/HttpClientAuthorizationDelegatingHandler.cs

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/ApiGateways/Web.Bff.Shopping/aggregator/Infrastructure/HttpClientAuthorizationDelegatingHandler.cs

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/Web/WebMVC/Infrastructure/HttpClientAuthorizationDelegatingHandler.cs renamed to src/Services/Services.Common/HttpClientAuthorizationDelegatingHandler.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
namespace WebMVC.Infrastructure;
1+
using System.Net.Http.Headers;
2+
using Microsoft.AspNetCore.Authentication;
3+
using Microsoft.AspNetCore.Http;
4+
5+
namespace Services.Common;
26

37
public class HttpClientAuthorizationDelegatingHandler
48
: DelegatingHandler
@@ -12,11 +16,14 @@ public HttpClientAuthorizationDelegatingHandler(IHttpContextAccessor httpContext
1216

1317
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
1418
{
15-
var accessToken = await _httpContextAccessor.HttpContext.GetTokenAsync("access_token");
16-
17-
if (accessToken is not null)
19+
if (_httpContextAccessor.HttpContext is HttpContext context)
1820
{
19-
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
21+
var accessToken = await context.GetTokenAsync("access_token");
22+
23+
if (accessToken is not null)
24+
{
25+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
26+
}
2027
}
2128

2229
return await base.SendAsync(request, cancellationToken);

src/Web/WebhookClient/Controllers/WebhooksReceivedController.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ public WebhooksReceivedController(IOptions<WebhookClientOptions> options, ILogge
1818
[HttpPost]
1919
public async Task<IActionResult> NewWebhook(WebhookData hook)
2020
{
21-
var header = Request.Headers[HeaderNames.WebHookCheckHeader];
22-
var token = header.FirstOrDefault();
21+
string token = Request.Headers[HeaderNames.WebHookCheckHeader];
2322

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

src/Web/WebhookClient/GlobalUsings.cs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
global using Microsoft.AspNetCore.Authentication;
1+
global using System;
2+
global using System.Collections.Generic;
3+
global using System.Linq;
4+
global using System.Net;
5+
global using System.Net.Http;
6+
global using System.Text.Json;
7+
global using System.Threading.Tasks;
8+
global using Microsoft.AspNetCore.Authentication;
29
global using Microsoft.AspNetCore.Authentication.Cookies;
310
global using Microsoft.AspNetCore.Authentication.OpenIdConnect;
411
global using Microsoft.AspNetCore.Authorization;
12+
global using Microsoft.AspNetCore.Builder;
13+
global using Microsoft.AspNetCore.Hosting;
14+
global using Microsoft.AspNetCore.Http;
515
global using Microsoft.AspNetCore.Mvc;
6-
global using System.Threading.Tasks;
16+
global using Microsoft.Extensions.Configuration;
17+
global using Microsoft.Extensions.DependencyInjection;
18+
global using Microsoft.Extensions.Hosting;
719
global using Microsoft.Extensions.Logging;
820
global using Microsoft.Extensions.Options;
9-
global using System.Linq;
10-
global using WebhookClient.Models;
11-
global using WebhookClient.Services;
12-
global using System;
13-
global using System.Collections.Generic;
14-
global using System.Net.Http;
15-
global using System.Text.Json;
16-
global using Microsoft.AspNetCore.Http;
17-
global using System.Net.Http.Headers;
18-
global using System.Threading;
1921
global using Services.Common;
20-
global using Microsoft.AspNetCore.Hosting;
2122
global using WebhookClient;
22-
global using Microsoft.AspNetCore.Builder;
23-
global using Microsoft.Extensions.Configuration;
24-
global using Microsoft.Extensions.DependencyInjection;
25-
global using Microsoft.Extensions.Hosting;
26-
global using System.Net;
23+
global using WebhookClient.Models;
24+
global using WebhookClient.Services;

src/Web/WebhookClient/HttpClientAuthorizationDelegatingHandler.cs

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/Web/WebhookClient/Program.cs

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,25 @@
11
var builder = WebApplication.CreateBuilder(args);
22

33
builder.AddServiceDefaults();
4-
builder.Services.AddSession(opt =>
5-
{
6-
opt.Cookie.Name = ".eShopWebhooks.Session";
7-
})
8-
.Configure<WebhookClientOptions>(builder.Configuration)
9-
.AddHttpClientServices(builder.Configuration)
10-
.AddCustomAuthentication(builder.Configuration)
11-
.AddTransient<IWebhooksClient, WebhooksClient>()
12-
.AddSingleton<IHooksRepository, InMemoryHooksRepository>()
13-
.AddMvc();
4+
5+
builder.Services.Configure<WebhookClientOptions>(builder.Configuration);
6+
builder.Services.AddHttpClientServices(builder.Configuration);
7+
builder.Services.AddCustomAuthentication(builder.Configuration);
8+
builder.Services.AddTransient<IWebhooksClient, WebhooksClient>();
9+
builder.Services.AddSingleton<IHooksRepository, InMemoryHooksRepository>();
10+
11+
builder.Services.AddRazorPages();
1412
builder.Services.AddControllers();
13+
1514
var app = builder.Build();
1615
app.UseServiceDefaults();
1716

18-
app.Map("/check", capp =>
19-
{
20-
capp.Run(async (context) =>
21-
{
22-
if ("OPTIONS".Equals(context.Request.Method, StringComparison.InvariantCultureIgnoreCase))
23-
{
24-
var validateToken = bool.TrueString.Equals(builder.Configuration["ValidateToken"], StringComparison.InvariantCultureIgnoreCase);
25-
var header = context.Request.Headers[HeaderNames.WebHookCheckHeader];
26-
var value = header.FirstOrDefault();
27-
var tokenToValidate = builder.Configuration["Token"];
28-
if (!validateToken || value == tokenToValidate)
29-
{
30-
if (!string.IsNullOrWhiteSpace(tokenToValidate))
31-
{
32-
context.Response.Headers.Add(HeaderNames.WebHookCheckHeader, tokenToValidate);
33-
}
34-
context.Response.StatusCode = (int)HttpStatusCode.OK;
35-
}
36-
else
37-
{
38-
await context.Response.WriteAsync("Invalid token");
39-
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
40-
}
41-
}
42-
else
43-
{
44-
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
45-
}
46-
});
47-
});
48-
4917
// Fix samesite issue when running eShop from docker-compose locally as by default http protocol is being used
5018
// Refer to https://github.com/dotnet-architecture/eShopOnContainers/issues/1391
5119
app.UseCookiePolicy(new CookiePolicyOptions { MinimumSameSitePolicy = SameSiteMode.Lax });
5220

5321
app.UseStaticFiles();
54-
app.UseSession();
22+
5523
app.UseAuthentication();
5624
app.UseAuthorization();
5725
app.MapDefaultControllerRoute();

0 commit comments

Comments
 (0)