Compare commits
	
		
			17 Commits
		
	
	
		
			feature/ad
			...
			feature/ad
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| e3d75fbfa8 | |||
| 9872c1b88b | |||
| fe4c0696e8 | |||
| 3b752f182f | |||
| 4a2ed52a2f | |||
| 5277896bdc | |||
| 9a02f0e4d6 | |||
| 4cd89c6a83 | |||
| 0bd46f2594 | |||
| 7bbb8ebfe5 | |||
| 035da054d6 | |||
| a025bd87c1 | |||
| ff404ec105 | |||
| 8d1e218eb9 | |||
| db10d185da | |||
| cbeeebd5a6 | |||
| 0f7567ec67 | 
| @@ -1,11 +1,6 @@ | |||||||
| using System; | using System.Text.Json; | ||||||
| using System.Collections.Generic; |  | ||||||
| using System.Linq; |  | ||||||
| using System.Text; |  | ||||||
| using System.Text.Json; |  | ||||||
| using System.Threading.Tasks; |  | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     public class BaseAdapterResponse |     public class BaseAdapterResponse | ||||||
|     { |     { | ||||||
|   | |||||||
							
								
								
									
										32
									
								
								Core.Thalos.BuildingBlocks/Adapters/CatalogAdapter.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								Core.Thalos.BuildingBlocks/Adapters/CatalogAdapter.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,32 @@ | |||||||
|  | using Core.Blueprint.Mongo; | ||||||
|  | using MongoDB.Bson; | ||||||
|  | using MongoDB.Bson.Serialization.Attributes; | ||||||
|  |  | ||||||
|  | namespace Core.Thalos.BuildingBlocks.Adapters | ||||||
|  | { | ||||||
|  |     [CollectionAttributeName("Catalogs")] | ||||||
|  |     public class CatalogAdapter : Document | ||||||
|  |     { | ||||||
|  |         [BsonElement("name")] | ||||||
|  |         public string Name { get; set; } = null!; | ||||||
|  |  | ||||||
|  |         [BsonElement("key")] | ||||||
|  |         public string? Key { get; set; } = null!; | ||||||
|  |  | ||||||
|  |         [BsonElement("description")] | ||||||
|  |         public string? Description { get; set; } | ||||||
|  |  | ||||||
|  |         public IEnumerable<CatalogValue>? Values { get; set; } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public class CatalogValue | ||||||
|  |     { | ||||||
|  |         [BsonId] | ||||||
|  |         [BsonElement("_id")] | ||||||
|  |         [BsonRepresentation(BsonType.ObjectId)] | ||||||
|  |         public string _Id { get; set; } = null!; | ||||||
|  |  | ||||||
|  |         [BsonElement("value")] | ||||||
|  |         public string Value { get; set; } = null!; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -5,12 +5,11 @@ | |||||||
| // *********************************************************************** | // *********************************************************************** | ||||||
|  |  | ||||||
| using Core.Blueprint.Mongo; | using Core.Blueprint.Mongo; | ||||||
| using Core.Thalos.Adapters.Common.Enums; |  | ||||||
| using MongoDB.Bson; | using MongoDB.Bson; | ||||||
| using MongoDB.Bson.Serialization.Attributes; | using MongoDB.Bson.Serialization.Attributes; | ||||||
| using System.Text.Json.Serialization; | using System.Text.Json.Serialization; | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Adapter for representing a module. |     /// Adapter for representing a module. | ||||||
|   | |||||||
| @@ -5,12 +5,11 @@ | |||||||
| // *********************************************************************** | // *********************************************************************** | ||||||
|  |  | ||||||
| using Core.Blueprint.Mongo; | using Core.Blueprint.Mongo; | ||||||
| using Core.Thalos.Adapters.Common.Constants; |  | ||||||
| using MongoDB.Bson; | using MongoDB.Bson; | ||||||
| using MongoDB.Bson.Serialization.Attributes; | using MongoDB.Bson.Serialization.Attributes; | ||||||
| using System.Text.Json.Serialization; | using System.Text.Json.Serialization; | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Adapter for representing a permission. |     /// Adapter for representing a permission. | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| using Microsoft.AspNetCore.Authorization; | using Microsoft.AspNetCore.Authorization; | ||||||
| 
 | 
 | ||||||
| namespace Core.Thalos.Adapters.Handlers.Adapters | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     public class PermissionsAuthorizationAdapter : IAuthorizationRequirement |     public class PermissionsAuthorizationAdapter : IAuthorizationRequirement | ||||||
|     { |     { | ||||||
| @@ -5,12 +5,11 @@ | |||||||
| // *********************************************************************** | // *********************************************************************** | ||||||
|  |  | ||||||
| using Core.Blueprint.Mongo; | using Core.Blueprint.Mongo; | ||||||
| using Core.Thalos.Adapters.Common.Enums; |  | ||||||
| using MongoDB.Bson; | using MongoDB.Bson; | ||||||
| using MongoDB.Bson.Serialization.Attributes; | using MongoDB.Bson.Serialization.Attributes; | ||||||
| using System.Text.Json.Serialization; | using System.Text.Json.Serialization; | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Adapter representing a role. |     /// Adapter representing a role. | ||||||
|   | |||||||
							
								
								
									
										53
									
								
								Core.Thalos.BuildingBlocks/Adapters/TenantAdapter.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								Core.Thalos.BuildingBlocks/Adapters/TenantAdapter.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,53 @@ | |||||||
|  | using Core.Blueprint.Mongo; | ||||||
|  | using MongoDB.Bson; | ||||||
|  | using MongoDB.Bson.Serialization.Attributes; | ||||||
|  |  | ||||||
|  | namespace Core.Thalos.BuildingBlocks | ||||||
|  | { | ||||||
|  |     [CollectionAttributeName("Tenants")] | ||||||
|  |     public class TenantAdapter : Document | ||||||
|  |     { | ||||||
|  |         [BsonElement("name")] | ||||||
|  |         public string Name { get; set; } = null!; | ||||||
|  |  | ||||||
|  |         [BsonElement("taxIdentifier")] | ||||||
|  |         public string TaxIdentifier { get; set; } = null!; | ||||||
|  |  | ||||||
|  |         [BsonElement("addressLine1")] | ||||||
|  |         public string AddressLine1 { get; set; } = null!; | ||||||
|  |  | ||||||
|  |         [BsonElement("addressLine2")] | ||||||
|  |         [BsonIgnoreIfNull] | ||||||
|  |         public string? AddressLine2 { get; set; } | ||||||
|  |  | ||||||
|  |         [BsonElement("city")] | ||||||
|  |         public string City { get; set; } = null!; | ||||||
|  |  | ||||||
|  |         [BsonElement("state")] | ||||||
|  |         public string State { get; set; } = null!; | ||||||
|  |  | ||||||
|  |         [BsonElement("country")] | ||||||
|  |         public string Country { get; set; } = null!; | ||||||
|  |  | ||||||
|  |         [BsonElement("postalCode")] | ||||||
|  |         public string PostalCode { get; set; } = null!; | ||||||
|  |  | ||||||
|  |         [BsonElement("contactEmail")] | ||||||
|  |         public string ContactEmail { get; set; } = null!; | ||||||
|  |  | ||||||
|  |         [BsonElement("contactPhone")] | ||||||
|  |         public string ContactPhone { get; set; } = null!; | ||||||
|  |  | ||||||
|  |         [BsonElement("website")] | ||||||
|  |         [BsonIgnoreIfNull] | ||||||
|  |         public string? Website { get; set; } | ||||||
|  |  | ||||||
|  |         [BsonElement("connectionString")] | ||||||
|  |         [BsonIgnoreIfNull] | ||||||
|  |         public string? ConnectionString { get; set; } | ||||||
|  |  | ||||||
|  |         [BsonElement("isolated")] | ||||||
|  |         public bool Isolated { get; set; } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  | } | ||||||
| @@ -4,13 +4,14 @@ | |||||||
| // </copyright> | // </copyright> | ||||||
| // *********************************************************************** | // *********************************************************************** | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     public class TokenAdapter |     public class TokenAdapter | ||||||
|     { |     { | ||||||
|         public UserAdapter? User { get; set; } |         public UserAdapter? User { get; set; } | ||||||
|  |  | ||||||
|         public RoleAdapter? Role { get; set; } |         public RoleAdapter? Role { get; set; } | ||||||
|  |         public TenantAdapter? Tenant { get; set; } | ||||||
|  |  | ||||||
|         public IEnumerable<PermissionAdapter>? Permissions { get; set; } |         public IEnumerable<PermissionAdapter>? Permissions { get; set; } | ||||||
|         public IEnumerable<ModuleAdapter> Modules { get; set; } = null!; |         public IEnumerable<ModuleAdapter> Modules { get; set; } = null!; | ||||||
|   | |||||||
| @@ -8,7 +8,7 @@ using MongoDB.Bson; | |||||||
| using MongoDB.Bson.Serialization.Attributes; | using MongoDB.Bson.Serialization.Attributes; | ||||||
| using System.Text.Json.Serialization; | using System.Text.Json.Serialization; | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Adapter representing a user. |     /// Adapter representing a user. | ||||||
| @@ -16,14 +16,6 @@ namespace Core.Thalos.Adapters | |||||||
|     [CollectionAttributeName("Users")] |     [CollectionAttributeName("Users")] | ||||||
|     public class UserAdapter : Document |     public class UserAdapter : Document | ||||||
|     { |     { | ||||||
|         /// <summary> |  | ||||||
|         /// Gets or sets the guid of the user. |  | ||||||
|         /// </summary> |  | ||||||
|         [BsonElement("guid")] |  | ||||||
|         [BsonRepresentation(BsonType.String)] |  | ||||||
|         [JsonPropertyName("guid")] |  | ||||||
|         public string? Guid { get; set; } |  | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Gets or sets the email address of the user. |         /// Gets or sets the email address of the user. | ||||||
|         /// </summary> |         /// </summary> | ||||||
| @@ -64,6 +56,14 @@ namespace Core.Thalos.Adapters | |||||||
|         [JsonPropertyName("displayName")] |         [JsonPropertyName("displayName")] | ||||||
|         public string? DisplayName { get; set; } |         public string? DisplayName { get; set; } | ||||||
|  |  | ||||||
|  |         /// <summary>     | ||||||
|  |         /// Gets or sets the Tenand ID of the user. | ||||||
|  |         /// </summary> | ||||||
|  |         [BsonElement("tenantId")] | ||||||
|  |         [BsonRepresentation(BsonType.ObjectId)] | ||||||
|  |         [JsonPropertyName("tenantId")] | ||||||
|  |         public string TenantId { get; set; } = null!; | ||||||
|  |  | ||||||
|         /// <summary>     |         /// <summary>     | ||||||
|         /// Gets or sets the role ID of the user. |         /// Gets or sets the role ID of the user. | ||||||
|         /// </summary> |         /// </summary> | ||||||
| @@ -72,28 +72,6 @@ namespace Core.Thalos.Adapters | |||||||
|         [JsonPropertyName("roleId")] |         [JsonPropertyName("roleId")] | ||||||
|         public string RoleId { get; set; } = null!; |         public string RoleId { get; set; } = null!; | ||||||
|  |  | ||||||
|         /// <summary> |  | ||||||
|         /// Gets or sets the array of companies associated with the user. |  | ||||||
|         /// </summary> |  | ||||||
|         [BsonElement("companies")] |  | ||||||
|         [JsonPropertyName("companies")] |  | ||||||
|         public string[] Companies { get; set; } = null!; |  | ||||||
|  |  | ||||||
|         /// <summary> |  | ||||||
|         /// Gets or sets the array of projects associated with the user. |  | ||||||
|         /// </summary> |  | ||||||
|         [BsonElement("projects")] |  | ||||||
|         [JsonPropertyName("projects")] |  | ||||||
|         public string[]? Projects { get; set; } |  | ||||||
|  |  | ||||||
|         /// <summary> |  | ||||||
|         /// Gets or sets the boolean of the consent form accepted by the user. |  | ||||||
|         /// </summary> |  | ||||||
|         [BsonElement("consentFormAccepted")] |  | ||||||
|         [JsonPropertyName("consentFormAccepted")] |  | ||||||
|         [BsonIgnoreIfNull] |  | ||||||
|         public bool ConsentFormAccepted { get; set; } |  | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Gets or sets the timestamp of the last login of the user. |         /// Gets or sets the timestamp of the last login of the user. | ||||||
|         /// </summary> |         /// </summary> | ||||||
|   | |||||||
| @@ -6,7 +6,7 @@ | |||||||
| 
 | 
 | ||||||
| using System.Text.Json.Serialization; | using System.Text.Json.Serialization; | ||||||
| 
 | 
 | ||||||
| namespace Core.Thalos.Adapters | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Adapter representing a user. |     /// Adapter representing a user. | ||||||
| @@ -1,4 +1,4 @@ | |||||||
| namespace Core.Thalos.Adapters | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     public class Permission |     public class Permission | ||||||
|     { |     { | ||||||
|   | |||||||
| @@ -2,7 +2,7 @@ | |||||||
| using Microsoft.AspNetCore.Mvc; | using Microsoft.AspNetCore.Mvc; | ||||||
| using Microsoft.AspNetCore.Mvc.Filters; | using Microsoft.AspNetCore.Mvc.Filters; | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters.Attributes | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Custom authorization attribute that checks if the user has any of the required permissions. |     /// Custom authorization attribute that checks if the user has any of the required permissions. | ||||||
|   | |||||||
| @@ -1,14 +1,13 @@ | |||||||
| using Core.Thalos.BuildingBlocks.Authentication.Helpers; | using Google.Apis.Auth.OAuth2; | ||||||
| using Google.Apis.Auth.OAuth2; |  | ||||||
| using Google.Apis.Auth.OAuth2.Flows; | using Google.Apis.Auth.OAuth2.Flows; | ||||||
| using Microsoft.Extensions.Configuration; | using Microsoft.Extensions.Configuration; | ||||||
|  |  | ||||||
| namespace Core.Thalos.BuildingBlocks.Authentication.Authorization.Google | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     public class GoogleAuthorization( |     public class GoogleAuthorization( | ||||||
|         IGoogleAuthHelper googleHelper, IConfiguration config) : IGoogleAuthorization |         IGoogleAuthHelper googleHelper, IConfiguration config, GoogleAuthSettings googlesettings) : IGoogleAuthorization | ||||||
|     { |     { | ||||||
|         private string RedirectUrl = config["Authentication:Google:RedirectUri"]!; |         private string RedirectUrl = googlesettings.RedirectUri ?? string.Empty; | ||||||
|  |  | ||||||
|         public async Task<UserCredential> ExchangeCodeForToken(string code) |         public async Task<UserCredential> ExchangeCodeForToken(string code) | ||||||
|         { |         { | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| using Google.Apis.Auth.OAuth2; | using Google.Apis.Auth.OAuth2; | ||||||
|  |  | ||||||
| namespace Core.Thalos.BuildingBlocks.Authentication.Authorization.Google | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     public interface IGoogleAuthorization |     public interface IGoogleAuthorization | ||||||
|     { |     { | ||||||
|   | |||||||
| @@ -1,9 +0,0 @@ | |||||||
| using Microsoft.AspNetCore.Authorization; |  | ||||||
|  |  | ||||||
| namespace Core.Thalos.BuildingBlocks.Authentication.Authorization.Jwt |  | ||||||
| { |  | ||||||
|     public class PermissionAuthorizationAdapter(string[] permission) : IAuthorizationRequirement |  | ||||||
|     { |  | ||||||
|         public string[] Permission { get; } = permission ?? throw new ArgumentNullException(nameof(permission)); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,18 +0,0 @@ | |||||||
| using Microsoft.AspNetCore.Authorization; |  | ||||||
|  |  | ||||||
| namespace Core.Thalos.BuildingBlocks.Authentication.Authorization.Jwt |  | ||||||
| { |  | ||||||
|     public class PermissionsAuthorizationHandler : AuthorizationHandler<PermissionAuthorizationAdapter> |  | ||||||
|     { |  | ||||||
|         protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, PermissionAuthorizationAdapter requirement) |  | ||||||
|         { |  | ||||||
|             PermissionAuthorizationAdapter requirement2 = requirement; |  | ||||||
|             if (context.User.Claims.Any((x) => x.Type == "roleId" && requirement2.Permission.Contains(x.Value))) |  | ||||||
|             { |  | ||||||
|                 context.Succeed(requirement2); |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             return Task.CompletedTask; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,92 +0,0 @@ | |||||||
| using Core.Thalos.Adapters.Common.Constants; |  | ||||||
| using Core.Thalos.Adapters.Options; |  | ||||||
| using Core.Thalos.BuildingBlocks.Authentication.Authorization.Google; |  | ||||||
| using Core.Thalos.BuildingBlocks.Authentication.Authorization.Jwt; |  | ||||||
| using Core.Thalos.BuildingBlocks.Authentication.Handlers; |  | ||||||
| using Core.Thalos.BuildingBlocks.Authentication.Helpers; |  | ||||||
| using Core.Thalos.BuildingBlocks.Common.Constants; |  | ||||||
| using Microsoft.AspNetCore.Authentication; |  | ||||||
| using Microsoft.AspNetCore.Authentication.JwtBearer; |  | ||||||
| using Microsoft.AspNetCore.Authorization; |  | ||||||
| using Microsoft.Extensions.Configuration; |  | ||||||
| using Microsoft.Extensions.DependencyInjection; |  | ||||||
| using Microsoft.IdentityModel.Tokens; |  | ||||||
| using System.Text; |  | ||||||
|  |  | ||||||
| namespace Core.Thalos.BuildingBlocks.Authentication.Extensions |  | ||||||
| { |  | ||||||
|     /// <summary> |  | ||||||
|     /// Extension methods for configuring authentication with various Google and JWT setups. |  | ||||||
|     /// </summary> |  | ||||||
|     public static class AuthenticationExtension |  | ||||||
|     { |  | ||||||
|         /// <summary> |  | ||||||
|         /// Configures authentication using Google Auth for an API that requires downstream API access. |  | ||||||
|         /// </summary> |  | ||||||
|         /// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param> |  | ||||||
|         /// <param name="configuration">The <see cref="IConfiguration"/> containing Google Auth configuration settings.</param> |  | ||||||
|         public static void ConfigureAuthentication(this IServiceCollection services, IConfiguration configuration) |  | ||||||
|         { |  | ||||||
|             var secretKey = configuration.GetSection("SecretKey").Value ?? string.Empty; |  | ||||||
|             var _signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secretKey)); |  | ||||||
|             var jwtAppSettingsOptions = configuration.GetSection(nameof(JwtIssuerOptions)); |  | ||||||
|             var jwtIssuerOptions = jwtAppSettingsOptions.Get<JwtIssuerOptions>(); |  | ||||||
|  |  | ||||||
|             var googleClientId = configuration["Authentication:Google:ClientId"]; |  | ||||||
|             var googleClientSecret = configuration["Authentication:Google:ClientSecret"]; |  | ||||||
|             var redirectUri = configuration["Authentication:Google:RedirectUri"]; |  | ||||||
|  |  | ||||||
|             services.AddAuthentication(options => |  | ||||||
|             { |  | ||||||
|                 options.DefaultAuthenticateScheme = Schemes.GoogleScheme; |  | ||||||
|                 options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; |  | ||||||
|             }) |  | ||||||
|             .AddScheme<AuthenticationSchemeOptions, |  | ||||||
|             GoogleAccessTokenAuthenticationHandler>(Schemes.GoogleScheme, null) |  | ||||||
|             .AddGoogle(options => |  | ||||||
|             { |  | ||||||
|                 options.ClientId = googleClientId!; |  | ||||||
|                 options.ClientSecret = googleClientSecret!; |  | ||||||
|                 //options.SaveTokens = true; |  | ||||||
|                 options.CallbackPath = $"/{redirectUri}"; |  | ||||||
|             }) |  | ||||||
|             .AddJwtBearer(Schemes.DefaultScheme, x => |  | ||||||
|             { |  | ||||||
|                 x.TokenValidationParameters = new TokenValidationParameters |  | ||||||
|                 { |  | ||||||
|                     ValidIssuer = jwtIssuerOptions?.Issuer, |  | ||||||
|                     ValidateIssuer = true, |  | ||||||
|                     ValidateAudience = true, |  | ||||||
|                     ValidateLifetime = true, |  | ||||||
|                     ValidateIssuerSigningKey = true, |  | ||||||
|                     ValidAudience = jwtIssuerOptions?.Audience, |  | ||||||
|                     IssuerSigningKey = new SymmetricSecurityKey( |  | ||||||
|                         Encoding.UTF8.GetBytes(configuration["SecretKey"] ?? string.Empty)) |  | ||||||
|                 }; |  | ||||||
|             }); |  | ||||||
|  |  | ||||||
|             services.Configure<JwtIssuerOptions>(options => |  | ||||||
|             { |  | ||||||
|                 options.Issuer = jwtAppSettingsOptions[nameof(jwtIssuerOptions.Issuer)] ?? string.Empty; |  | ||||||
|                 options.Audience = jwtAppSettingsOptions[nameof(jwtIssuerOptions.Audience)] ?? string.Empty; |  | ||||||
|                 options.SigningCredentials = new SigningCredentials(_signingKey, SecurityAlgorithms.HmacSha256); |  | ||||||
|             }); |  | ||||||
|  |  | ||||||
|             services.AddSingleton(jwtAppSettingsOptions); |  | ||||||
|  |  | ||||||
|             string[] roles = { Roles.Guest, Roles.Admin }; |  | ||||||
|  |  | ||||||
|             services.AddAuthorization(options => |  | ||||||
|             { |  | ||||||
|                 options.AddPolicy(Policies.Read, policy => policy.Requirements.Add(new PermissionAuthorizationAdapter(roles))); |  | ||||||
|                 options.AddPolicy(Policies.Write, policy => policy.Requirements.Add(new PermissionAuthorizationAdapter(roles))); |  | ||||||
|             }); |  | ||||||
|  |  | ||||||
|             services.AddTransient<IAuthorizationHandler, PermissionsAuthorizationHandler>(); |  | ||||||
|  |  | ||||||
|             services.AddAuthorization(); |  | ||||||
|             services.AddScoped<IGoogleAuthHelper, GoogleAuthHelper>(); |  | ||||||
|             services.AddScoped<IGoogleAuthorization, GoogleAuthorization>(); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,84 +0,0 @@ | |||||||
| using Microsoft.Extensions.Configuration; |  | ||||||
| using Microsoft.Extensions.DependencyInjection; |  | ||||||
| using Microsoft.OpenApi.Any; |  | ||||||
| using Microsoft.OpenApi.Interfaces; |  | ||||||
| using Microsoft.OpenApi.Models; |  | ||||||
|  |  | ||||||
| namespace Core.Thalos.BuildingBlocks.Authentication.Extensions |  | ||||||
| { |  | ||||||
|     public static class SwaggerExtension |  | ||||||
|     { |  | ||||||
|         public static void AddSwaggerGen(this IServiceCollection services, IConfiguration configuration) |  | ||||||
|         { |  | ||||||
|             services.AddSwaggerGen(opts => |  | ||||||
|             { |  | ||||||
|                 const string schemeName = "oauth2"; |  | ||||||
|  |  | ||||||
|                 opts.AddSecurityDefinition(schemeName, new OpenApiSecurityScheme |  | ||||||
|                 { |  | ||||||
|                     Type = SecuritySchemeType.OAuth2, |  | ||||||
|                     Scheme = "bearer", |  | ||||||
|                     BearerFormat = "JWT", |  | ||||||
|                     Name = "Authorization", |  | ||||||
|                     In = ParameterLocation.Header, |  | ||||||
|  |  | ||||||
|                     Extensions = new Dictionary<string, IOpenApiExtension> |  | ||||||
|                     { |  | ||||||
|                         ["x-tokenName"] = new OpenApiString("id_token") |  | ||||||
|                     }, |  | ||||||
|  |  | ||||||
|                     Flows = new OpenApiOAuthFlows |  | ||||||
|                     { |  | ||||||
|                         AuthorizationCode = new OpenApiOAuthFlow |  | ||||||
|                         { |  | ||||||
|                             AuthorizationUrl = new Uri("https://accounts.google.com/o/oauth2/v2/auth"), |  | ||||||
|                             TokenUrl = new Uri("https://oauth2.googleapis.com/token"), |  | ||||||
|                             Scopes = new Dictionary<string, string> |  | ||||||
|                             { |  | ||||||
|                                 { "openid",  "OpenID Connect" }, |  | ||||||
|                                 { "email",   "Access email"   }, |  | ||||||
|                                 { "profile", "Access profile" } |  | ||||||
|                             } |  | ||||||
|                         } |  | ||||||
|                     } |  | ||||||
|                 }); |  | ||||||
|  |  | ||||||
|                 // every operation requires the scheme |  | ||||||
|                 opts.AddSecurityRequirement(new OpenApiSecurityRequirement |  | ||||||
|                 { |  | ||||||
|                     [new OpenApiSecurityScheme |  | ||||||
|                         { |  | ||||||
|                             Reference = new OpenApiReference |  | ||||||
|                             { Type = ReferenceType.SecurityScheme, Id = schemeName } |  | ||||||
|                         } |  | ||||||
|                     ] = new[] { "openid", "email", "profile" } |  | ||||||
|                 }); |  | ||||||
|  |  | ||||||
|                 opts.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme |  | ||||||
|                 { |  | ||||||
|                     Description = "JWT Authorization header using the Bearer scheme", |  | ||||||
|                     Name = "Authorization", |  | ||||||
|                     In = ParameterLocation.Header, |  | ||||||
|                     Type = SecuritySchemeType.Http, |  | ||||||
|                     Scheme = "bearer", |  | ||||||
|                     BearerFormat = "JWT" |  | ||||||
|                 }); |  | ||||||
|  |  | ||||||
|                 opts.AddSecurityRequirement(new OpenApiSecurityRequirement |  | ||||||
|                 { |  | ||||||
|                     { |  | ||||||
|                         new OpenApiSecurityScheme |  | ||||||
|                         { |  | ||||||
|                             Reference = new OpenApiReference |  | ||||||
|                             { |  | ||||||
|                                 Type = ReferenceType.SecurityScheme, |  | ||||||
|                                 Id = "Bearer" |  | ||||||
|                             } |  | ||||||
|                         }, |  | ||||||
|                         Array.Empty<string>() |  | ||||||
|                     } |  | ||||||
|                 }); |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -6,7 +6,7 @@ | |||||||
|  |  | ||||||
| using System.Text.Json.Serialization; | using System.Text.Json.Serialization; | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters.Common.Constants | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Specifies different access level for a permission. |     /// Specifies different access level for a permission. | ||||||
|   | |||||||
| @@ -3,7 +3,7 @@ | |||||||
| //     AgileWebs | //     AgileWebs | ||||||
| // </copyright> | // </copyright> | ||||||
| // *********************************************************************** | // *********************************************************************** | ||||||
| namespace Core.Thalos.Adapters.Common.Constants | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Constants for Azure Active Directory. |     /// Constants for Azure Active Directory. | ||||||
|   | |||||||
| @@ -3,7 +3,7 @@ | |||||||
| //     AgileWebs | //     AgileWebs | ||||||
| // </copyright> | // </copyright> | ||||||
| // *********************************************************************** | // *********************************************************************** | ||||||
| namespace Core.Thalos.Adapters.Common.Constants | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Constants for claims used in JWT tokens. |     /// Constants for claims used in JWT tokens. | ||||||
| @@ -23,10 +23,20 @@ namespace Core.Thalos.Adapters.Common.Constants | |||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Claim name for user's ID. |         /// Claim name for user's ID. | ||||||
|         /// </summary> |         /// </summary> | ||||||
|         public const string Id = "id"; |         public const string Id = "_id"; | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Claim name for user's role ID. |         /// Claim name for user's tenant name. | ||||||
|  |         /// </summary> | ||||||
|  |         public const string Tenant = "tenant"; | ||||||
|  |  | ||||||
|  |         /// <summary> | ||||||
|  |         /// Claim name for user's tenant identifier. | ||||||
|  |         /// </summary> | ||||||
|  |         public const string TenantId = "tenantId"; | ||||||
|  |  | ||||||
|  |         /// <summary> | ||||||
|  |         /// Claim name for user's role name. | ||||||
|         /// </summary> |         /// </summary> | ||||||
|         public const string Role = "role"; |         public const string Role = "role"; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| namespace Core.Thalos.Adapters.Common.Constants | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     public static class CollectionNames |     public static class CollectionNames | ||||||
|     { |     { | ||||||
|   | |||||||
| @@ -4,7 +4,7 @@ | |||||||
| // </copyright> | // </copyright> | ||||||
| // *********************************************************************** | // *********************************************************************** | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters.Common.Constants | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Constants of the environment variables for this service. |     /// Constants of the environment variables for this service. | ||||||
|   | |||||||
| @@ -3,7 +3,7 @@ | |||||||
| //     AgileWebs | //     AgileWebs | ||||||
| // </copyright> | // </copyright> | ||||||
| // *********************************************************************** | // *********************************************************************** | ||||||
| namespace Core.Thalos.Adapters.Common.Constants | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Constants for Key Vault configuration. |     /// Constants for Key Vault configuration. | ||||||
|   | |||||||
| @@ -6,7 +6,7 @@ | |||||||
|  |  | ||||||
| using System.Globalization; | using System.Globalization; | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters.Common.Constants | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Constants for the mime types. |     /// Constants for the mime types. | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| using System; | using System; | ||||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||||
| namespace Core.Thalos.BuildingBlocks.Common.Constants | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Constants for policy. |     /// Constants for policy. | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| namespace Core.Thalos.BuildingBlocks.Common.Constants | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     public class Roles |     public class Roles | ||||||
|     { |     { | ||||||
|   | |||||||
| @@ -4,7 +4,7 @@ | |||||||
| // </copyright> | // </copyright> | ||||||
| // *********************************************************************** | // *********************************************************************** | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters.Common.Constants | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Constants of the routes of this service. |     /// Constants of the routes of this service. | ||||||
| @@ -24,7 +24,7 @@ namespace Core.Thalos.Adapters.Common.Constants | |||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// The identifier route. |         /// The identifier route. | ||||||
|         /// </summary> |         /// </summary> | ||||||
|         public const string Id = "{id}"; |         public const string Id = "{_id}"; | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// The Authentication route. |         /// The Authentication route. | ||||||
| @@ -74,7 +74,7 @@ namespace Core.Thalos.Adapters.Common.Constants | |||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// The ChangeStatus route. |         /// The ChangeStatus route. | ||||||
|         /// </summary> |         /// </summary> | ||||||
|         public const string ChangeStatus = "{id}/{newStatus}/ChangeStatus"; |         public const string ChangeStatus = "{_id}/{newStatus}/ChangeStatus"; | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// The AddCompany route. |         /// The AddCompany route. | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| namespace Core.Thalos.Adapters.Common.Constants | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Constants for schemes. |     /// Constants for schemes. | ||||||
|   | |||||||
| @@ -3,7 +3,7 @@ | |||||||
| //     AgileWebs | //     AgileWebs | ||||||
| // </copyright> | // </copyright> | ||||||
| // *********************************************************************** | // *********************************************************************** | ||||||
| namespace Core.Thalos.Adapters.Common.Constants | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Constants for secrets in azure key vault. |     /// Constants for secrets in azure key vault. | ||||||
| @@ -23,12 +23,12 @@ namespace Core.Thalos.Adapters.Common.Constants | |||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// The Issuer parameter for JWT settings. |         /// The Issuer parameter for JWT settings. | ||||||
|         /// </summary> |         /// </summary> | ||||||
|         public const string Issuer = "Issuer"; |         public const string Issuer = "JWTIssuer"; | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// The Audience parameter for JWT settings. |         /// The Audience parameter for JWT settings. | ||||||
|         /// </summary> |         /// </summary> | ||||||
|         public const string Audience = "Audience"; |         public const string Audience = "JWTAudience"; | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// The TokenExpirationInMinutes parameter for JWT settings. |         /// The TokenExpirationInMinutes parameter for JWT settings. | ||||||
| @@ -55,5 +55,8 @@ namespace Core.Thalos.Adapters.Common.Constants | |||||||
|         public const string ThalosAppScope = "Swagger:Scope"; |         public const string ThalosAppScope = "Swagger:Scope"; | ||||||
|         public const string PrivateKey = "JwtTokenPrivateKey"; |         public const string PrivateKey = "JwtTokenPrivateKey"; | ||||||
|         public const string PublicKey = "JwtTokenPublicKey"; |         public const string PublicKey = "JwtTokenPublicKey"; | ||||||
|  |         public const string GoogleClientId = "GoogleClientId"; | ||||||
|  |         public const string GoogleClientSecret = "GoogleClientSecret"; | ||||||
|  |         public const string GoogleRedirectUri = "GoogleRedirectUri"; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -6,7 +6,7 @@ | |||||||
|  |  | ||||||
| using System.Text.Json.Serialization; | using System.Text.Json.Serialization; | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters.Common.Enums | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Defines the applications associated with the role. |     /// Defines the applications associated with the role. | ||||||
|   | |||||||
| @@ -6,7 +6,7 @@ | |||||||
|  |  | ||||||
| using System.Text.Json.Serialization; | using System.Text.Json.Serialization; | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters.Common.Enums | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Defines the status of an entity. |     /// Defines the status of an entity. | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| using Google.Apis.Auth.OAuth2; | using Google.Apis.Auth.OAuth2; | ||||||
| 
 | 
 | ||||||
| namespace Core.Thalos.BuildingBlocks.Authentication.Helpers | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     public interface IGoogleAuthHelper |     public interface IGoogleAuthHelper | ||||||
|     { |     { | ||||||
| @@ -4,7 +4,7 @@ | |||||||
| // </copyright> | // </copyright> | ||||||
| // *********************************************************************** | // *********************************************************************** | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters.Contracts | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Interface for token provider. |     /// Interface for token provider. | ||||||
|   | |||||||
| @@ -7,7 +7,7 @@ | |||||||
| using Microsoft.AspNetCore.Http; | using Microsoft.AspNetCore.Http; | ||||||
| using Microsoft.AspNetCore.Mvc; | using Microsoft.AspNetCore.Mvc; | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters.Contracts | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Interface for authenticacion service. |     /// Interface for authenticacion service. | ||||||
|   | |||||||
| @@ -14,12 +14,14 @@ | |||||||
|  |  | ||||||
|   <ItemGroup> |   <ItemGroup> | ||||||
|     <PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" /> |     <PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" /> | ||||||
|  |     <PackageReference Include="Core.Blueprint.KeyVault" Version="1.0.3" /> | ||||||
|     <PackageReference Include="Core.Blueprint.Mongo" Version="1.0.0" /> |     <PackageReference Include="Core.Blueprint.Mongo" Version="1.0.0" /> | ||||||
|     <PackageReference Include="Google.Apis.Auth" Version="1.70.0" /> |     <PackageReference Include="Google.Apis.Auth" Version="1.70.0" /> | ||||||
|     <PackageReference Include="Google.Apis.Oauth2.v2" Version="1.68.0.1869" /> |     <PackageReference Include="Google.Apis.Oauth2.v2" Version="1.68.0.1869" /> | ||||||
|     <PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="8.0.18" /> |     <PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="8.0.18" /> | ||||||
|     <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.10" /> |     <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.10" /> | ||||||
|     <PackageReference Include="Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="8.2.0" /> |     <PackageReference Include="Microsoft.Extensions.Configuration.AzureAppConfiguration" Version="8.2.0" /> | ||||||
|  |     <PackageReference Include="Microsoft.Extensions.Options" Version="9.0.7" /> | ||||||
|     <PackageReference Include="Microsoft.Identity.Web" Version="3.9.1" /> |     <PackageReference Include="Microsoft.Identity.Web" Version="3.9.1" /> | ||||||
|     <PackageReference Include="Microsoft.Identity.Web.MicrosoftGraph" Version="3.9.1" /> |     <PackageReference Include="Microsoft.Identity.Web.MicrosoftGraph" Version="3.9.1" /> | ||||||
|     <PackageReference Include="MongoDB.Bson" Version="3.4.0" /> |     <PackageReference Include="MongoDB.Bson" Version="3.4.0" /> | ||||||
| @@ -33,4 +35,8 @@ | |||||||
|     <PackageReference Include="System.Text.Json" Version="9.0.5" /> |     <PackageReference Include="System.Text.Json" Version="9.0.5" /> | ||||||
|   </ItemGroup> |   </ItemGroup> | ||||||
|  |  | ||||||
|  |   <ItemGroup> | ||||||
|  |     <Folder Include="Handlers\Adapters\" /> | ||||||
|  |   </ItemGroup> | ||||||
|  |  | ||||||
| </Project> | </Project> | ||||||
|   | |||||||
| @@ -4,11 +4,7 @@ | |||||||
| // </copyright> | // </copyright> | ||||||
| // *********************************************************************** | // *********************************************************************** | ||||||
|  |  | ||||||
| using Core.Thalos.Adapters.Common.Constants; | using Microsoft.AspNetCore.Authentication; | ||||||
| using Core.Thalos.Adapters.Contracts; |  | ||||||
| using Core.Thalos.Adapters.Handlers; |  | ||||||
| using Core.Thalos.Adapters.Options; |  | ||||||
| using Core.Thalos.Adapters.Services; |  | ||||||
| using Microsoft.AspNetCore.Authentication.JwtBearer; | using Microsoft.AspNetCore.Authentication.JwtBearer; | ||||||
| using Microsoft.AspNetCore.Authorization; | using Microsoft.AspNetCore.Authorization; | ||||||
| using Microsoft.Extensions.Configuration; | using Microsoft.Extensions.Configuration; | ||||||
| @@ -17,7 +13,7 @@ using Microsoft.Identity.Web; | |||||||
| using Microsoft.IdentityModel.Tokens; | using Microsoft.IdentityModel.Tokens; | ||||||
| using System.Security.Cryptography; | using System.Security.Cryptography; | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters.Extensions | namespace Core.Thalos.BuildingBlocks.Configuration | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Extension methods for configuring authentication with various Azure AD setups. |     /// Extension methods for configuring authentication with various Azure AD setups. | ||||||
| @@ -33,36 +29,38 @@ namespace Core.Thalos.Adapters.Extensions | |||||||
|         { |         { | ||||||
|             var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? string.Empty; |             var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? string.Empty; | ||||||
|  |  | ||||||
|             var azureAdInMemorySettings = new Dictionary<string, string?> |             var identityProviders = new IdentityProviders(); | ||||||
|  |             configuration.GetSection("IdentityProviders").Bind(identityProviders); | ||||||
|  |  | ||||||
|  |             AddCustomAuthentication(services, authSettings.Token); | ||||||
|  |  | ||||||
|  |             if (identityProviders.Azure) | ||||||
|  |                 AddAzureAuthentication(authSettings, configuration, services); | ||||||
|  |  | ||||||
|  |             if (identityProviders.Google) | ||||||
|  |                 AddGoogleAuthentication(services, authSettings.Google); | ||||||
|  |  | ||||||
|  |             services.AddAuthorization(); | ||||||
|  |             services.AddTransient<IAuthorizationHandler, PermissionsAuthorizationHandler>(); | ||||||
|  |             services.AddTransient<ITokenService, TokenService>(); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         public static void AddCustomAuthentication(IServiceCollection services, TokenAuthSettings tokenAuthSettings) | ||||||
|         { |         { | ||||||
|                 { "AzureAdB2C:Instance",  authSettings.AzureADInstance ?? string.Empty }, |  | ||||||
|                 { "AzureAdB2C:TenantId", authSettings.AzureADTenantId ?? string.Empty }, |  | ||||||
|                 { "AzureAdB2C:ClientId", authSettings.AzureADClientId ?? string.Empty }, |  | ||||||
|                 { "AzureAdB2C:ClientSecret", authSettings.AzureADClientSecret ?? string.Empty } |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             var configurationBuilder = new ConfigurationBuilder() |  | ||||||
|                 .AddConfiguration(configuration) |  | ||||||
|                 .AddInMemoryCollection(azureAdInMemorySettings); |  | ||||||
|  |  | ||||||
|             var combinedConfiguration = configurationBuilder.Build(); |  | ||||||
|  |  | ||||||
|             services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) |  | ||||||
|                .AddMicrosoftIdentityWebApi(combinedConfiguration.GetSection("AzureAdB2C"), Schemes.AzureScheme) |  | ||||||
|                .EnableTokenAcquisitionToCallDownstreamApi() |  | ||||||
|                .AddMicrosoftGraph(configuration.GetSection("MicrosoftGraph")) |  | ||||||
|                .AddInMemoryTokenCaches(); |  | ||||||
|  |  | ||||||
|             var rsa = RSA.Create(); |             var rsa = RSA.Create(); | ||||||
|             rsa.ImportFromPem(authSettings.PrivateKey?.ToCharArray()); |             rsa.ImportFromPem(tokenAuthSettings.PrivateKey?.ToCharArray()); | ||||||
|             var rsaPrivateKey = new RsaSecurityKey(rsa); |             var rsaPrivateKey = new RsaSecurityKey(rsa); | ||||||
|  |  | ||||||
|             var rsaPublic = RSA.Create(); |             var rsaPublic = RSA.Create(); | ||||||
|             rsaPublic.ImportFromPem(authSettings.PublicKey?.ToCharArray()); |             rsaPublic.ImportFromPem(tokenAuthSettings.PublicKey?.ToCharArray()); | ||||||
|             var rsaPublicKey = new RsaSecurityKey(rsaPublic); |             var rsaPublicKey = new RsaSecurityKey(rsaPublic); | ||||||
|  |  | ||||||
|             var jwtAppSettingOptions = configuration.GetSection("B2C:JwtIssuerOptions"); |  | ||||||
|             var jwtIssuerOptions = jwtAppSettingOptions.Get<JwtIssuerOptions>(); |             var jwtIssuerOptions = new JwtIssuerOptions | ||||||
|  |             { | ||||||
|  |                 Audience = tokenAuthSettings.Audience, | ||||||
|  |                 Issuer = tokenAuthSettings.Issuer, | ||||||
|  |             }; | ||||||
|  |  | ||||||
|             if (string.IsNullOrEmpty(jwtIssuerOptions?.Issuer) || string.IsNullOrEmpty(jwtIssuerOptions.Audience)) |             if (string.IsNullOrEmpty(jwtIssuerOptions?.Issuer) || string.IsNullOrEmpty(jwtIssuerOptions.Audience)) | ||||||
|                 throw new InvalidOperationException("JwtIssuerOptions are not configured correctly."); |                 throw new InvalidOperationException("JwtIssuerOptions are not configured correctly."); | ||||||
| @@ -88,10 +86,52 @@ namespace Core.Thalos.Adapters.Extensions | |||||||
|                 options.Audience = jwtIssuerOptions?.Audience; |                 options.Audience = jwtIssuerOptions?.Audience; | ||||||
|                 options.SigningCredentials = new SigningCredentials(rsaPrivateKey, SecurityAlgorithms.RsaSha256); |                 options.SigningCredentials = new SigningCredentials(rsaPrivateKey, SecurityAlgorithms.RsaSha256); | ||||||
|             }); |             }); | ||||||
|  |         } | ||||||
|  |  | ||||||
|             services.AddSingleton(jwtAppSettingOptions); |         public static void AddAzureAuthentication(AuthSettings authSettings, IConfiguration configuration, IServiceCollection services) | ||||||
|             services.AddTransient<IAuthorizationHandler, PermissionsAuthorizationHandler>(); |         { | ||||||
|             services.AddTransient<ITokenService, TokenService>(); |             var azureAdInMemorySettings = new Dictionary<string, string?> | ||||||
|  |             { | ||||||
|  |                 { "AzureAdB2C:Instance",  authSettings.Azure.Instance ?? string.Empty }, | ||||||
|  |                 { "AzureAdB2C:TenantId", authSettings.Azure.TenantId ?? string.Empty }, | ||||||
|  |                 { "AzureAdB2C:ClientId", authSettings.Azure.ClientId ?? string.Empty }, | ||||||
|  |                 { "AzureAdB2C:ClientSecret", authSettings.Azure.ClientSecret ?? string.Empty } | ||||||
|  |             }; | ||||||
|  |  | ||||||
|  |             var configurationBuilder = new ConfigurationBuilder() | ||||||
|  |                 .AddConfiguration(configuration) | ||||||
|  |                 .AddInMemoryCollection(azureAdInMemorySettings); | ||||||
|  |  | ||||||
|  |             var combinedConfiguration = configurationBuilder.Build(); | ||||||
|  |  | ||||||
|  |             services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) | ||||||
|  |                .AddMicrosoftIdentityWebApi(combinedConfiguration.GetSection("AzureAdB2C"), Schemes.AzureScheme) | ||||||
|  |                .EnableTokenAcquisitionToCallDownstreamApi() | ||||||
|  |                .AddMicrosoftGraph(configuration.GetSection("MicrosoftGraph")) | ||||||
|  |                .AddInMemoryTokenCaches(); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         public static void AddGoogleAuthentication(IServiceCollection services, GoogleAuthSettings googleAuthSettings) | ||||||
|  |         { | ||||||
|  |             services.AddSingleton<GoogleAuthSettings>(googleAuthSettings); | ||||||
|  |  | ||||||
|  |             services.AddAuthentication(options => | ||||||
|  |             { | ||||||
|  |                 options.DefaultAuthenticateScheme = Schemes.GoogleScheme; | ||||||
|  |                 options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; | ||||||
|  |             }) | ||||||
|  |            .AddScheme<AuthenticationSchemeOptions, | ||||||
|  |             GoogleAccessTokenAuthenticationHandler>(Schemes.GoogleScheme, null) | ||||||
|  |            .AddGoogle(options => | ||||||
|  |            { | ||||||
|  |                options.ClientId = googleAuthSettings.ClientId!; | ||||||
|  |                options.ClientSecret = googleAuthSettings.ClientSecret!; | ||||||
|  |                //options.SaveTokens = true; | ||||||
|  |                options.CallbackPath = $"/{googleAuthSettings.RedirectUri}"; | ||||||
|  |            }); | ||||||
|  |  | ||||||
|  |             services.AddScoped<IGoogleAuthHelper, GoogleAuthHelper>(); | ||||||
|  |             services.AddScoped<IGoogleAuthorization, GoogleAuthorization>(); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -5,8 +5,6 @@ | |||||||
| // *********************************************************************** | // *********************************************************************** | ||||||
|  |  | ||||||
| using Asp.Versioning.ApiExplorer; | using Asp.Versioning.ApiExplorer; | ||||||
| using Core.Thalos.Adapters.Common.Constants; |  | ||||||
| using Core.Thalos.Adapters.Extensions; |  | ||||||
| using Microsoft.AspNetCore.Builder; | using Microsoft.AspNetCore.Builder; | ||||||
| using Microsoft.Extensions.Configuration; | using Microsoft.Extensions.Configuration; | ||||||
| using Microsoft.Extensions.DependencyInjection; | using Microsoft.Extensions.DependencyInjection; | ||||||
| @@ -16,7 +14,7 @@ using Microsoft.OpenApi.Models; | |||||||
| using Swashbuckle.AspNetCore.SwaggerGen; | using Swashbuckle.AspNetCore.SwaggerGen; | ||||||
| using Swashbuckle.AspNetCore.SwaggerUI; | using Swashbuckle.AspNetCore.SwaggerUI; | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters.Extensions | namespace Core.Thalos.BuildingBlocks.Configuration | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Extension methods for configuring Swagger documentation and UI. |     /// Extension methods for configuring Swagger documentation and UI. | ||||||
| @@ -40,40 +38,17 @@ namespace Core.Thalos.Adapters.Extensions | |||||||
|         /// </summary> |         /// </summary> | ||||||
|         /// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param> |         /// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param> | ||||||
|         /// <param name="configuration">The <see cref="IConfiguration"/> containing Swagger and OAuth2 configuration settings.</param> |         /// <param name="configuration">The <see cref="IConfiguration"/> containing Swagger and OAuth2 configuration settings.</param> | ||||||
|         public static void AddSwaggerGen(this IServiceCollection services, IConfiguration configuration, string DocumentationFile, AuthSettings authSettings) |         public static void AddSwaggerGen( | ||||||
|  |     this IServiceCollection services, | ||||||
|  |     IConfiguration configuration, | ||||||
|  |     string documentationFile, | ||||||
|  |     AuthSettings authSettings) | ||||||
|         { |         { | ||||||
|  |             var identityProviders = new IdentityProviders(); | ||||||
|  |             configuration.GetSection("IdentityProviders").Bind(identityProviders); | ||||||
|  |  | ||||||
|             services.AddSwaggerGen(c => |             services.AddSwaggerGen(c => | ||||||
|             { |             { | ||||||
|                     c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme |  | ||||||
|                     { |  | ||||||
|                         Description = "OAuth2.0 Authorization Code flow", |  | ||||||
|                         Name = "oauth2.0", |  | ||||||
|                         Type = SecuritySchemeType.OAuth2, |  | ||||||
|                         Flows = new OpenApiOAuthFlows |  | ||||||
|                         { |  | ||||||
|                             AuthorizationCode = new OpenApiOAuthFlow |  | ||||||
|                             { |  | ||||||
|                                 AuthorizationUrl = new Uri(authSettings.ThalosAppAuthorizationUrl ?? string.Empty), |  | ||||||
|                                 TokenUrl = new Uri(authSettings.ThalosAppTokenUrl ?? string.Empty), |  | ||||||
|                                 Scopes = new Dictionary<string, string> |  | ||||||
|                                 { |  | ||||||
|                                 { authSettings.ThalosAppScope ?? string.Empty, "Access API as User" } |  | ||||||
|                                 } |  | ||||||
|                             } |  | ||||||
|                         } |  | ||||||
|                     }); |  | ||||||
|  |  | ||||||
|                     c.AddSecurityRequirement(new OpenApiSecurityRequirement |  | ||||||
|                     { |  | ||||||
|                     { |  | ||||||
|                         new OpenApiSecurityScheme |  | ||||||
|                         { |  | ||||||
|                             Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "oauth2" } |  | ||||||
|                         }, |  | ||||||
|                         new[] { authSettings.ThalosAppScope } |  | ||||||
|                     } |  | ||||||
|                     }); |  | ||||||
|  |  | ||||||
|                 c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme |                 c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme | ||||||
|                 { |                 { | ||||||
|                     Description = "JWT Authorization header using the Bearer scheme", |                     Description = "JWT Authorization header using the Bearer scheme", | ||||||
| @@ -99,12 +74,79 @@ namespace Core.Thalos.Adapters.Extensions | |||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|                     var filePath = Path.Combine(AppContext.BaseDirectory, DocumentationFile); |                 if (identityProviders.Azure) | ||||||
|  |                 { | ||||||
|  |                     const string azureScheme = "oauth2-Azure"; | ||||||
|  |  | ||||||
|  |                     c.AddSecurityDefinition(azureScheme, new OpenApiSecurityScheme | ||||||
|  |                     { | ||||||
|  |                         Description = "Azure OAuth2 Authorization Code flow", | ||||||
|  |                         Type = SecuritySchemeType.OAuth2, | ||||||
|  |                         Flows = new OpenApiOAuthFlows | ||||||
|  |                         { | ||||||
|  |                             AuthorizationCode = new OpenApiOAuthFlow | ||||||
|  |                             { | ||||||
|  |                                 AuthorizationUrl = new Uri(authSettings.Azure?.ThalosAppAuthorizationUrl ?? | ||||||
|  |                                     "https://login.microsoftonline.com/common/oauth2/v2.0/authorize"), | ||||||
|  |                                 TokenUrl = new Uri(authSettings.Azure?.ThalosAppTokenUrl ?? | ||||||
|  |                                     "https://login.microsoftonline.com/common/oauth2/v2.0/token"), | ||||||
|  |                                 Scopes = new Dictionary<string, string> | ||||||
|  |                         { | ||||||
|  |                             { authSettings.Azure?.ThalosAppScope ?? "access_as_user", "Access API as User" } | ||||||
|  |                         } | ||||||
|  |                             } | ||||||
|  |                         } | ||||||
|  |                     }); | ||||||
|  |  | ||||||
|  |                     c.AddSecurityRequirement(new OpenApiSecurityRequirement | ||||||
|  |                     { | ||||||
|  |                         [new OpenApiSecurityScheme | ||||||
|  |                         { | ||||||
|  |                             Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = azureScheme } | ||||||
|  |                         }] = new[] { authSettings.Azure?.ThalosAppScope ?? "access_as_user" } | ||||||
|  |                     }); | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 if (identityProviders.Google) | ||||||
|  |                 { | ||||||
|  |                     const string googleScheme = "oauth2-Google"; | ||||||
|  |  | ||||||
|  |                     c.AddSecurityDefinition(googleScheme, new OpenApiSecurityScheme | ||||||
|  |                     { | ||||||
|  |                         Type = SecuritySchemeType.OAuth2, | ||||||
|  |                         Flows = new OpenApiOAuthFlows | ||||||
|  |                         { | ||||||
|  |                             AuthorizationCode = new OpenApiOAuthFlow | ||||||
|  |                             { | ||||||
|  |                                 AuthorizationUrl = new Uri("https://accounts.google.com/o/oauth2/v2/auth"), | ||||||
|  |                                 TokenUrl = new Uri("https://oauth2.googleapis.com/token"), | ||||||
|  |                                 Scopes = new Dictionary<string, string> | ||||||
|  |                         { | ||||||
|  |                             { "openid",  "OpenID Connect" }, | ||||||
|  |                             { "email",   "Access email" }, | ||||||
|  |                             { "profile", "Access profile" } | ||||||
|  |                         } | ||||||
|  |                             } | ||||||
|  |                         } | ||||||
|  |                     }); | ||||||
|  |  | ||||||
|  |                     c.AddSecurityRequirement(new OpenApiSecurityRequirement | ||||||
|  |                     { | ||||||
|  |                         [new OpenApiSecurityScheme | ||||||
|  |                         { | ||||||
|  |                             Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = googleScheme } | ||||||
|  |                         }] = new[] { "openid", "email", "profile" } | ||||||
|  |                     }); | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 // ✅ XML Comments | ||||||
|  |                 var filePath = Path.Combine(AppContext.BaseDirectory, documentationFile); | ||||||
|                 c.IncludeXmlComments(filePath); |                 c.IncludeXmlComments(filePath); | ||||||
|                 c.SchemaFilter<EnumSchemaFilter>(); |                 c.SchemaFilter<EnumSchemaFilter>(); | ||||||
|             }); |             }); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Configures Swagger and Swagger UI for the application. |         /// Configures Swagger and Swagger UI for the application. | ||||||
|         /// </summary> |         /// </summary> | ||||||
| @@ -129,17 +171,38 @@ namespace Core.Thalos.Adapters.Extensions | |||||||
|         /// </summary> |         /// </summary> | ||||||
|         /// <param name="app">The <see cref="WebApplication"/> instance.</param> |         /// <param name="app">The <see cref="WebApplication"/> instance.</param> | ||||||
|         /// <param name="configuration">The <see cref="IConfiguration"/> containing Swagger UI and OAuth2 configuration settings.</param> |         /// <param name="configuration">The <see cref="IConfiguration"/> containing Swagger UI and OAuth2 configuration settings.</param> | ||||||
|         public static void UseSwaggerUI(this WebApplication app, IConfiguration configuration, AuthSettings authSettings) |         public static void UseSwaggerUI( | ||||||
|  |             this WebApplication app, | ||||||
|  |             IConfiguration configuration, | ||||||
|  |             AuthSettings authSettings) | ||||||
|  |         { | ||||||
|  |             var identityProviders = new IdentityProviders(); | ||||||
|  |             configuration.GetSection("IdentityProviders").Bind(identityProviders); | ||||||
|  |  | ||||||
|  |             app.UseSwagger(); | ||||||
|  |  | ||||||
|  |             if (identityProviders.Google) | ||||||
|             { |             { | ||||||
|                 app.UseSwaggerUI(options => |                 app.UseSwaggerUI(options => | ||||||
|                 { |                 { | ||||||
|                 options.SwaggerEndpoint("/swagger/v1/swagger.json", "Custom Auth API with Azure AD v1"); |  | ||||||
|                 options.OAuthClientId(authSettings.ThalosAppClientId); |  | ||||||
|                     options.OAuthUsePkce(); |                     options.OAuthUsePkce(); | ||||||
|                     options.OAuthScopeSeparator(" "); |                     options.OAuthScopeSeparator(" "); | ||||||
|  |                     options.OAuthClientId(authSettings.Google?.ClientId); | ||||||
|  |                     options.OAuthClientSecret(authSettings.Google?.ClientSecret); | ||||||
|                 }); |                 }); | ||||||
|             } |             } | ||||||
|  |  | ||||||
|  |             if (identityProviders.Azure) | ||||||
|  |             { | ||||||
|  |                 app.UseSwaggerUI(options => | ||||||
|  |                 { | ||||||
|  |                     options.OAuthUsePkce(); | ||||||
|  |                     options.OAuthScopeSeparator(" "); | ||||||
|  |                     options.OAuthClientId(authSettings.Azure?.ThalosAppClientId); | ||||||
|  |                 }); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Adds API versioning and API explorer to the application. |         /// Adds API versioning and API explorer to the application. | ||||||
|         /// </summary> |         /// </summary> | ||||||
|   | |||||||
| @@ -4,15 +4,15 @@ using OpenTelemetry.Metrics; | |||||||
| using OpenTelemetry.Resources; | using OpenTelemetry.Resources; | ||||||
| using OpenTelemetry.Trace; | using OpenTelemetry.Trace; | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters.Extensions | namespace Core.Thalos.BuildingBlocks.Configuration | ||||||
| { | { | ||||||
|     public static class TelemetryExtensions |     public static class TelemetryExtensions | ||||||
|     { |     { | ||||||
|         public static void AddTelemetry(this IServiceCollection services) |         public static void AddTelemetry(this IServiceCollection services, string apiName) | ||||||
|         { |         { | ||||||
|             // Add OpenTelemetry Tracing |             // Add OpenTelemetry Tracing | ||||||
|             services.AddOpenTelemetry() |             services.AddOpenTelemetry() | ||||||
|                     .ConfigureResource(resource => resource.AddService("lsa.dashboard.bff.api")) |                     .ConfigureResource(resource => resource.AddService($"{apiName}")) | ||||||
|                     .WithTracing(tracing => tracing.AddAspNetCoreInstrumentation().AddConsoleExporter()) |                     .WithTracing(tracing => tracing.AddAspNetCoreInstrumentation().AddConsoleExporter()) | ||||||
|                     .WithMetrics(metrics => metrics.AddAspNetCoreInstrumentation().AddConsoleExporter()). |                     .WithMetrics(metrics => metrics.AddAspNetCoreInstrumentation().AddConsoleExporter()). | ||||||
|                      WithLogging(logs => logs.AddConsoleExporter()); |                      WithLogging(logs => logs.AddConsoleExporter()); | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| using Microsoft.AspNetCore.Http; | using Microsoft.AspNetCore.Http; | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters.Extensions | namespace Core.Thalos.BuildingBlocks.Extensions | ||||||
| { | { | ||||||
|     public sealed class TrackingMechanismExtension : DelegatingHandler |     public sealed class TrackingMechanismExtension : DelegatingHandler | ||||||
|     { |     { | ||||||
|   | |||||||
| @@ -4,9 +4,8 @@ | |||||||
| // </copyright> | // </copyright> | ||||||
| // *********************************************************************** | // *********************************************************************** | ||||||
|  |  | ||||||
| using Core.Thalos.Adapters.Contracts; |  | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters.Handlers | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Class to inject the token in all requests. |     /// Class to inject the token in all requests. | ||||||
|   | |||||||
| @@ -1,5 +1,4 @@ | |||||||
| using Core.Thalos.Adapters.Common.Constants; | using Google.Apis.Auth; | ||||||
| using Google.Apis.Auth; |  | ||||||
| using Microsoft.AspNetCore.Authentication; | using Microsoft.AspNetCore.Authentication; | ||||||
| using Microsoft.Extensions.Configuration; | using Microsoft.Extensions.Configuration; | ||||||
| using Microsoft.Extensions.Logging; | using Microsoft.Extensions.Logging; | ||||||
| @@ -7,12 +6,15 @@ using Microsoft.Extensions.Options; | |||||||
| using System.Security.Claims; | using System.Security.Claims; | ||||||
| using System.Text.Encodings.Web; | using System.Text.Encodings.Web; | ||||||
| 
 | 
 | ||||||
| namespace Core.Thalos.BuildingBlocks.Authentication.Handlers | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     public class GoogleAccessTokenAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, |     public class GoogleAccessTokenAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, | ||||||
|          ILoggerFactory logger, |          ILoggerFactory logger, | ||||||
|          UrlEncoder encoder, |          UrlEncoder encoder, | ||||||
|          IConfiguration config) : AuthenticationHandler<AuthenticationSchemeOptions>(options, logger, encoder) |          IConfiguration config, | ||||||
|  |          GoogleAuthSettings googleSettings | ||||||
|  |          ) : AuthenticationHandler<AuthenticationSchemeOptions>(options, logger, encoder) | ||||||
|  | 
 | ||||||
|     { |     { | ||||||
|         protected override async Task<AuthenticateResult> HandleAuthenticateAsync() |         protected override async Task<AuthenticateResult> HandleAuthenticateAsync() | ||||||
|         { |         { | ||||||
| @@ -32,7 +34,7 @@ namespace Core.Thalos.BuildingBlocks.Authentication.Handlers | |||||||
|                     idToken, |                     idToken, | ||||||
|                     new GoogleJsonWebSignature.ValidationSettings |                     new GoogleJsonWebSignature.ValidationSettings | ||||||
|                     { |                     { | ||||||
|                         Audience = new[] { config["Authentication:Google:ClientId"]! } |                         Audience = new[] { googleSettings.ClientId! } | ||||||
|                     }); |                     }); | ||||||
|             } |             } | ||||||
|             catch (InvalidJwtException) |             catch (InvalidJwtException) | ||||||
| @@ -1,7 +1,6 @@ | |||||||
| using Core.Thalos.Adapters.Handlers.Adapters; | using Microsoft.AspNetCore.Authorization; | ||||||
| using Microsoft.AspNetCore.Authorization; |  | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters.Handlers | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     public class PermissionsAuthorizationHandler : AuthorizationHandler<PermissionsAuthorizationAdapter> |     public class PermissionsAuthorizationHandler : AuthorizationHandler<PermissionsAuthorizationAdapter> | ||||||
|     { |     { | ||||||
|   | |||||||
| @@ -1,11 +1,12 @@ | |||||||
| using Azure.Identity; | using Azure.Identity; | ||||||
| using Core.Thalos.Adapters.Common.Constants; | using Core.Blueprint.KeyVault; | ||||||
| using Microsoft.AspNetCore.Builder; | using Microsoft.AspNetCore.Builder; | ||||||
| using Microsoft.Extensions.Configuration; | using Microsoft.Extensions.Configuration; | ||||||
| using Microsoft.Extensions.Configuration.AzureAppConfiguration; | using Microsoft.Extensions.Configuration.AzureAppConfiguration; | ||||||
|  | using Microsoft.Extensions.DependencyInjection; | ||||||
| using Microsoft.Extensions.Logging; | using Microsoft.Extensions.Logging; | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters.Helpers | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     public static class AuthHelper |     public static class AuthHelper | ||||||
|     { |     { | ||||||
| @@ -15,9 +16,16 @@ namespace Core.Thalos.Adapters.Helpers | |||||||
|         }).CreateLogger("AuthHelper"); |         }).CreateLogger("AuthHelper"); | ||||||
|  |  | ||||||
|  |  | ||||||
|         public static AuthSettings GetAuthSettings(WebApplicationBuilder builder, string appConfigLabel) |         public static async Task<AuthSettings> GetAuthSettings(this IServiceCollection services, WebApplicationBuilder builder, string appConfigLabel) | ||||||
|         { |         { | ||||||
|             var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? string.Empty; |             var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? string.Empty; | ||||||
|  |             var authSettings = new AuthSettings(); | ||||||
|  |  | ||||||
|  |             var identityProviders = new IdentityProviders(); | ||||||
|  |             builder.Configuration.GetSection("IdentityProviders").Bind(identityProviders); | ||||||
|  |  | ||||||
|  |             using var serviceProvider = services.BuildServiceProvider(); | ||||||
|  |             var keyVaultProvider = serviceProvider.GetRequiredService<IKeyVaultProvider>(); | ||||||
|  |  | ||||||
|             if (environment != "Local") |             if (environment != "Local") | ||||||
|             { |             { | ||||||
| @@ -39,19 +47,82 @@ namespace Core.Thalos.Adapters.Helpers | |||||||
|                 }); |                 }); | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             return new AuthSettings |             if (identityProviders.Google) | ||||||
|  |                 authSettings.Google = await GetGoogleSettings(keyVaultProvider, builder); | ||||||
|  |  | ||||||
|  |             if (identityProviders.Azure) | ||||||
|  |                 authSettings.Azure = GetAzureSettings(builder); | ||||||
|  |  | ||||||
|  |             authSettings.Token = await GetTokenSettings(keyVaultProvider, builder); | ||||||
|  |  | ||||||
|  |             return authSettings; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         private async static ValueTask<TokenAuthSettings> GetTokenSettings(IKeyVaultProvider keyVaultProvider, WebApplicationBuilder builder) | ||||||
|         { |         { | ||||||
|                 AzureADInstance = builder.Configuration.GetSection(Secrets.AzureADInstance).Value, |             var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? string.Empty; | ||||||
|                 AzureADTenantId = builder.Configuration.GetSection(Secrets.AzureADTenantId).Value, |  | ||||||
|                 AzureADClientId = builder.Configuration.GetSection(Secrets.AzureADClientId).Value, |             var tokenSettings = new TokenAuthSettings(); | ||||||
|                 AzureADClientSecret = builder.Configuration.GetSection(Secrets.AzureADClientSecret).Value, |  | ||||||
|  |             if (environment == "Local") | ||||||
|  |             { | ||||||
|  |                 tokenSettings.PublicKey = (await keyVaultProvider.GetSecretAsync(Secrets.PublicKey, new CancellationToken { })).Secret.Value; | ||||||
|  |                 tokenSettings.PrivateKey = (await keyVaultProvider.GetSecretAsync(Secrets.PrivateKey, new CancellationToken { })).Secret.Value; | ||||||
|  |                 tokenSettings.Issuer = (await keyVaultProvider.GetSecretAsync(Secrets.Issuer, new CancellationToken { })).Secret.Value; | ||||||
|  |                 tokenSettings.Audience = (await keyVaultProvider.GetSecretAsync(Secrets.Audience, new CancellationToken { })).Secret.Value; | ||||||
|  |             } | ||||||
|  |             else | ||||||
|  |             { | ||||||
|  |                 tokenSettings.PrivateKey = builder.Configuration.GetSection(Secrets.PrivateKey).Value; | ||||||
|  |                 tokenSettings.PublicKey = builder.Configuration.GetSection(Secrets.PublicKey).Value; | ||||||
|  |                 tokenSettings.Issuer = builder.Configuration.GetSection(Secrets.Issuer).Value; | ||||||
|  |                 tokenSettings.Audience = builder.Configuration.GetSection(Secrets.Audience).Value; | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             if (string.IsNullOrEmpty(tokenSettings.PrivateKey) || string.IsNullOrEmpty(tokenSettings.PublicKey)) | ||||||
|  |             { | ||||||
|  |                 logger.LogError("Settings for token creation are missing or incorrectly formatted."); | ||||||
|  |                 throw new InvalidOperationException("Invalid public or private key."); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             return tokenSettings; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         private static AzureAuthSettings GetAzureSettings(WebApplicationBuilder builder) | ||||||
|  |         { | ||||||
|  |             return new AzureAuthSettings | ||||||
|  |             { | ||||||
|  |                 Instance = builder.Configuration.GetSection(Secrets.AzureADInstance).Value, | ||||||
|  |                 TenantId = builder.Configuration.GetSection(Secrets.AzureADTenantId).Value, | ||||||
|  |                 ClientId = builder.Configuration.GetSection(Secrets.AzureADClientId).Value, | ||||||
|  |                 ClientSecret = builder.Configuration.GetSection(Secrets.AzureADClientSecret).Value, | ||||||
|                 ThalosAppAuthorizationUrl = builder.Configuration.GetSection(Secrets.ThalosAppAuthorizationUrl).Value, |                 ThalosAppAuthorizationUrl = builder.Configuration.GetSection(Secrets.ThalosAppAuthorizationUrl).Value, | ||||||
|                 ThalosAppTokenUrl = builder.Configuration.GetSection(Secrets.ThalosAppTokenUrl).Value, |                 ThalosAppTokenUrl = builder.Configuration.GetSection(Secrets.ThalosAppTokenUrl).Value, | ||||||
|                 ThalosAppClientId = builder.Configuration.GetSection(Secrets.ThalosAppClientId).Value, |                 ThalosAppClientId = builder.Configuration.GetSection(Secrets.ThalosAppClientId).Value, | ||||||
|                 ThalosAppScope = builder.Configuration.GetSection(Secrets.ThalosAppScope).Value, |                 ThalosAppScope = builder.Configuration.GetSection(Secrets.ThalosAppScope).Value, | ||||||
|                 PrivateKey = builder.Configuration.GetSection(Secrets.PrivateKey).Value, |  | ||||||
|                 PublicKey = builder.Configuration.GetSection(Secrets.PublicKey).Value, |  | ||||||
|             }; |             }; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         private static async ValueTask<GoogleAuthSettings> GetGoogleSettings(IKeyVaultProvider keyVaultProvider, WebApplicationBuilder builder) | ||||||
|  |         { | ||||||
|  |             var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); | ||||||
|  |             var googleSettings = new GoogleAuthSettings(); | ||||||
|  |  | ||||||
|  |             if (environment == "Local") | ||||||
|  |             { | ||||||
|  |                 googleSettings.ClientId = (await keyVaultProvider.GetSecretAsync(Secrets.GoogleClientId, new CancellationToken { })).Secret.Value; ; | ||||||
|  |                 googleSettings.ClientSecret = (await keyVaultProvider.GetSecretAsync(Secrets.GoogleClientSecret, new CancellationToken { })).Secret.Value; | ||||||
|  |                 googleSettings.RedirectUri = (await keyVaultProvider.GetSecretAsync(Secrets.GoogleRedirectUri, new CancellationToken { })).Secret.Value; | ||||||
|  |             } | ||||||
|  |             else | ||||||
|  |             { | ||||||
|  |                 googleSettings.ClientId = builder.Configuration.GetSection(Secrets.GoogleClientId).Value; | ||||||
|  |                 googleSettings.ClientSecret = builder.Configuration.GetSection(Secrets.GoogleClientSecret).Value; | ||||||
|  |                 googleSettings.RedirectUri = builder.Configuration.GetSection(Secrets.GoogleRedirectUri).Value; | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             return googleSettings; | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -2,14 +2,14 @@ | |||||||
| using Google.Apis.Oauth2.v2; | using Google.Apis.Oauth2.v2; | ||||||
| using Microsoft.Extensions.Configuration; | using Microsoft.Extensions.Configuration; | ||||||
| 
 | 
 | ||||||
| namespace Core.Thalos.BuildingBlocks.Authentication.Helpers | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     public class GoogleAuthHelper(IConfiguration config) : IGoogleAuthHelper |     public class GoogleAuthHelper(IConfiguration config, GoogleAuthSettings googleSettings) : IGoogleAuthHelper | ||||||
|     { |     { | ||||||
|         public ClientSecrets GetClientSecrets() |         public ClientSecrets GetClientSecrets() | ||||||
|         { |         { | ||||||
|             string clientId = config["Authentication:Google:ClientId"]!; |             string clientId = googleSettings.ClientId ?? string.Empty; | ||||||
|             string clientSecret = config["Authentication:Google:ClientSecret"]!; |             string clientSecret = googleSettings.ClientSecret ?? string.Empty; | ||||||
| 
 | 
 | ||||||
|             return new() { ClientId = clientId, ClientSecret = clientSecret }; |             return new() { ClientId = clientId, ClientSecret = clientSecret }; | ||||||
|         } |         } | ||||||
| @@ -10,7 +10,7 @@ using Org.BouncyCastle.Security; | |||||||
| using System.Security.Cryptography; | using System.Security.Cryptography; | ||||||
| using System.Text; | using System.Text; | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters.Helpers | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Handles all methods related to RSA encryption"/>. |     /// Handles all methods related to RSA encryption"/>. | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| using Microsoft.IdentityModel.Tokens; | using Microsoft.IdentityModel.Tokens; | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters.Options | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// JWT token Issuer options (used for JWT Factory) |     /// JWT token Issuer options (used for JWT Factory) | ||||||
|   | |||||||
| @@ -3,9 +3,6 @@ | |||||||
| //     AgileWebs | //     AgileWebs | ||||||
| // </copyright> | // </copyright> | ||||||
| // *********************************************************************** | // *********************************************************************** | ||||||
| using Core.Thalos.Adapters.Common.Constants; |  | ||||||
| using Core.Thalos.Adapters.Contracts; |  | ||||||
| using Core.Thalos.Adapters.Options; |  | ||||||
| using Microsoft.AspNetCore.Http; | using Microsoft.AspNetCore.Http; | ||||||
| using Microsoft.AspNetCore.Mvc; | using Microsoft.AspNetCore.Mvc; | ||||||
| using Microsoft.Extensions.Configuration; | using Microsoft.Extensions.Configuration; | ||||||
| @@ -16,7 +13,7 @@ using System.IdentityModel.Tokens.Jwt; | |||||||
| using System.Security.Claims; | using System.Security.Claims; | ||||||
| using System.Text.Json; | using System.Text.Json; | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters.Services | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Service responsible for manage authenticacion. |     /// Service responsible for manage authenticacion. | ||||||
| @@ -90,8 +87,10 @@ namespace Core.Thalos.Adapters.Services | |||||||
|                 { |                 { | ||||||
|  |  | ||||||
|                     new Claim(Claims.Name, adapter?.User?.DisplayName ?? string.Empty), |                     new Claim(Claims.Name, adapter?.User?.DisplayName ?? string.Empty), | ||||||
|                     new Claim(Claims.GUID, adapter?.User?.Guid ?? string.Empty), |                     new Claim(Claims.Id, adapter?.User?.Id ?? string.Empty), | ||||||
|                     new Claim(Claims.Email, adapter?.User?.Email ?? string.Empty), |                     new Claim(Claims.Email, adapter?.User?.Email ?? string.Empty), | ||||||
|  |                     new Claim(Claims.Tenant, adapter?.Tenant?.Name ?? string.Empty), | ||||||
|  |                     new Claim(Claims.Tenant, adapter?.Tenant?.Id ?? string.Empty), | ||||||
|                     new Claim(Claims.Role, adapter?.Role?.Name ?? string.Empty), |                     new Claim(Claims.Role, adapter?.Role?.Name ?? string.Empty), | ||||||
|                     new Claim(Claims.RoleId, adapter?.Role?.Id ?? string.Empty), |                     new Claim(Claims.RoleId, adapter?.Role?.Id ?? string.Empty), | ||||||
|                     new Claim(Claims.Applications, JsonSerializer.Serialize(adapter?.Role?.Applications), JsonClaimValueTypes.JsonArray), |                     new Claim(Claims.Applications, JsonSerializer.Serialize(adapter?.Role?.Applications), JsonClaimValueTypes.JsonArray), | ||||||
|   | |||||||
| @@ -4,22 +4,39 @@ | |||||||
| // </copyright> | // </copyright> | ||||||
| // *********************************************************************** | // *********************************************************************** | ||||||
|  |  | ||||||
|  | namespace Core.Thalos.BuildingBlocks; | ||||||
| public class AuthSettings | public class AuthSettings | ||||||
| { | { | ||||||
|     // Azure AD Settings |     public AzureAuthSettings? Azure { get; set; } | ||||||
|     public string? AzureADInstance { get; set; } |     public TokenAuthSettings Token { get; set; } = null!; | ||||||
|     public string? AzureADTenantId { get; set; } |     public GoogleAuthSettings? Google { get; set; } | ||||||
|     public string? AzureADClientId { get; set; } | } | ||||||
|     public string? AzureADClientSecret { get; set; } |  | ||||||
|  |  | ||||||
|     //Thalos App Settings | public class AzureAuthSettings | ||||||
|  | { | ||||||
|  |     public string? Instance { get; set; } | ||||||
|  |     public string? TenantId { get; set; } | ||||||
|  |     public string? ClientId { get; set; } | ||||||
|  |     public string? ClientSecret { get; set; } | ||||||
|     public string? ThalosAppAuthorizationUrl { get; set; } |     public string? ThalosAppAuthorizationUrl { get; set; } | ||||||
|     public string? ThalosAppTokenUrl { get; set; } |     public string? ThalosAppTokenUrl { get; set; } | ||||||
|     public string? ThalosAppClientId { get; set; } |     public string? ThalosAppClientId { get; set; } | ||||||
|     public string? ThalosAppScope { get; set; } |     public string? ThalosAppScope { get; set; } | ||||||
|  | } | ||||||
|     // Token Keys |  | ||||||
|     public string? PrivateKey { get; set; } | public class GoogleAuthSettings | ||||||
|     public string? PublicKey { get; set; } | { | ||||||
|  |     public string? ClientId { get; set; } | ||||||
|  |     public string? ClientSecret { get; set; } | ||||||
|  |     public string? RedirectUri { get; set; } | ||||||
|  |  | ||||||
|  | } | ||||||
|  |  | ||||||
|  | public class TokenAuthSettings | ||||||
|  | { | ||||||
|  |     public string? PrivateKey { get; set; } | ||||||
|  |     public string? PublicKey { get; set; } | ||||||
|  |     public string? Audience { get; set; } | ||||||
|  |     public string? Issuer { get; set; } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										8
									
								
								Core.Thalos.BuildingBlocks/Settings/IdentityProviders.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								Core.Thalos.BuildingBlocks/Settings/IdentityProviders.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | |||||||
|  | namespace Core.Thalos.BuildingBlocks | ||||||
|  | { | ||||||
|  |     public class IdentityProviders | ||||||
|  |     { | ||||||
|  |         public bool Google { get; set; } | ||||||
|  |         public bool Azure { get; set; } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -4,10 +4,9 @@ | |||||||
| // </copyright> | // </copyright> | ||||||
| // *********************************************************************** | // *********************************************************************** | ||||||
|  |  | ||||||
| using Core.Thalos.Adapters.Contracts; |  | ||||||
| using Microsoft.AspNetCore.Http; | using Microsoft.AspNetCore.Http; | ||||||
|  |  | ||||||
| namespace Core.Thalos.Adapters.TokenProvider | namespace Core.Thalos.BuildingBlocks | ||||||
| { | { | ||||||
|     /// <summary> |     /// <summary> | ||||||
|     /// Class to return the access token to controllers. |     /// Class to return the access token to controllers. | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user