From 9a02f0e4d681e71a04a498d2009502bd400ec771 Mon Sep 17 00:00:00 2001 From: Sergio Matias Date: Fri, 22 Aug 2025 21:04:25 -0600 Subject: [PATCH] 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 }; }