/* * dbMango * * Copyright 2025 Deutsche Bank AG * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.AspNetCore.Components.Server.Circuits; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; namespace Rms.Service.Bootstrap.Security; internal static class SecurityExtensions { public static IServiceCollection AddStandardSecurity(this IServiceCollection services, IOptions settings, ServiceBootstrapOptions options) where T : class { // For API services: authentication via mTLS and Oidc JWT tokens (both supported) // For Web applications: Oidc code authentication services .AddSingleton() ; if (!string.IsNullOrWhiteSpace(settings.Value.Ldap.Url)) { services .AddSingleton( s => new (s.GetRequiredService>().Value.Ldap)) ; } var authBuilder = services .AddAuthentication(cfg => { if ( options.EnableOpenIdConnect ) { cfg.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; cfg.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; cfg.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; } else if ( options.EnableOidc ) { cfg.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; cfg.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; } }); if ( options.EnableOidc ) { //TokenHelper.ConfigureLocalJwtBearer(builder.Configuration, x) authBuilder .AddJwtBearer(x => OidcHelper.ConfigureOidcJwtBearer(settings, x)) ; } if ( options.EnableOpenIdConnect ) { services .AddScoped() .AddScoped() .AddSingleton() ; services.TryAddEnumerable(ServiceDescriptor.Scoped()); authBuilder .AddCookie( OidcHelper.ConfigureCookieForOpenIdConnect) .AddOpenIdConnect( OpenIdConnectDefaults.AuthenticationScheme, x => OidcHelper.ConfigureOpenIdConnect(settings, x) ); } if ( options.EnableMTLS ) { authBuilder .AddCertificate(x => CertificateHelper.ConfigureCertificateAuthentication(settings, x)); } return services; } public static WebApplication UseStandardSecurity(this WebApplication app, ServiceBootstrapOptions options) where T : class { app .UseAuthentication() .UseAuthorization() ; if ( options.EnableOpenIdConnect ) app.UseMiddleware(); // to provide UserService with tokens return app; } }