Add project files.
This commit is contained in:
		| @@ -0,0 +1,59 @@ | ||||
| using Core.Cerberos.External.Clients; | ||||
| using Core.Cerberos.External.GatewayConfigurations; | ||||
| using Core.Cerberos.External.Helpers.Token; | ||||
| using Lib.Architecture.BuildingBlocks; | ||||
| using Microsoft.AspNetCore.Authentication; | ||||
| using Microsoft.AspNetCore.Http; | ||||
| using Microsoft.Extensions.Configuration; | ||||
| using Microsoft.Extensions.DependencyInjection; | ||||
| using Refit; | ||||
|  | ||||
| namespace Core.Cerberos.External.ClientConfiguration | ||||
| { | ||||
|     public static class RegisterClientConfiguration | ||||
|     { | ||||
|         public static IServiceCollection RegisterExternalLayer(this IServiceCollection services, IConfiguration configuration) | ||||
|         { | ||||
|             var gatewayConfiguration = new GatewayConfiguration(); | ||||
|             var gatewaySettingsConfiguration = new GatewaySettingsConfigurations(configuration); | ||||
|  | ||||
|             // Register GatewayConfiguration as a singleton | ||||
|             services.AddSingleton(gatewayConfiguration); | ||||
|  | ||||
|             // Register IHttpContextAccessor | ||||
|             services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); | ||||
|  | ||||
|             // Register ITokenProvider | ||||
|             services.AddSingleton<ITokenProvider, HttpContextTokenProvider>(); | ||||
|  | ||||
|             // Register the custom AuthenticatedHttpClientHandler | ||||
|             services.AddTransient(provider => | ||||
|             { | ||||
|                 var tokenProvider = provider.GetRequiredService<ITokenProvider>(); | ||||
|                 var handler = new AuthenticatedHttpClientHandler(tokenProvider) | ||||
|                 { | ||||
|                     InnerHandler = new HttpClientHandler() // Setting the InnerHandler manually | ||||
|                 }; | ||||
|                 return handler; | ||||
|             }); | ||||
|  | ||||
|             var cerberosServiceApiUrl = GatewaySettingsConfigurations.GetCerberosServiceAPIEndpoint().Endpoint.Url; | ||||
|  | ||||
|             // Register ICerberosServiceClient with the manually created HttpClient | ||||
|             services.AddScoped<ICerberosServiceClient>(provider => | ||||
|             { | ||||
|                 var handler = provider.GetRequiredService<AuthenticatedHttpClientHandler>(); | ||||
|                 var httpClient = new HttpClient(handler) | ||||
|                 { | ||||
|                     BaseAddress = new Uri(cerberosServiceApiUrl), | ||||
|                     Timeout = TimeSpan.FromMinutes(1) | ||||
|                 }; | ||||
|                 return RestService.For<ICerberosServiceClient>(httpClient); | ||||
|             }); | ||||
|  | ||||
|             services.AddScoped<IAuthenticationService, AuthenticationService>(); | ||||
|  | ||||
|             return services; | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										118
									
								
								Core.Cerberos.External/Clients/ICerberosServiceClient.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										118
									
								
								Core.Cerberos.External/Clients/ICerberosServiceClient.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,118 @@ | ||||
| using Core.Blueprint.Storage.Adapters; | ||||
| using Core.Cerberos.Adapters; | ||||
| using Core.Cerberos.Adapters.Common.Constants; | ||||
| using Core.Cerberos.Adapters.Common.Enums; | ||||
| using Core.Cerberos.External.Clients.Requests; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
| using Refit; | ||||
|  | ||||
| namespace Core.Cerberos.External.Clients | ||||
| { | ||||
|     public interface ICerberosServiceClient | ||||
|     { | ||||
|         [Get("/v1/User")] | ||||
|         Task<IEnumerable<UserAdapter>> GetAllUsersAsync(CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Get("/v1/User/" + Routes.Id)] | ||||
|         Task<UserAdapter> GetUserByIdAsync([FromRoute] string id, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Get("/v1/User/" + Routes.Email)] | ||||
|         Task<UserAdapter> GetUserByEmailAsync([FromRoute] string email, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Get("/v1/User/{email}/ValidateExistence")] | ||||
|         Task<UserExistenceAdapter> ValidateUserExistence([FromRoute] string email, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Post("/v1/User/" + Routes.Register)] | ||||
|         Task<UserAdapter> CreateUserAsync([FromBody] UserRequest newUser, [FromRoute] bool sendInvitation, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Put("/v1/User/" + Routes.Id)] | ||||
|         Task<UserAdapter> UpdateUserAsync([FromBody] UserAdapter entity, [FromRoute] string id, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Patch("/v1/User/" + Routes.LogIn)] | ||||
|         Task<UserAdapter> LoginUserAsync([FromRoute] string email, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Patch("/v1/User/" + Routes.LogOut)] | ||||
|         Task<UserAdapter> LogoutUserAsync([FromRoute] string email, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Patch("/v1/User/" + Routes.ChangeStatus)] | ||||
|         Task<UserAdapter> ChangeUserStatusAsync([FromRoute] string id, StatusEnum newStatus, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Get("/v1/User/{email}/GetTokenAdapter")] | ||||
|         Task<TokenAdapter> GetTokenAdapter([FromRoute] string email, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Post("/v1/User/" + Routes.AddCompany)] | ||||
|         Task<UserAdapter> AddCompanyToUserAsync([FromRoute] string userId, [FromRoute] string companyId, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Delete("/v1/User/" + Routes.RemoveCompany)] | ||||
|         Task<UserAdapter> RemoveCompanyToUserAsync([FromRoute] string userId, [FromRoute] string companyId, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Post("/v1/User/" + Routes.AddProject)] | ||||
|         Task<UserAdapter> AddProjectToUserAsync([FromRoute] string userId, [FromRoute] string projectId, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Delete("/v1/User/" + Routes.RemoveProject)] | ||||
|         Task<UserAdapter> RemoveProjectToUserAsync([FromRoute] string userId, [FromRoute] string projectId, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Get("/v1/User/GetConsentFormPDF")] | ||||
|         Task<BlobDownloadUriAdapter> GetConsentFormPDFAsync(CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Patch("/v1/User/AcceptUserConsentForm")] | ||||
|         Task<UserAdapter> AcceptUserConsentFormAsync(CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Get("/v1/Role")] | ||||
|         Task<IEnumerable<RoleAdapter>> GetAllRolesAsync(CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Get("/v1/Role/" + Routes.Id)] | ||||
|         Task<RoleAdapter> GetRoleByIdAsync([FromRoute] string id, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Post("/v1/Role")] | ||||
|         Task<RoleAdapter> CreateRoleAsync([FromBody] RoleRequest newRole, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Put("/v1/Role/" + Routes.Id)] | ||||
|         Task<RoleAdapter> UpdateRoleAsync([FromBody] RoleAdapter entity, [FromRoute] string id, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Patch("/v1/Role/" + Routes.ChangeStatus)] | ||||
|         Task<RoleAdapter> ChangeRoleStatusAsync([FromRoute] string id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Post("/v1/Role/" + Routes.AddApplication)] | ||||
|         Task<RoleAdapter> AddApplicationToRoleAsync([FromRoute] string RoleId, [FromRoute] ApplicationsEnum application, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Delete("/v1/Role/" + Routes.RemoveApplication)] | ||||
|         Task<RoleAdapter> RemoveApplicationToRoleAsync([FromRoute] string RoleId, [FromRoute] ApplicationsEnum application, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Get("/v1/Permission")] | ||||
|         Task<IEnumerable<PermissionAdapter>> GetAllPermissionsAsync(CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Post("/v1/Permission/" + Routes.GetPermissionList)] | ||||
|         Task<IEnumerable<PermissionAdapter>> GetAllPermissionsByListAsync([FromBody] string[] request, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Get("/v1/Permission/" + Routes.Id)] | ||||
|         Task<PermissionAdapter> GetPermissionByIdAsync([FromRoute] string id, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Post("/v1/Permission")] | ||||
|         Task<PermissionAdapter> CreatePermissionAsync([FromBody] PermissionRequest newPermission, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Put("/v1/Permission/" + Routes.Id)] | ||||
|         Task<PermissionAdapter> UpdatePermissionAsync([FromBody] PermissionAdapter entity, [FromRoute] string id, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Patch("/v1/Permission/" + Routes.ChangeStatus)] | ||||
|         Task<PermissionAdapter> ChangeStatusPermissionAsync([FromRoute] string id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Get("/v1/Module")] | ||||
|         Task<IEnumerable<ModuleAdapter>> GetAllModulesAsync(CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Post("/v1/Module/" + Routes.GetModuleList)] | ||||
|         Task<IEnumerable<ModuleAdapter>> GetAllModulesByListAsync([FromBody] string[] request, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Get("/v1/Module/" + Routes.Id)] | ||||
|         Task<ModuleAdapter> GetModuleByIdAsync([FromRoute] string id, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Post("/v1/Module")] | ||||
|         Task<ModuleAdapter> CreateModuleAsync([FromBody] ModuleRequest newModule, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Put("/v1/Module/" + Routes.Id)] | ||||
|         Task<ModuleAdapter> UpdateModuleAsync([FromBody] ModuleAdapter entity, [FromRoute] string id, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Patch("/v1/Module/" + Routes.ChangeStatus)] | ||||
|         Task<ModuleAdapter> ChangeStatusModuleAsync([FromRoute] string id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken = default); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										16
									
								
								Core.Cerberos.External/Clients/Requests/ModuleRequest.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								Core.Cerberos.External/Clients/Requests/ModuleRequest.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | ||||
| using Core.Cerberos.Adapters.Common.Enums; | ||||
| using System.Text.Json.Serialization; | ||||
|  | ||||
| namespace Core.Cerberos.External.Clients.Requests | ||||
| { | ||||
|     public class ModuleRequest | ||||
|     { | ||||
|         public string Name { get; set; } = null!; | ||||
|         public string? Description { get; set; } | ||||
|         public string? Icon { get; set; } | ||||
|         public string? Route { get; set; } | ||||
|         public int? Order { get; set; } | ||||
|         [JsonConverter(typeof(JsonStringEnumConverter))] | ||||
|         public ApplicationsEnum? Application { get; set; } = null!; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										12
									
								
								Core.Cerberos.External/Clients/Requests/PermissionRequest.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								Core.Cerberos.External/Clients/Requests/PermissionRequest.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| using Core.Cerberos.Adapters.Common.Constants; | ||||
|  | ||||
| namespace Core.Cerberos.External.Clients.Requests | ||||
| { | ||||
|     public class PermissionRequest | ||||
|     { | ||||
|         public string Name { get; set; } = null!; | ||||
|         public string? Description { get; set; } | ||||
|         public string? RoleId { get; set; } | ||||
|         public AccessLevelEnum? AccessLevel { get; set; } = null!; | ||||
|     } | ||||
| } | ||||
							
								
								
									
										16
									
								
								Core.Cerberos.External/Clients/Requests/RoleRequest.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								Core.Cerberos.External/Clients/Requests/RoleRequest.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | ||||
| using Core.Cerberos.Adapters.Common.Enums; | ||||
| using System.Text.Json.Serialization; | ||||
|  | ||||
| namespace Core.Cerberos.External.Clients.Requests | ||||
| { | ||||
|     public class RoleRequest | ||||
|     { | ||||
|         public string Name { get; set; } = null!; | ||||
|         public string? Description { get; set; } | ||||
|         [JsonConverter(typeof(EnumArrayJsonConverter<ApplicationsEnum>))] | ||||
|         public ApplicationsEnum[]? Applications { get; set; } | ||||
|         public string[] Modules { get; set; } = null!; | ||||
|         public string[] Permissions { get; set; } = null!; | ||||
|  | ||||
|     } | ||||
| } | ||||
							
								
								
									
										13
									
								
								Core.Cerberos.External/Clients/Requests/UserRequest.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								Core.Cerberos.External/Clients/Requests/UserRequest.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,13 @@ | ||||
| namespace Core.Cerberos.External.Clients.Requests | ||||
| { | ||||
|     public class UserRequest | ||||
|     { | ||||
|         public string Email { get; set; } = null!; | ||||
|         public string Name { get; set; } = null!; | ||||
|         public string? MiddleName { get; set; } | ||||
|         public string LastName { get; set; } = null!; | ||||
|         public string RoleId { get; set; } = null!; | ||||
|         public string[] Companies { get; set; } = null!; | ||||
|         public string[]? Projects { get; set; } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										16
									
								
								Core.Cerberos.External/Core.Cerberos.External.csproj
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								Core.Cerberos.External/Core.Cerberos.External.csproj
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | ||||
| <Project Sdk="Microsoft.NET.Sdk"> | ||||
|  | ||||
|   <PropertyGroup> | ||||
|     <TargetFramework>net8.0</TargetFramework> | ||||
|     <ImplicitUsings>enable</ImplicitUsings> | ||||
|     <Nullable>enable</Nullable> | ||||
|   </PropertyGroup> | ||||
|  | ||||
|   <ItemGroup> | ||||
|     <PackageReference Include="Core.Blueprint.Storage" Version="0.3.0-alpha0049" /> | ||||
|     <PackageReference Include="Core.Cerberos.Adapters" Version="0.3.0-alpha0042" /> | ||||
|     <PackageReference Include="Lib.Architecture.BuildingBlocks" Version="0.9.0-alpha0001" /> | ||||
|     <PackageReference Include="Refit" Version="8.0.0" /> | ||||
|   </ItemGroup> | ||||
|  | ||||
| </Project> | ||||
| @@ -0,0 +1,19 @@ | ||||
| using Core.Blueprint.External; | ||||
|  | ||||
| namespace Core.Cerberos.External.GatewayConfigurations | ||||
| { | ||||
|     public record GatewayConfiguration | ||||
|     { | ||||
|         public GatewayConfiguration() | ||||
|         { | ||||
|             CerberosService = new CerberosServiceAPI(); | ||||
|         } | ||||
|  | ||||
|         public CerberosServiceAPI CerberosService { get; set; } | ||||
|     } | ||||
|     public record CerberosServiceAPI | ||||
|     { | ||||
|         public string Channel { get; set; } | ||||
|         public BaseEndpoint Endpoint { get; set; } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,48 @@ | ||||
| using Core.Blueprint.External; | ||||
| using Microsoft.Extensions.Configuration; | ||||
|  | ||||
| namespace Core.Cerberos.External.GatewayConfigurations | ||||
| { | ||||
|     public class GatewaySettingsConfigurations | ||||
|     { | ||||
|         private static GatewayConfiguration GatewayConfigurations { get; set; } = new GatewayConfiguration(); | ||||
|         private readonly IConfiguration _configuration; | ||||
|  | ||||
|         public GatewaySettingsConfigurations(IConfiguration configuration) | ||||
|         { | ||||
|             _configuration = configuration; | ||||
|             this.SetCerberosServiceAPIEndpoint(); | ||||
|         } | ||||
|         public static CerberosServiceAPI GetCerberosServiceAPIEndpoint() | ||||
|         { | ||||
|             return GatewayConfigurations.CerberosService; | ||||
|         } | ||||
|         private GatewayConfiguration SetCerberosServiceAPIEndpoint() | ||||
|         { | ||||
|             IConfigurationSection source; | ||||
|             var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? string.Empty; | ||||
|  | ||||
|             if (environment == "Local") | ||||
|                 source = _configuration.GetSection("LocalGateways"); | ||||
|             else | ||||
|                 source = _configuration.GetSection("Gateways"); | ||||
|  | ||||
|             var endpoint = source["CerberosDAL"] ?? string.Empty; | ||||
|  | ||||
|             if (string.IsNullOrEmpty(endpoint)) throw new Exception("Cerberos DAL endpoint is empty or null"); | ||||
|  | ||||
|             GatewayConfigurations.CerberosService = new CerberosServiceAPI() | ||||
|             { | ||||
|                 Endpoint = new BaseEndpoint() | ||||
|                 { | ||||
|                     Uri = new Uri(endpoint), | ||||
|                     Url = endpoint, | ||||
|                     Token = string.Empty, | ||||
|                     APIName = "Cerberos Service" | ||||
|                 } | ||||
|             }; | ||||
|  | ||||
|             return GatewayConfigurations; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,32 @@ | ||||
| // *********************************************************************** | ||||
| // <copyright file="AuthenticatedHttpClientHandler.cs"> | ||||
| //     Heath | ||||
| // </copyright> | ||||
| // *********************************************************************** | ||||
|  | ||||
| namespace Core.Cerberos.External.Helpers.Token | ||||
| { | ||||
|     /// <summary> | ||||
|     /// Class to inject the token in all requests. | ||||
|     /// </summary> | ||||
|     public class AuthenticatedHttpClientHandler : DelegatingHandler | ||||
|     { | ||||
|         private readonly ITokenProvider _tokenProvider; | ||||
|  | ||||
|         public AuthenticatedHttpClientHandler(ITokenProvider tokenProvider) | ||||
|         { | ||||
|             _tokenProvider = tokenProvider; | ||||
|         } | ||||
|  | ||||
|         protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var token = _tokenProvider.GetToken(); | ||||
|             if (!string.IsNullOrEmpty(token)) | ||||
|             { | ||||
|                 request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); | ||||
|             } | ||||
|  | ||||
|             return await base.SendAsync(request, cancellationToken); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,31 @@ | ||||
| // *********************************************************************** | ||||
| // <copyright file="HttpContextTokenProvider.cs"> | ||||
| //     Heath | ||||
| // </copyright> | ||||
| // *********************************************************************** | ||||
|  | ||||
| using Microsoft.AspNetCore.Http; | ||||
|  | ||||
| namespace Core.Cerberos.External.Helpers.Token | ||||
| { | ||||
|     /// <summary> | ||||
|     /// Class to return the access token to controllers. | ||||
|     /// </summary> | ||||
|     public class HttpContextTokenProvider : ITokenProvider | ||||
|     { | ||||
|         private readonly IHttpContextAccessor _httpContextAccessor; | ||||
|  | ||||
|         public HttpContextTokenProvider(IHttpContextAccessor httpContextAccessor) | ||||
|         { | ||||
|             _httpContextAccessor = httpContextAccessor; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Get token from headers. | ||||
|         /// </summary> | ||||
|         public string GetToken() | ||||
|         { | ||||
|             return _httpContextAccessor.HttpContext?.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										19
									
								
								Core.Cerberos.External/Helpers/Token/ITokenProvider.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								Core.Cerberos.External/Helpers/Token/ITokenProvider.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | ||||
| // *********************************************************************** | ||||
| // <copyright file="ITokenProvider.cs"> | ||||
| //     Heath | ||||
| // </copyright> | ||||
| // *********************************************************************** | ||||
|  | ||||
| namespace Core.Cerberos.External.Helpers.Token | ||||
| { | ||||
|     /// <summary> | ||||
|     /// Interface for token provider. | ||||
|     /// </summary> | ||||
|     public interface ITokenProvider | ||||
|     { | ||||
|         /// <summary> | ||||
|         /// Get token from headers. | ||||
|         /// </summary> | ||||
|         string GetToken(); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Sergio Matias Urquin
					Sergio Matias Urquin