Added authentication and authorization, updated dependency injections, removed hard-coded connection string
This commit is contained in:
		| @@ -0,0 +1,27 @@ | ||||
| using ApplicationLayer.AuthServices.LoginService; | ||||
| using ApplicationLayer.AuthServices.RegisterService; | ||||
| using ApplicationLayer.AuthServices.Requests; | ||||
| using Microsoft.AspNetCore.Identity.Data; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
|  | ||||
| namespace SchengenVisaApi.Controllers | ||||
| { | ||||
|     [ApiController] | ||||
|     [Route("auth")] | ||||
|     public class UsersController(IRegisterService registerService, ILoginService loginService) : Controller | ||||
|     { | ||||
|         [HttpPost] | ||||
|         public async Task<IActionResult> Register(RegisterApplicantRequest request, CancellationToken cancellationToken) | ||||
|         { | ||||
|             await registerService.Register(request, cancellationToken); | ||||
|             return Created(); | ||||
|         } | ||||
|  | ||||
|         [HttpGet] | ||||
|         public async Task<IActionResult> Login(string email, string password, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var result = await loginService.LoginAsync(new UserLoginRequest(email, password), cancellationToken); | ||||
|             return Ok(result); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -1,5 +1,5 @@ | ||||
| using ApplicationLayer.VisaApplications.Handlers; | ||||
| using ApplicationLayer.VisaApplications.Requests; | ||||
| using ApplicationLayer.DataAccessingServices.VisaApplications.Handlers; | ||||
| using ApplicationLayer.DataAccessingServices.VisaApplications.Requests; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
|  | ||||
| namespace SchengenVisaApi.Controllers; | ||||
|   | ||||
| @@ -1,6 +1,10 @@ | ||||
| using System.Reflection; | ||||
| using System.Text; | ||||
| using ApplicationLayer; | ||||
| using Infrastructure; | ||||
| using Infrastructure.Auth; | ||||
| using Microsoft.AspNetCore.Authentication.JwtBearer; | ||||
| using Microsoft.IdentityModel.Tokens; | ||||
|  | ||||
| namespace SchengenVisaApi; | ||||
|  | ||||
| @@ -8,21 +12,61 @@ namespace SchengenVisaApi; | ||||
| public static class DependencyInjection | ||||
| { | ||||
|     /// Add needed services | ||||
|     public static IServiceCollection RegisterServices(this IServiceCollection services) | ||||
|     public static void RegisterServices(this WebApplicationBuilder builder) | ||||
|     { | ||||
|         services | ||||
|             .AddInfrastructure() | ||||
|         var config = builder.Configuration; | ||||
|         var environment = builder.Environment; | ||||
|  | ||||
|         builder.Services | ||||
|             .AddInfrastructure(config, environment.IsDevelopment()) | ||||
|             .AddApplicationLayer() | ||||
|             .AddPresentation(); | ||||
|             .AddAuth(config) | ||||
|             .AddPresentation(environment); | ||||
|     } | ||||
|  | ||||
|     /// Add services needed for Presentation layer | ||||
|     private static void AddPresentation(this IServiceCollection services, | ||||
|         IWebHostEnvironment environment) | ||||
|     { | ||||
|         if (environment.IsDevelopment()) | ||||
|         { | ||||
|             services.AddSwagger(); | ||||
|         } | ||||
|  | ||||
|         services.AddControllers(); | ||||
|     } | ||||
|  | ||||
|     /// Adds authentication, authorization and token generator | ||||
|     private static IServiceCollection AddAuth(this IServiceCollection services, IConfigurationManager configurationManager) | ||||
|     { | ||||
|         var parameters = new TokenValidationParameters | ||||
|         { | ||||
|             ValidIssuer = configurationManager["JwtSettings:Issuer"], | ||||
|             ValidAudience = configurationManager["JwtSettings:Audience"], | ||||
|             IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configurationManager["JwtSettings:Key"]!)), | ||||
|             ValidateIssuer = true, | ||||
|             ValidateAudience = true, | ||||
|             ValidateLifetime = true, | ||||
|             ValidateIssuerSigningKey = true | ||||
|         }; | ||||
|  | ||||
|         services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) | ||||
|             .AddJwtBearer(opts => opts.TokenValidationParameters = parameters); | ||||
|         services.AddAuthorization(); | ||||
|  | ||||
|         services.AddTokenGenerator(new TokenGeneratorOptions( | ||||
|             Issuer: parameters.ValidIssuer!, | ||||
|             Audience: parameters.ValidAudience!, | ||||
|             Credentials: new SigningCredentials(parameters.IssuerSigningKey, SecurityAlgorithms.HmacSha256), | ||||
|             ValidTime: TimeSpan.FromMinutes(30) | ||||
|         )); | ||||
|  | ||||
|         return services; | ||||
|     } | ||||
|  | ||||
|     /// Add services needed for Presentation layer | ||||
|     private static void AddPresentation(this IServiceCollection services) | ||||
|     /// Add swagger | ||||
|     private static void AddSwagger(this IServiceCollection services) | ||||
|     { | ||||
|         services.AddControllers(); | ||||
|         services.AddEndpointsApiExplorer(); | ||||
|         services.AddSwaggerGen(options => | ||||
|         { | ||||
|             var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; | ||||
|   | ||||
| @@ -1,11 +1,12 @@ | ||||
| namespace SchengenVisaApi; | ||||
|  | ||||
| #pragma warning disable CS1591 | ||||
| public class Program | ||||
| { | ||||
|     public static void Main(string[] args) | ||||
|     { | ||||
|         var builder = WebApplication.CreateBuilder(args); | ||||
|         builder.Services.RegisterServices(); | ||||
|         builder.RegisterServices(); | ||||
|  | ||||
|         var app = builder.Build(); | ||||
|         app.ConfigurePipelineRequest(); | ||||
| @@ -13,4 +14,4 @@ public class Program | ||||
|         app.Run(); | ||||
|     } | ||||
| } | ||||
| #pragma warning restore CS1591 | ||||
| #pragma warning restore CS1591 | ||||
|   | ||||
| @@ -11,8 +11,11 @@ public static class PipelineRequest | ||||
|  | ||||
|         app.UseHttpsRedirection(); | ||||
|  | ||||
|         app.UseAuthentication() | ||||
|             .UseAuthorization(); | ||||
|  | ||||
|         app.MapControllers(); | ||||
|  | ||||
|         return app; | ||||
|     } | ||||
| } | ||||
| } | ||||
|   | ||||
| @@ -5,5 +5,16 @@ | ||||
|       "Microsoft.AspNetCore": "Warning" | ||||
|     } | ||||
|   }, | ||||
|   "AllowedHosts": "*" | ||||
|   "AllowedHosts": "*", | ||||
|  | ||||
|   "ConnectionStrings": { | ||||
|     "developmentDB": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=visadb;Integrated Security=True;", | ||||
|     "normal'naya db": "" | ||||
|   }, | ||||
|  | ||||
|   "JwtSettings": { | ||||
|     "Issuer":"visaAPI", | ||||
|     "Audience":"visaClient", | ||||
|     "Key": "frsjiajfapojrpwauflakpiowaidoaplakrf" | ||||
|   } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user