Skip to content

Commit

Permalink
Support for use authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-YousefiTelori committed Nov 27, 2023
1 parent 18b6d5d commit 7a9ce7f
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ async Task<MessageContract<bool>> HasPermission(HttpContext httpContext)
string actionName = httpContext.Request.RouteValues["action"].ToString();
List<Claim> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.55</Version>
<Version>0.0.0.56</Version>
<Description>asp core servces.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,services,asp,aspnet</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.55</Version>
<Version>0.0.0.56</Version>
<Description>asp core servces.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,services,asp,aspnet,aspcore,efcore</PackageTags>
Expand All @@ -22,6 +22,19 @@
<PackageReference Include="EasyMicroservices.Mapper.SerializerMapper" Version="0.0.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="EasyMicroservices.WhiteLabelsMicroservice.Clients" Version="0.0.0.9" />
<!--<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.14" />-->
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net7.0' ">
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.14" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.25" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -215,11 +217,30 @@ public static WebApplication Build(this WebApplicationBuilder app)
/// <typeparam name="TContext"></typeparam>
/// <param name="app"></param>
/// <param name="useGlobalExceptionHandling"></param>
/// <param name="useAuthorization"></param>
/// <returns></returns>
public static async Task<WebApplication> Build<TContext>(this WebApplicationBuilder app, bool useGlobalExceptionHandling = false, bool useAuthorization = false)
public static async Task<WebApplication> Build<TContext>(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<IAuthorization, AspCoreAuthorization>();
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<TContext>(webApp, useGlobalExceptionHandling, useAuthorization);
Expand All @@ -237,7 +258,6 @@ public static Task<WebApplication> Use(this WebApplication webApplication, bool
webApplication.UseRouting();
if (useAuthorization)
webApplication.UseAuthentication();

if (useGlobalExceptionHandling)
{
webApplication.UseExceptionHandler();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,26 @@ public virtual Task InitializeWhiteLabel(string microserviceName, string whiteLa
return Task.CompletedTask;
}

/// <summary>
///
/// </summary>
/// <param name="microserviceName"></param>
/// <param name="dbContextTypes"></param>
/// <returns></returns>
/// <exception cref="ObjectDisposedException"></exception>
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<TEntity, TCreateRequestContract, TUpdateRequestContract, TResponseContract, long> GetInternalLongContractLogic<TEntity, TCreateRequestContract, TUpdateRequestContract, TResponseContract>()
where TResponseContract : class
where TEntity : class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.55</Version>
<Version>0.0.0.56</Version>
<Description>core of database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,client,clients</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.0;netstandard2.1;net45;net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.55</Version>
<Version>0.0.0.56</Version>
<Description>core contracts.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,contract,contracts,dto,dtos</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.0;netstandard2.1;net45;net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.55</Version>
<Version>0.0.0.56</Version>
<Description>core of database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.55</Version>
<Version>0.0.0.56</Version>
<Description>ef core of database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,ef,efcore</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.55</Version>
<Version>0.0.0.56</Version>
<Description>ef core of Relational database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,ef,efcore,Relational</PackageTags>
Expand Down

0 comments on commit 7a9ce7f

Please sign in to comment.