using Azure.Identity; using Core.Cerberos.Adapters.Common.Constants; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration.AzureAppConfiguration; using Microsoft.Extensions.Logging; namespace Core.Cerberos.Adapters.Helpers { public static class AuthHelper { private static readonly ILogger logger = LoggerFactory.Create(builder => { builder.AddConsole(); }).CreateLogger("AuthHelper"); public static AuthSettings GetAuthSettings(WebApplicationBuilder builder, string appConfigLabel) { builder.Configuration.AddAzureAppConfiguration(options => { var endpoint = builder.Configuration.GetSection("Endpoints:AppConfigurationURI").Value; if (string.IsNullOrEmpty(endpoint)) throw new ArgumentException("The app configuration is missing"); options.Connect(new Uri(endpoint), new DefaultAzureCredential()) .Select(KeyFilter.Any, "cerberos_common") .Select(KeyFilter.Any, appConfigLabel); options.ConfigureKeyVault(keyVaultOptions => { keyVaultOptions.SetCredential(new DefaultAzureCredential()); }); }); return new AuthSettings { AzureADInstance = builder.Configuration.GetSection(Secrets.AzureADInstance).Value, AzureADTenantId = builder.Configuration.GetSection(Secrets.AzureADTenantId).Value, AzureADClientId = builder.Configuration.GetSection(Secrets.AzureADClientId).Value, AzureADClientSecret = builder.Configuration.GetSection(Secrets.AzureADClientSecret).Value, HeathCerberosAppAuthorizationUrl = builder.Configuration.GetSection(Secrets.HeathCerberosAppAuthorizationUrl).Value, HeathCerberosAppTokenUrl = builder.Configuration.GetSection(Secrets.HeathCerberosAppTokenUrl).Value, HeathCerberosAppClientId = builder.Configuration.GetSection(Secrets.HeathCerberosAppClientId).Value, HeathCerberosAppScope = builder.Configuration.GetSection(Secrets.HeathCerberosAppScope).Value, PrivateKey = builder.Configuration.GetSection(Secrets.PrivateKey).Value, PublicKey = builder.Configuration.GetSection(Secrets.PublicKey).Value, }; } } }