This repository has been archived by the owner on Nov 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PDNDClientAssertionServiceExtensions | Some code refactor
- Loading branch information
1 parent
89da2b4
commit e2cb31d
Showing
8 changed files
with
140 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,44 @@ | ||
// (c) 2024 Francesco Del Re <francesco.delre.87@gmail.com> | ||
// This code is licensed under MIT license (see LICENSE.txt for details) | ||
using PDNDClientAssertionGenerator.Configuration; | ||
using PDNDClientAssertionGenerator.Interfaces; | ||
using PDNDClientAssertionGenerator.Services; | ||
using PDNDClientAssertionGenerator.Middleware; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
internal class Program | ||
{ | ||
private static void Main(string[] args) | ||
{ | ||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add services to the container. | ||
// Add services to the container. | ||
|
||
builder.Services.AddControllers(); | ||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
builder.Services.AddControllers(); | ||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
|
||
var configuration = builder.Configuration; | ||
var configuration = builder.Configuration; | ||
|
||
// Add configuration PDNDClientAssertionGenerator | ||
builder.Services.Configure<ClientAssertionConfig>(configuration.GetSection("ClientAssertionConfig")); | ||
builder.Services.AddSingleton<ClientAssertionConfig>(); | ||
builder.Services.AddScoped<IOAuth2Service, OAuth2Service>(); | ||
builder.Services.AddScoped<IClientAssertionGenerator, ClientAssertionGeneratorService>(); | ||
// Add configuration PDNDClientAssertionGenerator | ||
//builder.Services.Configure<ClientAssertionConfig>(configuration.GetSection("ClientAssertionConfig")); | ||
//builder.Services.AddSingleton<ClientAssertionConfig>(); | ||
//builder.Services.AddScoped<IOAuth2Service, OAuth2Service>(); | ||
//builder.Services.AddScoped<IClientAssertionGenerator, ClientAssertionGeneratorService>(); | ||
builder.Services.AddPDNDClientAssertionServices(); | ||
|
||
var app = builder.Build(); | ||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
app.UseHttpsRedirection(); | ||
|
||
app.UseAuthorization(); | ||
app.UseAuthorization(); | ||
|
||
app.MapControllers(); | ||
app.MapControllers(); | ||
|
||
app.Run(); | ||
app.Run(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/PDNDClientAssertionGenerator/Middleware/PDNDClientAssertionServiceExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using PDNDClientAssertionGenerator.Configuration; | ||
using PDNDClientAssertionGenerator.Interfaces; | ||
using PDNDClientAssertionGenerator.Services; | ||
|
||
namespace PDNDClientAssertionGenerator.Middleware | ||
{ | ||
public static class PDNDClientAssertionServiceExtensions | ||
{ | ||
/// <summary> | ||
/// Configures the services required for the PDND Client Assertion process. | ||
/// This method sets up the configuration for `ClientAssertionConfig` and registers necessary services. | ||
/// </summary> | ||
/// <param name="services">The IServiceCollection to which the services are added.</param> | ||
/// <returns>The updated IServiceCollection instance.</returns> | ||
public static IServiceCollection AddPDNDClientAssertionServices(this IServiceCollection services) | ||
{ | ||
// Use ConfigurationManager to load the configuration file (appsettings.json) | ||
var configuration = new ConfigurationManager() | ||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) // Load configuration | ||
.Build(); | ||
|
||
// Ensure that the configuration contains required sections and values | ||
var configSection = configuration.GetSection("ClientAssertionConfig"); | ||
if (!configSection.Exists()) | ||
{ | ||
throw new InvalidOperationException("Missing 'ClientAssertionConfig' section in appsettings.json."); | ||
} | ||
|
||
// Register ClientAssertionConfig as a singleton using the IOptions pattern | ||
services.Configure<ClientAssertionConfig>(config => | ||
{ | ||
// Copy values from the configuration file into the ClientAssertionConfig model | ||
config.ClientId = configuration["ClientAssertionConfig:ClientId"]; | ||
config.ServerUrl = configuration["ClientAssertionConfig:ServerUrl"]; | ||
config.KeyId = configuration["ClientAssertionConfig:KeyId"]; | ||
config.Algorithm = configuration["ClientAssertionConfig:Algorithm"]; | ||
config.Type = configuration["ClientAssertionConfig:Type"]; | ||
config.Issuer = configuration["ClientAssertionConfig:Issuer"]; | ||
config.Subject = configuration["ClientAssertionConfig:Subject"]; | ||
config.Audience = configuration["ClientAssertionConfig:Audience"]; | ||
config.PurposeId = configuration["ClientAssertionConfig:PurposeId"]; | ||
config.KeyPath = configuration["ClientAssertionConfig:KeyPath"]; | ||
config.Duration = int.Parse(configuration["ClientAssertionConfig:Duration"]); | ||
}); | ||
|
||
// Register OAuth2Service and ClientAssertionGeneratorService as scoped services | ||
services.AddScoped<IOAuth2Service, OAuth2Service>(); | ||
services.AddScoped<IClientAssertionGenerator, ClientAssertionGeneratorService>(); | ||
|
||
// Return the updated service collection | ||
return services; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters