diff --git a/README.md b/README.md index 85b5d18..e5589eb 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ To properly set up and use the Client Assertion Generator in your ASP.NET Core a 1. Configure Client Assertion Settings, an example below: ```xml "ClientAssertionConfig": { - "ServerUrl": "", + "ServerUrl": "https://test-server-url.com", "KeyId": "ZmYxZGE2YjQtMzY2Yy00NWI5LThjNGItMDJmYmQyZGIyMmZh", "Algorithm": "RS256", "Type": "at+jwt", @@ -45,7 +45,7 @@ To properly set up and use the Client Assertion Generator in your ASP.NET Core a "Subject": "9b361d49-33f4-4f1e-a88b-4e12661f2309", "Audience": "https://erogatore.example/ente-example/v1", "PurposeId": "1b361d49-33f4-4f1e-a88b-4e12661f2300", - "KeyPath": "/path/", + "KeyPath": "C:/Keys/private.pem", "Duration": "600" }, ``` diff --git a/src/PDNDClientAssertionGenerator.Api/PDNDClientAssertionGenerator.Api.csproj b/src/PDNDClientAssertionGenerator.Api/PDNDClientAssertionGenerator.Api.csproj index 275c1f1..5776d9c 100644 --- a/src/PDNDClientAssertionGenerator.Api/PDNDClientAssertionGenerator.Api.csproj +++ b/src/PDNDClientAssertionGenerator.Api/PDNDClientAssertionGenerator.Api.csproj @@ -8,12 +8,12 @@ - - - - - - + + + + + + diff --git a/src/PDNDClientAssertionGenerator.Api/appsettings.json b/src/PDNDClientAssertionGenerator.Api/appsettings.json index 49016a8..8687156 100644 --- a/src/PDNDClientAssertionGenerator.Api/appsettings.json +++ b/src/PDNDClientAssertionGenerator.Api/appsettings.json @@ -9,7 +9,7 @@ "Subject": "9b361d49-33f4-4f1e-a88b-4e12661f2309", "Audience": "https://erogatore.example/ente-example/v1", "PurposeId": "1b361d49-33f4-4f1e-a88b-4e12661f2300", - "KeyPath": "/path/", + "KeyPath": "C:/Keys/private.pem", "Duration": "600" }, "Logging": { diff --git a/src/PDNDClientAssertionGenerator/Middleware/PDNDClientAssertionServiceExtensions.cs b/src/PDNDClientAssertionGenerator/Middleware/PDNDClientAssertionServiceExtensions.cs index 307d94c..e5f8b18 100644 --- a/src/PDNDClientAssertionGenerator/Middleware/PDNDClientAssertionServiceExtensions.cs +++ b/src/PDNDClientAssertionGenerator/Middleware/PDNDClientAssertionServiceExtensions.cs @@ -1,4 +1,6 @@ -using Microsoft.Extensions.Configuration; +// (c) 2024 Francesco Del Re +// This code is licensed under MIT license (see LICENSE.txt for details) +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using PDNDClientAssertionGenerator.Configuration; using PDNDClientAssertionGenerator.Interfaces; @@ -16,9 +18,11 @@ public static class PDNDClientAssertionServiceExtensions /// The updated IServiceCollection instance. public static IServiceCollection AddPDNDClientAssertionServices(this IServiceCollection services) { - // Use ConfigurationManager to load the configuration file (appsettings.json) + // Use ConfigurationManager to load the configuration file (appsettings.json or environment variables) var configuration = new ConfigurationManager() - .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) // Load configuration + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) + .AddEnvironmentVariables() .Build(); // Ensure that the configuration contains required sections and values diff --git a/src/PDNDClientAssertionGenerator/PDNDClientAssertionGenerator.csproj b/src/PDNDClientAssertionGenerator/PDNDClientAssertionGenerator.csproj index fd197dd..b9f6a3f 100644 --- a/src/PDNDClientAssertionGenerator/PDNDClientAssertionGenerator.csproj +++ b/src/PDNDClientAssertionGenerator/PDNDClientAssertionGenerator.csproj @@ -6,13 +6,14 @@ enable .NET Client Assertion Generator for PDND Service API https://github.com/italia/pdnd-client-assertion-generator - https://github.com/engineering87/pdnd-client-assertion-generator + https://github.com/italia/pdnd-client-assertion-generator LICENSE True .NET Client Assertion Generator for PDND Service API README.md - 1.0.2 + 1.0.3 + Francesco Del Re @@ -27,15 +28,16 @@ - - - - - - - - - + + + + + + + + + + diff --git a/src/PDNDClientAssertionGenerator/Utils/SecurityUtils.cs b/src/PDNDClientAssertionGenerator/Utils/SecurityUtils.cs index 2c8010d..3cb3f87 100644 --- a/src/PDNDClientAssertionGenerator/Utils/SecurityUtils.cs +++ b/src/PDNDClientAssertionGenerator/Utils/SecurityUtils.cs @@ -21,11 +21,20 @@ public static RSAParameters GetSecurityParameters(string keyPath) throw new ArgumentException("Key path cannot be null or empty.", nameof(keyPath)); } + // Normalize the key path by removing any trailing directory or alternative directory separators + string normalizedPath = keyPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + + // Check if the key file exists at the specified path + if (!File.Exists(normalizedPath)) + { + throw new FileNotFoundException($"The specified key file does not exist at the path: {keyPath}"); + } + // Read the PEM content from the specified file string pemContent; try { - pemContent = File.ReadAllText(keyPath).Trim(); + pemContent = File.ReadAllText(normalizedPath).Trim(); } catch (Exception ex) {