From 96f4921d950785d6d07c5fc42197febf21b94b3e Mon Sep 17 00:00:00 2001 From: damienbod Date: Sat, 22 Jun 2024 06:45:45 +0200 Subject: [PATCH] formatting --- ui/HostingExtensions.cs | 252 ++++++++++++++++++++-------------------- ui/Program.cs | 62 +++++----- 2 files changed, 157 insertions(+), 157 deletions(-) diff --git a/ui/HostingExtensions.cs b/ui/HostingExtensions.cs index 2a8232b..28f3c07 100644 --- a/ui/HostingExtensions.cs +++ b/ui/HostingExtensions.cs @@ -1,129 +1,129 @@ -using Microsoft.AspNetCore.Authentication; -using Microsoft.IdentityModel.JsonWebTokens; -using Microsoft.IdentityModel.Logging; -using Microsoft.IdentityModel.Tokens; -using Serilog; -using System.Security.Cryptography.X509Certificates; -using System.Text.Json; - -namespace WebCodeFlowPkceClient; - -internal static class HostingExtensions -{ - private static IWebHostEnvironment? _env; - public static WebApplication ConfigureServices(this WebApplicationBuilder builder) - { - var services = builder.Services; - var configuration = builder.Configuration; - _env = builder.Environment; - - services.AddAuthentication(options => - { - options.DefaultScheme = "cookie"; - options.DefaultChallengeScheme = "oidc"; - }) - .AddCookie("cookie", options => - { - options.ExpireTimeSpan = TimeSpan.FromHours(8); - options.SlidingExpiration = false; - options.Events.OnSigningOut = async e => - { - await e.HttpContext.RevokeRefreshTokenAsync(); - }; - }) - .AddOpenIdConnect("oidc", options => - { - options.Authority = "https://localhost:5001"; - options.ClientId = "web-dpop"; - options.ClientSecret = "ddedF4f289k$3eDa23ed0iTk4Raq&tttk23d08nhzd"; - options.ResponseType = "code"; - options.ResponseMode = "query"; - options.UsePkce = true; - - options.Scope.Clear(); - options.Scope.Add("openid"); - options.Scope.Add("profile"); - options.Scope.Add("scope-dpop"); - options.Scope.Add("offline_access"); - options.GetClaimsFromUserInfoEndpoint = true; - options.SaveTokens = true; - - options.TokenValidationParameters = new TokenValidationParameters - { - NameClaimType = "name", - RoleClaimType = "role" - }; - }); - - var privatePem = File.ReadAllText(Path.Combine(_env.ContentRootPath, - "ecdsa384-private.pem")); - var publicPem = File.ReadAllText(Path.Combine(_env.ContentRootPath, - "ecdsa384-public.pem")); - var ecdsaCertificate = X509Certificate2.CreateFromPem(publicPem, privatePem); - var ecdsaCertificateKey = new ECDsaSecurityKey(ecdsaCertificate.GetECDsaPrivateKey()); - - //var privatePem = File.ReadAllText(Path.Combine(_environment.ContentRootPath, - // "rsa256-private.pem")); - //var publicPem = File.ReadAllText(Path.Combine(_environment.ContentRootPath, - // "rsa256-public.pem")); - //var rsaCertificate = X509Certificate2.CreateFromPem(publicPem, privatePem); - //var rsaCertificateKey = new RsaSecurityKey(rsaCertificate.GetRSAPrivateKey()); - - // add automatic token management - services.AddOpenIdConnectAccessTokenManagement(options => - { - // create and configure a DPoP JWK - //var rsaKey = new RsaSecurityKey(RSA.Create(2048)); - //var jwk = JsonWebKeyConverter.ConvertFromSecurityKey(rsaKey); - //jwk.Alg = "PS256"; - //options.DPoPJsonWebKey = JsonSerializer.Serialize(jwk); - - //var jwk = JsonWebKeyConverter.ConvertFromSecurityKey(rsaCertificateKey); - //jwk.Alg = "PS256"; - //options.DPoPJsonWebKey = JsonSerializer.Serialize(jwk); - - var jwk = JsonWebKeyConverter.ConvertFromSecurityKey(ecdsaCertificateKey); - jwk.Alg = "ES384"; - options.DPoPJsonWebKey = JsonSerializer.Serialize(jwk); - }); - - services.AddUserAccessTokenHttpClient("dpop-api-client", configureClient: client => - { - client.BaseAddress = new Uri("https://localhost:5005"); - }); - - services.AddRazorPages(); - - return builder.Build(); +using Microsoft.AspNetCore.Authentication; +using Microsoft.IdentityModel.JsonWebTokens; +using Microsoft.IdentityModel.Logging; +using Microsoft.IdentityModel.Tokens; +using Serilog; +using System.Security.Cryptography.X509Certificates; +using System.Text.Json; + +namespace WebCodeFlowPkceClient; + +internal static class HostingExtensions +{ + private static IWebHostEnvironment? _env; + public static WebApplication ConfigureServices(this WebApplicationBuilder builder) + { + var services = builder.Services; + var configuration = builder.Configuration; + _env = builder.Environment; + + services.AddAuthentication(options => + { + options.DefaultScheme = "cookie"; + options.DefaultChallengeScheme = "oidc"; + }) + .AddCookie("cookie", options => + { + options.ExpireTimeSpan = TimeSpan.FromHours(8); + options.SlidingExpiration = false; + options.Events.OnSigningOut = async e => + { + await e.HttpContext.RevokeRefreshTokenAsync(); + }; + }) + .AddOpenIdConnect("oidc", options => + { + options.Authority = "https://localhost:5001"; + options.ClientId = "web-dpop"; + options.ClientSecret = "ddedF4f289k$3eDa23ed0iTk4Raq&tttk23d08nhzd"; + options.ResponseType = "code"; + options.ResponseMode = "query"; + options.UsePkce = true; + + options.Scope.Clear(); + options.Scope.Add("openid"); + options.Scope.Add("profile"); + options.Scope.Add("scope-dpop"); + options.Scope.Add("offline_access"); + options.GetClaimsFromUserInfoEndpoint = true; + options.SaveTokens = true; + + options.TokenValidationParameters = new TokenValidationParameters + { + NameClaimType = "name", + RoleClaimType = "role" + }; + }); + + var privatePem = File.ReadAllText(Path.Combine(_env.ContentRootPath, + "ecdsa384-private.pem")); + var publicPem = File.ReadAllText(Path.Combine(_env.ContentRootPath, + "ecdsa384-public.pem")); + var ecdsaCertificate = X509Certificate2.CreateFromPem(publicPem, privatePem); + var ecdsaCertificateKey = new ECDsaSecurityKey(ecdsaCertificate.GetECDsaPrivateKey()); + + //var privatePem = File.ReadAllText(Path.Combine(_environment.ContentRootPath, + // "rsa256-private.pem")); + //var publicPem = File.ReadAllText(Path.Combine(_environment.ContentRootPath, + // "rsa256-public.pem")); + //var rsaCertificate = X509Certificate2.CreateFromPem(publicPem, privatePem); + //var rsaCertificateKey = new RsaSecurityKey(rsaCertificate.GetRSAPrivateKey()); + + // add automatic token management + services.AddOpenIdConnectAccessTokenManagement(options => + { + // create and configure a DPoP JWK + //var rsaKey = new RsaSecurityKey(RSA.Create(2048)); + //var jwk = JsonWebKeyConverter.ConvertFromSecurityKey(rsaKey); + //jwk.Alg = "PS256"; + //options.DPoPJsonWebKey = JsonSerializer.Serialize(jwk); + + //var jwk = JsonWebKeyConverter.ConvertFromSecurityKey(rsaCertificateKey); + //jwk.Alg = "PS256"; + //options.DPoPJsonWebKey = JsonSerializer.Serialize(jwk); + + var jwk = JsonWebKeyConverter.ConvertFromSecurityKey(ecdsaCertificateKey); + jwk.Alg = "ES384"; + options.DPoPJsonWebKey = JsonSerializer.Serialize(jwk); + }); + + services.AddUserAccessTokenHttpClient("dpop-api-client", configureClient: client => + { + client.BaseAddress = new Uri("https://localhost:5005"); + }); + + services.AddRazorPages(); + + return builder.Build(); } - public static WebApplication ConfigurePipeline(this WebApplication app) - { - IdentityModelEventSource.ShowPII = true; - JsonWebTokenHandler.DefaultInboundClaimTypeMap.Clear(); - - app.UseSerilogRequestLogging(); - - if (_env!.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - else - { - app.UseExceptionHandler("/Error"); - app.UseHsts(); - } - - app.UseHttpsRedirection(); - app.UseStaticFiles(); - - app.UseRouting(); - - app.UseAuthentication(); - app.UseAuthorization(); - - app.MapRazorPages(); - - return app; - } + public static WebApplication ConfigurePipeline(this WebApplication app) + { + IdentityModelEventSource.ShowPII = true; + JsonWebTokenHandler.DefaultInboundClaimTypeMap.Clear(); + + app.UseSerilogRequestLogging(); + + if (_env!.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + else + { + app.UseExceptionHandler("/Error"); + app.UseHsts(); + } + + app.UseHttpsRedirection(); + app.UseStaticFiles(); + + app.UseRouting(); + + app.UseAuthentication(); + app.UseAuthorization(); + + app.MapRazorPages(); + + return app; + } } \ No newline at end of file diff --git a/ui/Program.cs b/ui/Program.cs index 2abcdbe..d0d08ce 100644 --- a/ui/Program.cs +++ b/ui/Program.cs @@ -1,34 +1,34 @@ -using Serilog; -using WebCodeFlowPkceClient; - -Log.Logger = new LoggerConfiguration() - .WriteTo.Console() - .CreateBootstrapLogger(); - -Log.Information("Starting up API"); - -try -{ - var builder = WebApplication.CreateBuilder(args); - - builder.Host.UseSerilog((ctx, lc) => lc - .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}") - .WriteTo.File("../_logs-webCodeFlowPkceClient.txt") - .Enrich.FromLogContext() - .ReadFrom.Configuration(ctx.Configuration)); - - var app = builder - .ConfigureServices() +using Serilog; +using WebCodeFlowPkceClient; + +Log.Logger = new LoggerConfiguration() + .WriteTo.Console() + .CreateBootstrapLogger(); + +Log.Information("Starting up API"); + +try +{ + var builder = WebApplication.CreateBuilder(args); + + builder.Host.UseSerilog((ctx, lc) => lc + .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}") + .WriteTo.File("../_logs-webCodeFlowPkceClient.txt") + .Enrich.FromLogContext() + .ReadFrom.Configuration(ctx.Configuration)); + + var app = builder + .ConfigureServices() .ConfigurePipeline(); - app.Run(); -} -catch (Exception ex) when (ex.GetType().Name is not "StopTheHostException" && ex.GetType().Name is not "HostAbortedException") -{ - Log.Fatal(ex, "Unhandled exception"); -} -finally -{ - Log.Information("Shut down complete"); - Log.CloseAndFlush(); + app.Run(); +} +catch (Exception ex) when (ex.GetType().Name is not "StopTheHostException" && ex.GetType().Name is not "HostAbortedException") +{ + Log.Fatal(ex, "Unhandled exception"); +} +finally +{ + Log.Information("Shut down complete"); + Log.CloseAndFlush(); } \ No newline at end of file