using System.Text; using Azure.Identity; using Hutopy.Application.Common.Interfaces; using Hutopy.Infrastructure.Data; using Hutopy.Web.Services; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.Facebook; using Microsoft.AspNetCore.Authentication.Google; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Mvc; using Microsoft.IdentityModel.Tokens; using NSwag; using NSwag.Generation.Processors.Security; namespace Hutopy.Web; public static class DependencyInjection { public static IServiceCollection AddWebServices(this IServiceCollection services) { services.AddDatabaseDeveloperPageExceptionFilter(); services.AddScoped(); services.AddHttpContextAccessor(); services.AddHealthChecks() .AddDbContextCheck(); services.AddExceptionHandler(); services.AddRazorPages(); services.AddHttpClient(); // Customise default API behaviour services.Configure(options => options.SuppressModelStateInvalidFilter = true); services.AddEndpointsApiExplorer(); return services; } public static IServiceCollection AddKeyVaultIfConfigured(this IServiceCollection services, ConfigurationManager configuration) { var keyVaultUri = configuration["KeyVaultUri"]; if (!string.IsNullOrWhiteSpace(keyVaultUri)) { configuration.AddAzureKeyVault( new Uri(keyVaultUri), new DefaultAzureCredential()); } return services; } public static IServiceCollection AddAuthorizationAndAuthentication(this IServiceCollection services, ConfigurationManager configuration) { var authenticationBuilder = services .AddAuthentication(options => { options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme; }) .AddCookie("Identity.Application", options => { options.LoginPath = "/api/Users/login"; }); var authJwt = configuration.GetSection("Authentication:Jwt"); if (authJwt.Exists()) { authenticationBuilder.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, jwtBearerOptions => { jwtBearerOptions.Authority = "https://hutopy.com"; jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidIssuer = authJwt["Issuer"], ValidateAudience = true, ValidAudience = authJwt["Audience"], ValidateLifetime = true, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(authJwt["Key"] ?? throw new ArgumentNullException("The Jwt Key is missing."))) }; }); } var authGoogle = configuration.GetSection("Authentication:Google"); if (authGoogle.Exists()) { authenticationBuilder.AddGoogle(GoogleDefaults.AuthenticationScheme, options => { options.ClientId = authGoogle["ClientId"] ?? throw new ArgumentNullException("The Google ClientId is missing."); options.ClientSecret = authGoogle["ClientSecret"] ?? throw new ArgumentNullException("The Google ClientSecret is missing."); }); } var authFacebook = configuration.GetSection("Authentication:Facebook"); if (authFacebook.Exists()) { authenticationBuilder.AddFacebook(FacebookDefaults.AuthenticationScheme, options => { options.ClientId = authFacebook["ClientId"] ?? throw new ArgumentNullException("The Facebook ClientId is missing."); options.ClientSecret = authFacebook["ClientSecret"] ?? throw new ArgumentNullException("The Facebook ClientSecret is missing."); }); } return services; } }