Updates webhook client project to .NET 6.0 (#1777)
* Included globalusing file for webhookclient * Included file scope namespaces for Webhookclient * Updated packages in WebHookClient project
This commit is contained in:
parent
f5c1af1535
commit
0823cb977d
@ -1,37 +1,28 @@
|
|||||||
using Microsoft.AspNetCore.Authentication;
|
namespace WebhookClient.Controllers;
|
||||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
||||||
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
|
|
||||||
using Microsoft.AspNetCore.Authorization;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using System.Security.Claims;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace WebhookClient.Controllers
|
[Authorize]
|
||||||
|
public class AccountController : Controller
|
||||||
{
|
{
|
||||||
[Authorize]
|
public async Task<IActionResult> SignIn(string returnUrl)
|
||||||
public class AccountController : Controller
|
|
||||||
{
|
{
|
||||||
public async Task<IActionResult> SignIn(string returnUrl)
|
var user = User as ClaimsPrincipal;
|
||||||
|
|
||||||
|
var token = await HttpContext.GetTokenAsync("access_token");
|
||||||
|
|
||||||
|
if (token != null)
|
||||||
{
|
{
|
||||||
var user = User as ClaimsPrincipal;
|
ViewData["access_token"] = token;
|
||||||
|
|
||||||
var token = await HttpContext.GetTokenAsync("access_token");
|
|
||||||
|
|
||||||
if (token != null)
|
|
||||||
{
|
|
||||||
ViewData["access_token"] = token;
|
|
||||||
}
|
|
||||||
|
|
||||||
return RedirectToPage("/Index");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IActionResult> Signout()
|
return RedirectToPage("/Index");
|
||||||
{
|
}
|
||||||
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
||||||
await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
|
public async Task<IActionResult> Signout()
|
||||||
var homeUrl = Url.Page("/Index");
|
{
|
||||||
return new SignOutResult(OpenIdConnectDefaults.AuthenticationScheme,
|
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
||||||
new AuthenticationProperties { RedirectUri = homeUrl });
|
await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
|
||||||
}
|
var homeUrl = Url.Page("/Index");
|
||||||
|
return new SignOutResult(OpenIdConnectDefaults.AuthenticationScheme,
|
||||||
|
new AuthenticationProperties { RedirectUri = homeUrl });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,53 +1,44 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
namespace WebhookClient.Controllers;
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using WebhookClient.Models;
|
|
||||||
using WebhookClient.Services;
|
|
||||||
|
|
||||||
namespace WebhookClient.Controllers
|
[ApiController]
|
||||||
|
[Route("webhook-received")]
|
||||||
|
public class WebhooksReceivedController : Controller
|
||||||
{
|
{
|
||||||
[ApiController]
|
|
||||||
[Route("webhook-received")]
|
private readonly Settings _settings;
|
||||||
public class WebhooksReceivedController : Controller
|
private readonly ILogger _logger;
|
||||||
|
private readonly IHooksRepository _hooksRepository;
|
||||||
|
|
||||||
|
public WebhooksReceivedController(IOptions<Settings> settings, ILogger<WebhooksReceivedController> logger, IHooksRepository hooksRepository)
|
||||||
{
|
{
|
||||||
|
_settings = settings.Value;
|
||||||
|
_logger = logger;
|
||||||
|
_hooksRepository = hooksRepository;
|
||||||
|
}
|
||||||
|
|
||||||
private readonly Settings _settings;
|
[HttpPost]
|
||||||
private readonly ILogger _logger;
|
public async Task<IActionResult> NewWebhook(WebhookData hook)
|
||||||
private readonly IHooksRepository _hooksRepository;
|
{
|
||||||
|
var header = Request.Headers[HeaderNames.WebHookCheckHeader];
|
||||||
|
var token = header.FirstOrDefault();
|
||||||
|
|
||||||
public WebhooksReceivedController(IOptions<Settings> settings, ILogger<WebhooksReceivedController> logger, IHooksRepository hooksRepository)
|
_logger.LogInformation("Received hook with token {Token}. My token is {MyToken}. Token validation is set to {ValidateToken}", token, _settings.Token, _settings.ValidateToken);
|
||||||
|
|
||||||
|
if (!_settings.ValidateToken || _settings.Token == token)
|
||||||
{
|
{
|
||||||
_settings = settings.Value;
|
_logger.LogInformation("Received hook is going to be processed");
|
||||||
_logger = logger;
|
var newHook = new WebHookReceived()
|
||||||
_hooksRepository = hooksRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpPost]
|
|
||||||
public async Task<IActionResult> NewWebhook(WebhookData hook)
|
|
||||||
{
|
|
||||||
var header = Request.Headers[HeaderNames.WebHookCheckHeader];
|
|
||||||
var token = header.FirstOrDefault();
|
|
||||||
|
|
||||||
_logger.LogInformation("Received hook with token {Token}. My token is {MyToken}. Token validation is set to {ValidateToken}", token, _settings.Token, _settings.ValidateToken);
|
|
||||||
|
|
||||||
if (!_settings.ValidateToken || _settings.Token == token)
|
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Received hook is going to be processed");
|
Data = hook.Payload,
|
||||||
var newHook = new WebHookReceived()
|
When = hook.When,
|
||||||
{
|
Token = token
|
||||||
Data = hook.Payload,
|
};
|
||||||
When = hook.When,
|
await _hooksRepository.AddNew(newHook);
|
||||||
Token = token
|
_logger.LogInformation("Received hook was processed.");
|
||||||
};
|
return Ok(newHook);
|
||||||
await _hooksRepository.AddNew(newHook);
|
|
||||||
_logger.LogInformation("Received hook was processed.");
|
|
||||||
return Ok(newHook);
|
|
||||||
}
|
|
||||||
|
|
||||||
_logger.LogInformation("Received hook is NOT processed - Bad Request returned.");
|
|
||||||
return BadRequest();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_logger.LogInformation("Received hook is NOT processed - Bad Request returned.");
|
||||||
|
return BadRequest();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
27
src/Web/WebhookClient/GlobalUsings.cs
Normal file
27
src/Web/WebhookClient/GlobalUsings.cs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
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.Mvc;
|
||||||
|
global using System.Security.Claims;
|
||||||
|
global using System.Threading.Tasks;
|
||||||
|
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 Microsoft.AspNetCore;
|
||||||
|
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;
|
@ -1,7 +1,6 @@
|
|||||||
namespace WebhookClient
|
namespace WebhookClient;
|
||||||
|
|
||||||
|
static class HeaderNames
|
||||||
{
|
{
|
||||||
static class HeaderNames
|
public const string WebHookCheckHeader = "X-eshop-whtoken";
|
||||||
{
|
|
||||||
public const string WebHookCheckHeader = "X-eshop-whtoken";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,49 +1,40 @@
|
|||||||
using Microsoft.AspNetCore.Authentication;
|
namespace WebhookClient;
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Net.Http;
|
|
||||||
using System.Net.Http.Headers;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace WebhookClient
|
public class HttpClientAuthorizationDelegatingHandler
|
||||||
|
: DelegatingHandler
|
||||||
{
|
{
|
||||||
public class HttpClientAuthorizationDelegatingHandler
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||||
: DelegatingHandler
|
|
||||||
|
public HttpClientAuthorizationDelegatingHandler(IHttpContextAccessor httpContextAccessor)
|
||||||
{
|
{
|
||||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
_httpContextAccessor = httpContextAccessor;
|
||||||
|
}
|
||||||
|
|
||||||
public HttpClientAuthorizationDelegatingHandler(IHttpContextAccessor httpContextAccessor)
|
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var authorizationHeader = _httpContextAccessor.HttpContext
|
||||||
|
.Request.Headers["Authorization"];
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(authorizationHeader))
|
||||||
{
|
{
|
||||||
_httpContextAccessor = httpContextAccessor;
|
request.Headers.Add("Authorization", new List<string>() { authorizationHeader });
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
var token = await GetToken();
|
||||||
|
|
||||||
|
if (token != null)
|
||||||
{
|
{
|
||||||
var authorizationHeader = _httpContextAccessor.HttpContext
|
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
||||||
.Request.Headers["Authorization"];
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(authorizationHeader))
|
|
||||||
{
|
|
||||||
request.Headers.Add("Authorization", new List<string>() { authorizationHeader });
|
|
||||||
}
|
|
||||||
|
|
||||||
var token = await GetToken();
|
|
||||||
|
|
||||||
if (token != null)
|
|
||||||
{
|
|
||||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
|
||||||
}
|
|
||||||
|
|
||||||
return await base.SendAsync(request, cancellationToken);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async Task<string> GetToken()
|
return await base.SendAsync(request, cancellationToken);
|
||||||
{
|
}
|
||||||
const string ACCESS_TOKEN = "access_token";
|
|
||||||
|
|
||||||
return await _httpContextAccessor.HttpContext
|
async Task<string> GetToken()
|
||||||
.GetTokenAsync(ACCESS_TOKEN);
|
{
|
||||||
}
|
const string ACCESS_TOKEN = "access_token";
|
||||||
|
|
||||||
|
return await _httpContextAccessor.HttpContext
|
||||||
|
.GetTokenAsync(ACCESS_TOKEN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,10 @@
|
|||||||
using System;
|
namespace WebhookClient.Models;
|
||||||
|
|
||||||
namespace WebhookClient.Models
|
public class WebHookReceived
|
||||||
{
|
{
|
||||||
public class WebHookReceived
|
public DateTime When { get; set; }
|
||||||
{
|
|
||||||
public DateTime When { get; set; }
|
|
||||||
|
|
||||||
public string Data { get; set; }
|
public string Data { get; set; }
|
||||||
|
|
||||||
public string Token { get; set; }
|
public string Token { get; set; }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,10 @@
|
|||||||
using System;
|
namespace WebhookClient.Models;
|
||||||
|
|
||||||
namespace WebhookClient.Models
|
public class WebhookData
|
||||||
{
|
{
|
||||||
public class WebhookData
|
public DateTime When { get; set; }
|
||||||
{
|
|
||||||
public DateTime When { get; set; }
|
|
||||||
|
|
||||||
public string Payload { get; set; }
|
public string Payload { get; set; }
|
||||||
|
|
||||||
public string Type { get; set; }
|
public string Type { get; set; }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,8 @@
|
|||||||
using System;
|
namespace WebhookClient.Models;
|
||||||
|
|
||||||
namespace WebhookClient.Models
|
public class WebhookResponse
|
||||||
{
|
{
|
||||||
public class WebhookResponse
|
public DateTime Date { get; set; }
|
||||||
{
|
public string DestUrl { get; set; }
|
||||||
public DateTime Date { get; set; }
|
public string Token { get; set; }
|
||||||
public string DestUrl { get; set; }
|
|
||||||
public string Token { get; set; }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
namespace WebhookClient.Models
|
namespace WebhookClient.Models;
|
||||||
|
|
||||||
|
public class WebhookSubscriptionRequest
|
||||||
{
|
{
|
||||||
public class WebhookSubscriptionRequest
|
public string Url { get; set; }
|
||||||
{
|
public string Token { get; set; }
|
||||||
public string Url { get; set; }
|
public string Event { get; set; }
|
||||||
public string Token { get; set; }
|
public string GrantUrl { get; set; }
|
||||||
public string Event { get; set; }
|
|
||||||
public string GrantUrl { get; set; }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,4 @@
|
|||||||
using Microsoft.AspNetCore;
|
CreateWebHostBuilder(args).Build().Run();
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using WebhookClient;
|
|
||||||
|
|
||||||
CreateWebHostBuilder(args).Build().Run();
|
|
||||||
|
|
||||||
|
|
||||||
IWebHostBuilder CreateWebHostBuilder(string[] args) =>
|
IWebHostBuilder CreateWebHostBuilder(string[] args) =>
|
||||||
|
@ -1,12 +1,7 @@
|
|||||||
using System.Collections.Generic;
|
namespace WebhookClient.Services;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using WebhookClient.Models;
|
|
||||||
|
|
||||||
namespace WebhookClient.Services
|
public interface IHooksRepository
|
||||||
{
|
{
|
||||||
public interface IHooksRepository
|
Task<IEnumerable<WebHookReceived>> GetAll();
|
||||||
{
|
Task AddNew(WebHookReceived hook);
|
||||||
Task<IEnumerable<WebHookReceived>> GetAll();
|
|
||||||
Task AddNew(WebHookReceived hook);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
namespace WebhookClient.Services;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using WebhookClient.Models;
|
|
||||||
|
|
||||||
namespace WebhookClient.Services
|
public interface IWebhooksClient
|
||||||
{
|
{
|
||||||
public interface IWebhooksClient
|
Task<IEnumerable<WebhookResponse>> LoadWebhooks();
|
||||||
{
|
|
||||||
Task<IEnumerable<WebhookResponse>> LoadWebhooks();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,25 +1,19 @@
|
|||||||
using System.Collections.Generic;
|
namespace WebhookClient.Services;
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using WebhookClient.Models;
|
|
||||||
|
|
||||||
namespace WebhookClient.Services
|
public class InMemoryHooksRepository : IHooksRepository
|
||||||
{
|
{
|
||||||
public class InMemoryHooksRepository : IHooksRepository
|
private readonly List<WebHookReceived> _data;
|
||||||
|
|
||||||
|
public InMemoryHooksRepository() => _data = new List<WebHookReceived>();
|
||||||
|
|
||||||
|
public Task AddNew(WebHookReceived hook)
|
||||||
{
|
{
|
||||||
private readonly List<WebHookReceived> _data;
|
_data.Add(hook);
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
public InMemoryHooksRepository() => _data = new List<WebHookReceived>();
|
public Task<IEnumerable<WebHookReceived>> GetAll()
|
||||||
|
{
|
||||||
public Task AddNew(WebHookReceived hook)
|
return Task.FromResult(_data.AsEnumerable());
|
||||||
{
|
|
||||||
_data.Add(hook);
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task<IEnumerable<WebHookReceived>> GetAll()
|
|
||||||
{
|
|
||||||
return Task.FromResult(_data.AsEnumerable());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,32 +1,24 @@
|
|||||||
using Microsoft.Extensions.Options;
|
namespace WebhookClient.Services;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Net.Http;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using WebhookClient.Models;
|
|
||||||
using System.Text.Json;
|
|
||||||
|
|
||||||
namespace WebhookClient.Services
|
public class WebhooksClient : IWebhooksClient
|
||||||
{
|
{
|
||||||
public class WebhooksClient : IWebhooksClient
|
|
||||||
{
|
|
||||||
|
|
||||||
private readonly IHttpClientFactory _httpClientFactory;
|
private readonly IHttpClientFactory _httpClientFactory;
|
||||||
private readonly Settings _settings;
|
private readonly Settings _settings;
|
||||||
public WebhooksClient(IHttpClientFactory httpClientFactory, IOptions<Settings> settings)
|
public WebhooksClient(IHttpClientFactory httpClientFactory, IOptions<Settings> settings)
|
||||||
|
{
|
||||||
|
_httpClientFactory = httpClientFactory;
|
||||||
|
_settings = settings.Value;
|
||||||
|
}
|
||||||
|
public async Task<IEnumerable<WebhookResponse>> LoadWebhooks()
|
||||||
|
{
|
||||||
|
var client = _httpClientFactory.CreateClient("GrantClient");
|
||||||
|
var response = await client.GetAsync(_settings.WebhooksUrl + "/api/v1/webhooks");
|
||||||
|
var json = await response.Content.ReadAsStringAsync();
|
||||||
|
var subscriptions = JsonSerializer.Deserialize<IEnumerable<WebhookResponse>>(json, new JsonSerializerOptions
|
||||||
{
|
{
|
||||||
_httpClientFactory = httpClientFactory;
|
PropertyNameCaseInsensitive = true
|
||||||
_settings = settings.Value;
|
});
|
||||||
}
|
return subscriptions;
|
||||||
public async Task<IEnumerable<WebhookResponse>> LoadWebhooks()
|
|
||||||
{
|
|
||||||
var client = _httpClientFactory.CreateClient("GrantClient");
|
|
||||||
var response = await client.GetAsync(_settings.WebhooksUrl + "/api/v1/webhooks");
|
|
||||||
var json = await response.Content.ReadAsStringAsync();
|
|
||||||
var subscriptions = JsonSerializer.Deserialize<IEnumerable<WebhookResponse>>(json, new JsonSerializerOptions
|
|
||||||
{
|
|
||||||
PropertyNameCaseInsensitive = true
|
|
||||||
});
|
|
||||||
return subscriptions;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,13 @@
|
|||||||
namespace WebhookClient
|
namespace WebhookClient;
|
||||||
|
|
||||||
|
public class Settings
|
||||||
{
|
{
|
||||||
public class Settings
|
public string Token { get; set; }
|
||||||
{
|
public string IdentityUrl { get; set; }
|
||||||
public string Token { get; set; }
|
public string CallBackUrl { get; set; }
|
||||||
public string IdentityUrl { get; set; }
|
public string WebhooksUrl { get; set; }
|
||||||
public string CallBackUrl { get; set; }
|
public string SelfUrl { get; set; }
|
||||||
public string WebhooksUrl { get; set; }
|
|
||||||
public string SelfUrl { get; set; }
|
|
||||||
|
|
||||||
public bool ValidateToken { get; set; }
|
public bool ValidateToken { get; set; }
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,165 +1,149 @@
|
|||||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
namespace WebhookClient;
|
||||||
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
|
|
||||||
using Microsoft.AspNetCore.Builder;
|
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using Microsoft.Extensions.Hosting;
|
|
||||||
using System;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net;
|
|
||||||
using System.Threading;
|
|
||||||
using WebhookClient.Services;
|
|
||||||
|
|
||||||
namespace WebhookClient
|
public class Startup
|
||||||
{
|
{
|
||||||
public class Startup
|
public Startup(IConfiguration configuration)
|
||||||
{
|
{
|
||||||
public Startup(IConfiguration configuration)
|
Configuration = configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IConfiguration Configuration { get; }
|
||||||
|
|
||||||
|
// This method gets called by the runtime. Use this method to add services to the container.
|
||||||
|
public void ConfigureServices(IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSession(opt =>
|
||||||
|
{
|
||||||
|
opt.Cookie.Name = ".eShopWebhooks.Session";
|
||||||
|
})
|
||||||
|
.AddConfiguration(Configuration)
|
||||||
|
.AddHttpClientServices(Configuration)
|
||||||
|
.AddCustomAuthentication(Configuration)
|
||||||
|
.AddTransient<IWebhooksClient, WebhooksClient>()
|
||||||
|
.AddSingleton<IHooksRepository, InMemoryHooksRepository>()
|
||||||
|
.AddMvc()
|
||||||
|
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
|
||||||
|
|
||||||
|
services.AddControllers();
|
||||||
|
}
|
||||||
|
|
||||||
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||||
|
{
|
||||||
|
|
||||||
|
var pathBase = Configuration["PATH_BASE"];
|
||||||
|
if (!string.IsNullOrEmpty(pathBase))
|
||||||
{
|
{
|
||||||
Configuration = configuration;
|
app.UsePathBase(pathBase);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IConfiguration Configuration { get; }
|
if (env.IsDevelopment())
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to add services to the container.
|
|
||||||
public void ConfigureServices(IServiceCollection services)
|
|
||||||
{
|
{
|
||||||
services.AddSession(opt =>
|
app.UseDeveloperExceptionPage();
|
||||||
{
|
|
||||||
opt.Cookie.Name = ".eShopWebhooks.Session";
|
|
||||||
})
|
|
||||||
.AddConfiguration(Configuration)
|
|
||||||
.AddHttpClientServices(Configuration)
|
|
||||||
.AddCustomAuthentication(Configuration)
|
|
||||||
.AddTransient<IWebhooksClient, WebhooksClient>()
|
|
||||||
.AddSingleton<IHooksRepository, InMemoryHooksRepository>()
|
|
||||||
.AddMvc()
|
|
||||||
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
|
|
||||||
|
|
||||||
services.AddControllers();
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
||||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
||||||
{
|
{
|
||||||
|
app.UseExceptionHandler("/Error");
|
||||||
var pathBase = Configuration["PATH_BASE"];
|
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
||||||
if (!string.IsNullOrEmpty(pathBase))
|
}
|
||||||
|
app.Map("/check", capp =>
|
||||||
|
{
|
||||||
|
capp.Run(async (context) =>
|
||||||
{
|
{
|
||||||
app.UsePathBase(pathBase);
|
if ("OPTIONS".Equals(context.Request.Method, StringComparison.InvariantCultureIgnoreCase))
|
||||||
}
|
|
||||||
|
|
||||||
if (env.IsDevelopment())
|
|
||||||
{
|
|
||||||
app.UseDeveloperExceptionPage();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
app.UseExceptionHandler("/Error");
|
|
||||||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
|
||||||
}
|
|
||||||
app.Map("/check", capp =>
|
|
||||||
{
|
|
||||||
capp.Run(async (context) =>
|
|
||||||
{
|
{
|
||||||
if ("OPTIONS".Equals(context.Request.Method, StringComparison.InvariantCultureIgnoreCase))
|
var validateToken = bool.TrueString.Equals(Configuration["ValidateToken"], StringComparison.InvariantCultureIgnoreCase);
|
||||||
|
var header = context.Request.Headers[HeaderNames.WebHookCheckHeader];
|
||||||
|
var value = header.FirstOrDefault();
|
||||||
|
var tokenToValidate = Configuration["Token"];
|
||||||
|
if (!validateToken || value == tokenToValidate)
|
||||||
{
|
{
|
||||||
var validateToken = bool.TrueString.Equals(Configuration["ValidateToken"], StringComparison.InvariantCultureIgnoreCase);
|
if (!string.IsNullOrWhiteSpace(tokenToValidate))
|
||||||
var header = context.Request.Headers[HeaderNames.WebHookCheckHeader];
|
|
||||||
var value = header.FirstOrDefault();
|
|
||||||
var tokenToValidate = Configuration["Token"];
|
|
||||||
if (!validateToken || value == tokenToValidate)
|
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrWhiteSpace(tokenToValidate))
|
context.Response.Headers.Add(HeaderNames.WebHookCheckHeader, 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;
|
|
||||||
}
|
}
|
||||||
|
context.Response.StatusCode = (int)HttpStatusCode.OK;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
await context.Response.WriteAsync("Invalid token");
|
||||||
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
|
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 });
|
||||||
|
|
||||||
app.UseStaticFiles();
|
app.UseStaticFiles();
|
||||||
app.UseSession();
|
app.UseSession();
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
app.UseEndpoints(endpoints =>
|
app.UseEndpoints(endpoints =>
|
||||||
{
|
|
||||||
endpoints.MapDefaultControllerRoute();
|
|
||||||
endpoints.MapRazorPages();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static class ServiceExtensions
|
|
||||||
{
|
|
||||||
public static IServiceCollection AddConfiguration(this IServiceCollection services, IConfiguration configuration)
|
|
||||||
{
|
{
|
||||||
services.AddOptions();
|
endpoints.MapDefaultControllerRoute();
|
||||||
services.Configure<Settings>(configuration);
|
endpoints.MapRazorPages();
|
||||||
return services;
|
});
|
||||||
}
|
}
|
||||||
public static IServiceCollection AddCustomAuthentication(this IServiceCollection services, IConfiguration configuration)
|
}
|
||||||
{
|
|
||||||
var identityUrl = configuration.GetValue<string>("IdentityUrl");
|
static class ServiceExtensions
|
||||||
var callBackUrl = configuration.GetValue<string>("CallBackUrl");
|
{
|
||||||
|
public static IServiceCollection AddConfiguration(this IServiceCollection services, IConfiguration configuration)
|
||||||
// Add Authentication services
|
{
|
||||||
|
services.AddOptions();
|
||||||
services.AddAuthentication(options =>
|
services.Configure<Settings>(configuration);
|
||||||
{
|
return services;
|
||||||
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
}
|
||||||
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
|
public static IServiceCollection AddCustomAuthentication(this IServiceCollection services, IConfiguration configuration)
|
||||||
})
|
{
|
||||||
.AddCookie(setup => setup.ExpireTimeSpan = TimeSpan.FromHours(2))
|
var identityUrl = configuration.GetValue<string>("IdentityUrl");
|
||||||
.AddOpenIdConnect(options =>
|
var callBackUrl = configuration.GetValue<string>("CallBackUrl");
|
||||||
{
|
|
||||||
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
// Add Authentication services
|
||||||
options.Authority = identityUrl.ToString();
|
|
||||||
options.SignedOutRedirectUri = callBackUrl.ToString();
|
services.AddAuthentication(options =>
|
||||||
options.ClientId = "webhooksclient";
|
{
|
||||||
options.ClientSecret = "secret";
|
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
||||||
options.ResponseType = "code id_token";
|
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
|
||||||
options.SaveTokens = true;
|
})
|
||||||
options.GetClaimsFromUserInfoEndpoint = true;
|
.AddCookie(setup => setup.ExpireTimeSpan = TimeSpan.FromHours(2))
|
||||||
options.RequireHttpsMetadata = false;
|
.AddOpenIdConnect(options =>
|
||||||
options.Scope.Add("openid");
|
{
|
||||||
options.Scope.Add("webhooks");
|
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
||||||
});
|
options.Authority = identityUrl.ToString();
|
||||||
|
options.SignedOutRedirectUri = callBackUrl.ToString();
|
||||||
return services;
|
options.ClientId = "webhooksclient";
|
||||||
}
|
options.ClientSecret = "secret";
|
||||||
|
options.ResponseType = "code id_token";
|
||||||
public static IServiceCollection AddHttpClientServices(this IServiceCollection services, IConfiguration configuration)
|
options.SaveTokens = true;
|
||||||
{
|
options.GetClaimsFromUserInfoEndpoint = true;
|
||||||
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
options.RequireHttpsMetadata = false;
|
||||||
services.AddTransient<HttpClientAuthorizationDelegatingHandler>();
|
options.Scope.Add("openid");
|
||||||
services.AddHttpClient("extendedhandlerlifetime").SetHandlerLifetime(Timeout.InfiniteTimeSpan);
|
options.Scope.Add("webhooks");
|
||||||
|
});
|
||||||
//add http client services
|
|
||||||
services.AddHttpClient("GrantClient")
|
return services;
|
||||||
.SetHandlerLifetime(TimeSpan.FromMinutes(5))
|
}
|
||||||
.AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>();
|
|
||||||
|
public static IServiceCollection AddHttpClientServices(this IServiceCollection services, IConfiguration configuration)
|
||||||
return services;
|
{
|
||||||
}
|
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
||||||
|
services.AddTransient<HttpClientAuthorizationDelegatingHandler>();
|
||||||
|
services.AddHttpClient("extendedhandlerlifetime").SetHandlerLifetime(Timeout.InfiniteTimeSpan);
|
||||||
|
|
||||||
|
//add http client services
|
||||||
|
services.AddHttpClient("GrantClient")
|
||||||
|
.SetHandlerLifetime(TimeSpan.FromMinutes(5))
|
||||||
|
.AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>();
|
||||||
|
|
||||||
|
return services;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,9 +13,9 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
|
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.2" />
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.0-preview.7.21378.6" />
|
||||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.8" />
|
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.8" />
|
||||||
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.1" />
|
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.0-preview.7.21413.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user