Skip to content

Commit

Permalink
Merge pull request #145 from Ali-YousefiTelori/develop
Browse files Browse the repository at this point in the history
Support for GetFullAccessPersonalAccessToken
  • Loading branch information
Ali-YousefiTelori authored Jan 31, 2024
2 parents 02b9cb2 + 22b3200 commit 857f598
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.Primitives;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand Down Expand Up @@ -112,7 +113,14 @@ MessageContract<bool> IsAnonymousMethodCalling(HttpContext httpContext)
return (FailedReasonType.Nothing, "controllerActionDescriptor is null or empty, did you sent correct route to me?");
if (controllerActionDescriptor.ControllerTypeInfo.GetCustomAttributes(typeof(AllowAnonymousAttribute)).Any() ||
controllerActionDescriptor.MethodInfo.GetCustomAttributes(typeof(AllowAnonymousAttribute)).Any())
return true;
return true;

if (httpContext.Request.Headers.TryGetValue("Authorization", out StringValues authAvalue))
{
var ownerPat = BaseUnitOfWork.GetFullAccessPersonalAccessToken();
if (authAvalue.Any(x => x.Replace("Bearer ", "").Equals(ownerPat)))
return true;
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.1;net6.0;net7.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.1.42</Version>
<Version>0.0.1.43</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.1.42</Version>
<Version>0.0.1.43</Version>
<Description>asp core servces.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,services,asp,aspnet,aspcore,efcore</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using EasyMicroservices.Cores.AspEntityFrameworkCoreApi;
using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces;
using EasyMicroservices.Cores.Database.Managers;
using EasyMicroservices.Cores.Models;
using EasyMicroservices.Database.Interfaces;
using EasyMicroservices.ServiceContracts;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WhiteLables.GeneratedServices;
Expand Down Expand Up @@ -64,6 +67,7 @@ string GetDefaultUniqueIdentity(ICollection<WhiteLabelContract> whiteLables, lon
}

static SemaphoreSlim SemaphoreSlim { get; set; } = new SemaphoreSlim(1);
static HttpClient WhiteLabelHttpClient = new HttpClient();
/// <summary>
///
/// </summary>
Expand All @@ -84,16 +88,18 @@ public async Task<WhiteLabelInfo> Initialize(IHttpContextAccessor httpContext, s
Console.WriteLine($"WhiteLabelManager Initialize! {microserviceName} {whiteLableRoute}");
if (dbContextTypes.IsEmpty())
return CurrentWhiteLabel;
HttpClient httpClient = new HttpClient();
if (httpContext?.HttpContext != null && httpContext.HttpContext.Request.Headers.TryGetValue("Authorization", out var authorizationHeader))
{
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authorizationHeader.ToString().Replace("Bearer ", ""));
}
var whiteLabelClient = new WhiteLables.GeneratedServices.WhiteLabelClient(whiteLableRoute, httpClient);
var unitOfWork = _serviceProvider.GetService<IUnitOfWork>();
var ownerPat = unitOfWork.GetFullAccessPersonalAccessToken();
if (ownerPat.HasValue())
WhiteLabelHttpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", ownerPat);
else if (httpContext?.HttpContext != null && httpContext.HttpContext.Request.Headers.TryGetValue("Authorization", out var authorizationHeader))
WhiteLabelHttpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authorizationHeader.ToString().Replace("Bearer ", ""));

var whiteLabelClient = new WhiteLables.GeneratedServices.WhiteLabelClient(whiteLableRoute, WhiteLabelHttpClient);
var whiteLabels = await whiteLabelClient.GetAllAsync().AsCheckedResult(x => x.Result).ConfigureAwait(false);
var defaultUniqueIdentity = GetDefaultUniqueIdentity(whiteLabels, null);

var microserviceClient = new WhiteLables.GeneratedServices.MicroserviceClient(whiteLableRoute, httpClient);
var microserviceClient = new WhiteLables.GeneratedServices.MicroserviceClient(whiteLableRoute, WhiteLabelHttpClient);
var microservices = await microserviceClient.GetAllAsync().ConfigureAwait(false);
var foundMicroservice = microservices.Result.FirstOrDefault(x => x.Name.Equals(microserviceName, StringComparison.OrdinalIgnoreCase));
if (foundMicroservice == null)
Expand All @@ -117,7 +123,7 @@ public async Task<WhiteLabelInfo> Initialize(IHttpContextAccessor httpContext, s

var uniqueIdentityManager = new UnitOfWork(_serviceProvider).GetUniqueIdentityManager() as DefaultUniqueIdentityManager;

var microserviceContextTableClient = new WhiteLables.GeneratedServices.MicroserviceContextTableClient(whiteLableRoute, httpClient);
var microserviceContextTableClient = new WhiteLables.GeneratedServices.MicroserviceContextTableClient(whiteLableRoute, WhiteLabelHttpClient);
var microserviceContextTables = await microserviceContextTableClient.GetAllAsync().ConfigureAwait(false);

HashSet<string> addedInWhitLabels = new HashSet<string>();
Expand All @@ -129,7 +135,7 @@ public async Task<WhiteLabelInfo> Initialize(IHttpContextAccessor httpContext, s

foreach (var contextType in dbContextTypes)
{
var contextTableClient = new WhiteLables.GeneratedServices.ContextTableClient(whiteLableRoute, httpClient);
var contextTableClient = new WhiteLables.GeneratedServices.ContextTableClient(whiteLableRoute, WhiteLabelHttpClient);
var contextTables = await contextTableClient.GetAllAsync().ConfigureAwait(false);
using var instanceOfContext = _serviceProvider.GetService(contextType) as DbContext;
string contextName = uniqueIdentityManager.GetContextName(contextType);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EasyMicroservices.Cores.AspCoreApi.Interfaces;
using Authentications.GeneratedServices;
using EasyMicroservices.Cores.AspCoreApi.Interfaces;
using EasyMicroservices.Cores.AspCoreApi.Managers;
using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces;
using EasyMicroservices.Cores.Database.Interfaces;
Expand All @@ -15,13 +16,15 @@
using EasyMicroservices.Mapper.SerializerMapper.Providers;
using EasyMicroservices.Serialization.Interfaces;
using EasyMicroservices.Serialization.Newtonsoft.Json.Providers;
using EasyMicroservices.ServiceContracts;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -528,6 +531,30 @@ public virtual Task<bool> HasUniqueIdentityRole()
return auth.HasUnlimitedPermission(ServiceProvider.GetService<IHttpContextAccessor>()?.HttpContext);
}


/// <summary>
///
/// </summary>
/// <returns></returns>
public string GetFullAccessPersonalAccessToken()
{
var config = GetConfiguration();
var section = config?.GetSection("Authorization");
if (section == null)
return null;
return section.GetValue<string>("FullAccessPAT");
}

/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T GetService<T>()
{
return ServiceProvider.GetService<T>();
}

/// <summary>
///
/// </summary>
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.1.42</Version>
<Version>0.0.1.43</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.1.42</Version>
<Version>0.0.1.43</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.1.42</Version>
<Version>0.0.1.43</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 @@ -44,6 +44,18 @@ public interface IBaseUnitOfWork : IDisposable
/// </summary>
/// <returns></returns>
IUniqueIdentityManager GetUniqueIdentityManager();
/// <summary>
///
/// </summary>
/// <returns></returns>
string GetFullAccessPersonalAccessToken();
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
T GetService<T>();

/// <summary>
///
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.1;net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.1.42</Version>
<Version>0.0.1.43</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>netstandard2.1;net6.0;net7.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.1.42</Version>
<Version>0.0.1.43</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 857f598

Please sign in to comment.