using Hutopy.Application.Common.Interfaces; using Hutopy.Domain.Constants; using Hutopy.Infrastructure.Data; using Hutopy.Infrastructure.Data.Interceptors; using Hutopy.Infrastructure.Identity; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.Extensions.Configuration; namespace Microsoft.Extensions.DependencyInjection; public static class DependencyInjection { public static IServiceCollection AddInfrastructureServices(this IServiceCollection services, IConfiguration configuration) { // Replace password in the connection string with env var. var connectionString = configuration.GetConnectionString("DefaultConnection") ?? ""; var dbPassword = Environment.GetEnvironmentVariable("DB_PASSWORD"); connectionString = connectionString.Replace("{DB_PASSWORD}", dbPassword); Guard.Against.Null(connectionString, message: "Connection string 'DefaultConnection' not found."); services.AddScoped(); services.AddScoped(); services.AddDbContext((sp, options) => { options.AddInterceptors(sp.GetServices()); options.UseSqlServer(connectionString); }); services.AddScoped(provider => provider.GetRequiredService()); services.AddScoped(); services.AddAuthentication() .AddBearerToken(IdentityConstants.BearerScheme); services.AddAuthorizationBuilder(); // Might need to change and use AddIdentity() when we need to integrate connection via third party ( facebook, google ) services .AddIdentityCore() .AddRoles() .AddEntityFrameworkStores() .AddApiEndpoints(); services.AddSingleton(TimeProvider.System); services.AddTransient(); services.AddAuthorization(options => options.AddPolicy(Policies.CanPurge, policy => policy.RequireRole(Roles.Administrator))); return services; } }