Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
damienbod committed Jun 22, 2024
1 parent e1eb2ee commit 96f4921
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 157 deletions.
252 changes: 126 additions & 126 deletions ui/HostingExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
62 changes: 31 additions & 31 deletions ui/Program.cs
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 96f4921

Please sign in to comment.