From 7a9ce7fccabdc45b3d08a4948caf8d80f16b494f Mon Sep 17 00:00:00 2001 From: Ali Yousefi Date: Mon, 27 Nov 2023 09:51:44 +0330 Subject: [PATCH] Support for use authentication --- .../Authorizations/AspCoreAuthorization.cs | 2 +- .../EasyMicroservices.Cores.AspCoreApi.csproj | 2 +- ...ces.Cores.AspEntityFrameworkCoreApi.csproj | 15 +++++++++- .../StartUpExtensions.cs | 28 ++++++++++++++++--- .../UnitOfWork.cs | 20 +++++++++++++ .../EasyMicroservices.Cores.Clients.csproj | 2 +- .../EasyMicroservices.Cores.Contracts.csproj | 2 +- .../EasyMicroservices.Cores.Database.csproj | 2 +- ...oservices.Cores.EntityFrameworkCore.csproj | 2 +- ...ores.Relational.EntityFrameworkCore.csproj | 2 +- 10 files changed, 65 insertions(+), 12 deletions(-) diff --git a/src/CSharp/EasyMicroservices.Cores.AspCoreApi/Authorizations/AspCoreAuthorization.cs b/src/CSharp/EasyMicroservices.Cores.AspCoreApi/Authorizations/AspCoreAuthorization.cs index fa1025a..5988dc4 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspCoreApi/Authorizations/AspCoreAuthorization.cs +++ b/src/CSharp/EasyMicroservices.Cores.AspCoreApi/Authorizations/AspCoreAuthorization.cs @@ -109,7 +109,7 @@ async Task> HasPermission(HttpContext httpContext) string actionName = httpContext.Request.RouteValues["action"].ToString(); List roleClaims = httpContext.User.FindAll(ClaimTypes.Role).ToList(); if (roleClaims.Count == 0) - return (FailedReasonType.AccessDenied, "There is no claim role founded! did you forgot to use services.AddAuthentication?"); + return (FailedReasonType.AccessDenied, "There is no claim role founded! did you forgot to use services.AddAuthentication? or did you set Bearer for authorize?"); if (!roleClaims.All(x => CachedPermissions.ContainsKey(x.Value))) { foreach (var role in roleClaims) diff --git a/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj b/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj index b7f1d66..42731bb 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj +++ b/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj @@ -4,7 +4,7 @@ net6.0;net7.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.55 + 0.0.0.56 asp core servces. EasyMicroservices@gmail.com core,cores,base,database,services,asp,aspnet diff --git a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj index a705432..fa95a40 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj +++ b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj @@ -4,7 +4,7 @@ net6.0;net7.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.55 + 0.0.0.56 asp core servces. EasyMicroservices@gmail.com core,cores,base,database,services,asp,aspnet,aspcore,efcore @@ -22,6 +22,19 @@ + + + + + + + + + + + + + diff --git a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/StartUpExtensions.cs b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/StartUpExtensions.cs index 0fd91b1..6d4f79e 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/StartUpExtensions.cs +++ b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/StartUpExtensions.cs @@ -1,4 +1,5 @@ using EasyMicroservices.Cores.AspCoreApi.Authorizations; +using EasyMicroservices.Cores.AspCoreApi.Interfaces; using EasyMicroservices.Cores.AspCoreApi.Managers; using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces; using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Middlewares; @@ -7,10 +8,11 @@ using EasyMicroservices.Cores.Interfaces; using EasyMicroservices.Cores.Relational.EntityFrameworkCore; using EasyMicroservices.Cores.Relational.EntityFrameworkCore.Builders; +using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Any; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerGen; @@ -215,11 +217,30 @@ public static WebApplication Build(this WebApplicationBuilder app) /// /// /// - /// /// - public static async Task Build(this WebApplicationBuilder app, bool useGlobalExceptionHandling = false, bool useAuthorization = false) + public static async Task Build(this WebApplicationBuilder app, bool useGlobalExceptionHandling = false) where TContext : RelationalCoreContext { + var useAuth = app.Configuration["Authorization:Use"]; + var useAuthorization = useAuth.HasValue() && useAuth.Equals("true", StringComparison.OrdinalIgnoreCase); + if (useAuthorization) + { + app.Services.AddScoped(); + app.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) + .AddJwtBearer(options => + { + options.TokenValidationParameters = new TokenValidationParameters + { + ValidateIssuer = true, + ValidateAudience = true, + ValidateLifetime = true, + ValidateIssuerSigningKey = true, + ValidIssuer = app.Configuration["Authorization:JWT:Issuer"], + ValidAudience = app.Configuration["Authorization:JWT:Audience"], + IssuerSigningKey = new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(app.Configuration["Authorization:Jwt:Key"])) + }; + }); + } var build = app.Build(); var webApp = await Use(build, useGlobalExceptionHandling, useAuthorization); return await Use(webApp, useGlobalExceptionHandling, useAuthorization); @@ -237,7 +258,6 @@ public static Task Use(this WebApplication webApplication, bool webApplication.UseRouting(); if (useAuthorization) webApplication.UseAuthentication(); - if (useGlobalExceptionHandling) { webApplication.UseExceptionHandler(); diff --git a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/UnitOfWork.cs b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/UnitOfWork.cs index 02e94c2..552341f 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/UnitOfWork.cs +++ b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/UnitOfWork.cs @@ -372,6 +372,26 @@ public virtual Task InitializeWhiteLabel(string microserviceName, string whiteLa return Task.CompletedTask; } + /// + /// + /// + /// + /// + /// + /// + public virtual void Initialize(string microserviceName, params Type[] dbContextTypes) + { + if (ServiceProvider == null) + throw new ObjectDisposedException(nameof(ServiceProvider)); + _InitializeWhiteLabel = (serviceProvider) => + { + return Task.FromResult(new WhiteLabelInfo() + { + MicroserviceName = microserviceName + }); + }; + } + IContractLogic GetInternalLongContractLogic() where TResponseContract : class where TEntity : class diff --git a/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj b/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj index f45c5ea..601d933 100644 --- a/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj @@ -4,7 +4,7 @@ netstandard2.0;netstandard2.1;net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.55 + 0.0.0.56 core of database. EasyMicroservices@gmail.com core,cores,base,client,clients diff --git a/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj b/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj index bf198dd..375de15 100644 --- a/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj @@ -4,7 +4,7 @@ netstandard2.0;netstandard2.1;net45;net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.55 + 0.0.0.56 core contracts. EasyMicroservices@gmail.com core,cores,base,contract,contracts,dto,dtos diff --git a/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj b/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj index 4783e27..426e056 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj @@ -4,7 +4,7 @@ netstandard2.0;netstandard2.1;net45;net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.55 + 0.0.0.56 core of database. EasyMicroservices@gmail.com core,cores,base,database diff --git a/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj b/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj index 35e1217..c9dc1e9 100644 --- a/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj +++ b/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj @@ -4,7 +4,7 @@ net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.55 + 0.0.0.56 ef core of database. EasyMicroservices@gmail.com core,cores,base,database,ef,efcore diff --git a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj index 5ede0e8..333b9c7 100644 --- a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj @@ -4,7 +4,7 @@ net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.55 + 0.0.0.56 ef core of Relational database. EasyMicroservices@gmail.com core,cores,base,database,ef,efcore,Relational