diff --git a/src/Host/Nashel.Host.csproj b/src/Host/Nashel.Host.csproj index 5afb6b0..85f5232 100644 --- a/src/Host/Nashel.Host.csproj +++ b/src/Host/Nashel.Host.csproj @@ -3,13 +3,14 @@ net10.0 enable enable - true + false true true $(NoWarn);1591 + diff --git a/src/Host/Program.cs b/src/Host/Program.cs index 83767b1..ec6cccb 100644 --- a/src/Host/Program.cs +++ b/src/Host/Program.cs @@ -1,4 +1,9 @@ +using System.Text; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; +using Nashel.Modules.Identity.Infrastructure; +using Nashel.Modules.Identity.Presentation.Endpoints; var builder = WebApplication.CreateBuilder(args); @@ -41,6 +46,27 @@ builder.Services.AddSwaggerGen(options => } }); +// Регистрация модуля Identity +builder.Services.AddIdentityModule(builder.Configuration); + +// Настройка аутентификации +builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) + .AddJwtBearer(options => + { + options.TokenValidationParameters = new TokenValidationParameters + { + ValidateIssuer = true, + ValidateAudience = true, + ValidateLifetime = true, + ValidateIssuerSigningKey = true, + ValidIssuer = builder.Configuration["JwtSettings:Issuer"], + ValidAudience = builder.Configuration["JwtSettings:Audience"], + IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JwtSettings:Secret"] ?? "super_secret_key_change_me_please_this_is_for_development_only_12345")) + }; + }); + +builder.Services.AddAuthorization(); + var app = builder.Build(); // Настройка конвейера HTTP-запросов. @@ -50,33 +76,14 @@ if (app.Environment.IsDevelopment()) app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Nashel API v1"); - // Чтобы Swagger UI открывался в корне (опционально) - // c.RoutePrefix = string.Empty; }); } -var summaries = new[] -{ - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" -}; +// Включение аутентификации и авторизации +app.UseAuthentication(); +app.UseAuthorization(); -app.MapGet("/weatherforecast", () => -{ - var forecast = Enumerable.Range(1, 5).Select(index => - new WeatherForecast - ( - DateOnly.FromDateTime(DateTime.Now.AddDays(index)), - Random.Shared.Next(-20, 55), - summaries[Random.Shared.Next(summaries.Length)] - )) - .ToArray(); - return forecast; -}) -.WithName("GetWeatherForecast"); +// Маппинг эндпоинтов модуля Identity +app.MapIdentityEndpoints(); app.Run(); - -record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) -{ - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); -} diff --git a/src/Host/appsettings.json b/src/Host/appsettings.json index 8f2d937..7d187a8 100644 --- a/src/Host/appsettings.json +++ b/src/Host/appsettings.json @@ -6,6 +6,9 @@ } }, "AllowedHosts": "*", + "ConnectionStrings": { + "DefaultConnection": "Host=localhost;Database=nashel;Username=postgres;Password=postgres" + }, "JwtSettings": { "Secret": "super_secret_key_change_me_please_this_is_for_development_only_12345", "Issuer": "Nashel", diff --git a/src/Modules/Identity/Infrastructure/DependencyInjection.cs b/src/Modules/Identity/Infrastructure/DependencyInjection.cs index 6d49e2d..b2c9776 100644 --- a/src/Modules/Identity/Infrastructure/DependencyInjection.cs +++ b/src/Modules/Identity/Infrastructure/DependencyInjection.cs @@ -1,14 +1,15 @@ using MediatR; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Nashel.BuildingBlocks.Application.Abstractions; using Nashel.BuildingBlocks.Application.Behaviors; +using Nashel.Modules.Identity.Application.Commands; using Nashel.Modules.Identity.Domain.Repositories; using Nashel.Modules.Identity.Domain.Services; using Nashel.Modules.Identity.Infrastructure.Persistence; using Nashel.Modules.Identity.Infrastructure.Repositories; using Nashel.Modules.Identity.Infrastructure.Services; -using Nashel.Modules.Identity.Application.Commands; namespace Nashel.Modules.Identity.Infrastructure; @@ -19,27 +20,18 @@ public static class DependencyInjection services.AddScoped(); services.AddScoped(); services.AddScoped(); - services.AddHttpContextAccessor(); // Ensure available + services.AddHttpContextAccessor(); - // Add MediatR (auto-scan for handlers in Application layer) services.AddMediatR(cfg => { cfg.RegisterServicesFromAssembly(typeof(RegisterUserCommand).Assembly); cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>)); }); - // Add DbContext services.AddDbContext(options => - // Configure PostgreSQL inside Host or pass options builder - // Assume Host configures DbContext options or use connection string here - // If "NativeAOT" -> Npgsql DataSource approach is preferred in Host Program.cs. - // But standard AddDbContext works too for now. - // Let's assume connection string is provided in configuration or options. - // For now, simple AddDbContext - { - // options.UseNpgsql(configuration.GetConnectionString("DefaultConnection")); - // This requires Microsoft.EntityFrameworkCore.Npgsql package in Infrastructure. - }); + { + options.UseNpgsql(configuration.GetConnectionString("DefaultConnection")); + }); return services; }