Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 31 additions & 20 deletions EstateReportingAPI/Bootstrapper/MiddlewareRegistry.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
using Microsoft.OpenApi;
using OpenIddict.Client;
using Shared.Middleware;

namespace EstateReportingAPI.Bootstrapper{
using System.Diagnostics.CodeAnalysis;
using System.Net.Security;
using System.Reflection;
using Common;
using Lamar;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using OpenIddict.Validation.AspNetCore;
using Shared.General;
using Swashbuckle.AspNetCore.Filters;
using System.Diagnostics.CodeAnalysis;
using System.Net.Security;
using System.Reflection;

[ExcludeFromCodeCoverage]
public class MiddlewareRegistry : ServiceRegistry{
Expand Down Expand Up @@ -81,24 +81,35 @@ private void ConfigureSwagger(){
private void ConfigureAuthentication(){
String? inTestMode = Environment.GetEnvironmentVariable("InTestMode");
if (String.Compare(inTestMode, Boolean.TrueString, StringComparison.InvariantCultureIgnoreCase) != 0){
this.AddAuthentication(options =>
{
options.DefaultScheme = OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme;
});

this.AddOpenIddict()
// Register the OpenIddict client components.
.AddClient(options => {
// Allow grant_type=client_credentials to be negotiated.
options.AllowClientCredentialsFlow();

// Disable token storage, which is not necessary for non-interactive flows like
// grant_type=password, grant_type=client_credentials or grant_type=refresh_token.
options.DisableTokenStorage();

// Register the System.Net.Http integration and use the identity of the current
// assembly as a more specific user agent, which can be useful when dealing with
// providers that use the user agent as a way to throttle requests (e.g Reddit).
options.UseSystemNetHttp().SetProductInformation(typeof(Program).Assembly);

// Add a client registration matching the client application definition in the server project.
options.AddRegistration(new OpenIddictClientRegistration { Issuer = new Uri(ConfigurationReader.GetValue("SecurityConfiguration", "Authority"), UriKind.Absolute), ClientId = ConfigurationReader.GetValue("SecurityConfiguration", "ApiName") });
.AddValidation(options =>
{
// Same as your Authority
options.SetIssuer(new Uri(ConfigurationReader.GetValue("SecurityConfiguration", "Authority")));

// Enables discovery and HTTP backchannel support
options.UseSystemNetHttp()
.ConfigureHttpClientHandler(handler =>
{
// DEV ONLY: bypass all certificate errors
handler.ServerCertificateCustomValidationCallback =
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
});

// Register the ASP.NET Core integration
options.UseAspNetCore();

// Optionally set expected audience(s):
options.AddAudiences(ConfigurationReader.GetValue("SecurityConfiguration", "ApiName"));

});

this.AddAuthorization();
}
}

Expand Down
3 changes: 2 additions & 1 deletion EstateReportingAPI/EstateReportingAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OpenIddict.Client.SystemNetHttp" Version="7.4.0" />
<PackageReference Include="OpenIddict.Validation.AspNetCore" Version="7.4.0" />
<PackageReference Include="OpenIddict.Validation.SystemNetHttp" Version="7.4.0" />
<PackageReference Include="Lamar" Version="15.0.1" />
<PackageReference Include="Lamar.Microsoft.DependencyInjection" Version="15.0.1" />
<PackageReference Include="NLog.Extensions.Logging" Version="6.1.2" />
Expand Down
Loading