diff --git a/Core.Thalos.BuildingBlocks/Adapters/UserAdapter.cs b/Core.Thalos.BuildingBlocks/Adapters/UserAdapter.cs index 35f99be..449f0ca 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; } } } 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 }; }