From 9a02f0e4d681e71a04a498d2009502bd400ec771 Mon Sep 17 00:00:00 2001 From: Sergio Matias Date: Fri, 22 Aug 2025 21:04:25 -0600 Subject: [PATCH 1/4] Fixed google settings --- .../Authorization/Google/GoogleAuthorization.cs | 4 ++-- .../Extensions/AuthenticationExtension.cs | 5 ++--- .../Handlers/GoogleAccessTokenAuthenticationHandler.cs | 7 +++++-- Core.Thalos.BuildingBlocks/Helpers/GoogleAuthHelper.cs | 6 +++--- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Core.Thalos.BuildingBlocks/Authentication/Authorization/Google/GoogleAuthorization.cs b/Core.Thalos.BuildingBlocks/Authentication/Authorization/Google/GoogleAuthorization.cs index df52cc1..5f3af36 100644 --- a/Core.Thalos.BuildingBlocks/Authentication/Authorization/Google/GoogleAuthorization.cs +++ b/Core.Thalos.BuildingBlocks/Authentication/Authorization/Google/GoogleAuthorization.cs @@ -5,9 +5,9 @@ using Microsoft.Extensions.Configuration; namespace Core.Thalos.BuildingBlocks { 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 ExchangeCodeForToken(string code) { diff --git a/Core.Thalos.BuildingBlocks/Extensions/AuthenticationExtension.cs b/Core.Thalos.BuildingBlocks/Extensions/AuthenticationExtension.cs index 7f4b403..7af9050 100644 --- a/Core.Thalos.BuildingBlocks/Extensions/AuthenticationExtension.cs +++ b/Core.Thalos.BuildingBlocks/Extensions/AuthenticationExtension.cs @@ -9,7 +9,6 @@ using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; using Microsoft.Identity.Web; using Microsoft.IdentityModel.Tokens; using System.Security.Cryptography; @@ -87,8 +86,6 @@ namespace Core.Thalos.BuildingBlocks.Configuration options.Audience = jwtIssuerOptions?.Audience; options.SigningCredentials = new SigningCredentials(rsaPrivateKey, SecurityAlgorithms.RsaSha256); }); - - services.AddSingleton>(Microsoft.Extensions.Options.Options.Create(jwtIssuerOptions)); } public static void AddAzureAuthentication(AuthSettings authSettings, IConfiguration configuration, IServiceCollection services) @@ -116,6 +113,8 @@ namespace Core.Thalos.BuildingBlocks.Configuration public static void AddGoogleAuthentication(IServiceCollection services, GoogleAuthSettings googleAuthSettings) { + services.AddSingleton(googleAuthSettings); + services.AddAuthentication(options => { options.DefaultAuthenticateScheme = Schemes.GoogleScheme; diff --git a/Core.Thalos.BuildingBlocks/Handlers/GoogleAccessTokenAuthenticationHandler.cs b/Core.Thalos.BuildingBlocks/Handlers/GoogleAccessTokenAuthenticationHandler.cs index 86eb33d..71bca78 100644 --- a/Core.Thalos.BuildingBlocks/Handlers/GoogleAccessTokenAuthenticationHandler.cs +++ b/Core.Thalos.BuildingBlocks/Handlers/GoogleAccessTokenAuthenticationHandler.cs @@ -11,7 +11,10 @@ namespace Core.Thalos.BuildingBlocks public class GoogleAccessTokenAuthenticationHandler(IOptionsMonitor options, ILoggerFactory logger, UrlEncoder encoder, - IConfiguration config) : AuthenticationHandler(options, logger, encoder) + IConfiguration config, + GoogleAuthSettings googleSettings + ) : AuthenticationHandler(options, logger, encoder) + { protected override async Task HandleAuthenticateAsync() { @@ -31,7 +34,7 @@ namespace Core.Thalos.BuildingBlocks idToken, new GoogleJsonWebSignature.ValidationSettings { - Audience = new[] { config["Authentication:Google:ClientId"]! } + Audience = new[] { googleSettings.ClientId! } }); } catch (InvalidJwtException) diff --git a/Core.Thalos.BuildingBlocks/Helpers/GoogleAuthHelper.cs b/Core.Thalos.BuildingBlocks/Helpers/GoogleAuthHelper.cs index 3acaa37..2b15b6a 100644 --- a/Core.Thalos.BuildingBlocks/Helpers/GoogleAuthHelper.cs +++ b/Core.Thalos.BuildingBlocks/Helpers/GoogleAuthHelper.cs @@ -4,12 +4,12 @@ using Microsoft.Extensions.Configuration; namespace Core.Thalos.BuildingBlocks { - public class GoogleAuthHelper(IConfiguration config) : IGoogleAuthHelper + public class GoogleAuthHelper(IConfiguration config, GoogleAuthSettings googleSettings) : IGoogleAuthHelper { public ClientSecrets GetClientSecrets() { - string clientId = config["Authentication:Google:ClientId"]!; - string clientSecret = config["Authentication:Google:ClientSecret"]!; + string clientId = googleSettings.ClientId ?? string.Empty; + string clientSecret = googleSettings.ClientSecret ?? string.Empty; return new() { ClientId = clientId, ClientSecret = clientSecret }; } From 5277896bdc4ad9314ea39fddcf1ed833d24e56fe Mon Sep 17 00:00:00 2001 From: Sergio Matias Date: Tue, 26 Aug 2025 14:10:29 -0600 Subject: [PATCH 2/4] Add tenant identifier in user property --- Core.Thalos.BuildingBlocks/Adapters/UserAdapter.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Core.Thalos.BuildingBlocks/Adapters/UserAdapter.cs b/Core.Thalos.BuildingBlocks/Adapters/UserAdapter.cs index 684bdc8..e8eea66 100644 --- a/Core.Thalos.BuildingBlocks/Adapters/UserAdapter.cs +++ b/Core.Thalos.BuildingBlocks/Adapters/UserAdapter.cs @@ -96,5 +96,13 @@ namespace Core.Thalos.BuildingBlocks [BsonRepresentation(BsonType.String)] [JsonPropertyName("token")] public string? Token { get; set; } = null; + + /// + /// Gets or sets the tenant identifier associated with the user. + /// + [BsonElement("tenantId")] + [BsonRepresentation(BsonType.ObjectId)] + [JsonPropertyName("tenantId")] + public string? TenantId { get; set; } = null; } } From 4a2ed52a2f4e020dc8fdbdeef782c3db15433de0 Mon Sep 17 00:00:00 2001 From: Sergio Matias Date: Tue, 26 Aug 2025 14:10:48 -0600 Subject: [PATCH 3/4] fix --- Core.Thalos.BuildingBlocks/Adapters/UserAdapter.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Core.Thalos.BuildingBlocks/Adapters/UserAdapter.cs b/Core.Thalos.BuildingBlocks/Adapters/UserAdapter.cs index e8eea66..52e6988 100644 --- a/Core.Thalos.BuildingBlocks/Adapters/UserAdapter.cs +++ b/Core.Thalos.BuildingBlocks/Adapters/UserAdapter.cs @@ -103,6 +103,5 @@ namespace Core.Thalos.BuildingBlocks [BsonElement("tenantId")] [BsonRepresentation(BsonType.ObjectId)] [JsonPropertyName("tenantId")] - public string? TenantId { get; set; } = null; + public string? TenantId { get; set; } } -} From 3b752f182f493da4043a62143f8c9b203d07ab03 Mon Sep 17 00:00:00 2001 From: Sergio Matias Date: Tue, 26 Aug 2025 14:11:09 -0600 Subject: [PATCH 4/4] fix --- Core.Thalos.BuildingBlocks/Adapters/UserAdapter.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Core.Thalos.BuildingBlocks/Adapters/UserAdapter.cs b/Core.Thalos.BuildingBlocks/Adapters/UserAdapter.cs index 52e6988..c4cc62c 100644 --- a/Core.Thalos.BuildingBlocks/Adapters/UserAdapter.cs +++ b/Core.Thalos.BuildingBlocks/Adapters/UserAdapter.cs @@ -105,3 +105,4 @@ namespace Core.Thalos.BuildingBlocks [JsonPropertyName("tenantId")] public string? TenantId { get; set; } } +}