feat: DependentExtensions für extensions mit Abhängigkeiten hinzugefügt
- Methoden `AddDependentExtensions` und `TryGetByRoute` hinzugefügt, um die Konfiguration von `AuthApiParams` und das Abrufen von Deskriptoren zu ermöglichen.
This commit is contained in:
39
src/DigitalData.Auth.API/Config/DependentExtensions.cs
Normal file
39
src/DigitalData.Auth.API/Config/DependentExtensions.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using DigitalData.Core.Abstractions.Security;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace DigitalData.Auth.API.Config
|
||||
{
|
||||
public static class DependentExtensions
|
||||
{
|
||||
private static AuthApiParams? _authApiParams;
|
||||
|
||||
private static AuthApiParams AuthApiParams
|
||||
{
|
||||
get => _authApiParams
|
||||
?? throw new InvalidOperationException(
|
||||
$"DependentExtensions have not been added to the application or are not configured correctly. {typeof(AuthApiParams)} cannot be provided."
|
||||
);
|
||||
set => _authApiParams = value;
|
||||
}
|
||||
|
||||
public static IApplicationBuilder AddDependentExtensions(this IApplicationBuilder application)
|
||||
{
|
||||
var authApiParamOptions = application.ApplicationServices.GetRequiredService<IOptions<AuthApiParams>>();
|
||||
_authApiParams = authApiParamOptions.Value;
|
||||
return application;
|
||||
}
|
||||
|
||||
public static bool TryGetByRoute(this IEnumerable<IAsymmetricTokenDescriptor> descriptors, string consumerRoute, out IAsymmetricTokenDescriptor descriptor)
|
||||
{
|
||||
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
|
||||
descriptor = null;
|
||||
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
|
||||
if (!AuthApiParams.Consumers.TryGetByRoute(consumerRoute, out var consumer)
|
||||
|| descriptors.TryGet(AuthApiParams.Issuer, consumer.Audience, out var _descriptor))
|
||||
return false;
|
||||
|
||||
descriptor = _descriptor;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ using Microsoft.IdentityModel.JsonWebTokens;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using System.Security.Claims;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
@@ -116,6 +117,8 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.AddDependentExtensions();
|
||||
|
||||
issuerSigningKeyInitiator = new Lazy<SecurityKey>(() =>
|
||||
{
|
||||
var factory = app.Services.GetRequiredService<ICryptoFactory>();
|
||||
|
||||
Reference in New Issue
Block a user