12 Commits

9 changed files with 31 additions and 12 deletions

View File

@@ -5,9 +5,9 @@ using Microsoft.Extensions.Configuration;
namespace Core.Thalos.BuildingBlocks 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)
{ {

View File

@@ -58,5 +58,6 @@ namespace Core.Thalos.BuildingBlocks
public const string GoogleClientId = "GoogleClientId"; public const string GoogleClientId = "GoogleClientId";
public const string GoogleClientSecret = "GoogleClientSecret"; public const string GoogleClientSecret = "GoogleClientSecret";
public const string GoogleRedirectUri = "GoogleRedirectUri"; public const string GoogleRedirectUri = "GoogleRedirectUri";
public const string GoogleLocalRedirectUri = "GoogleLocalRedirectUri";
} }
} }

View File

@@ -14,7 +14,7 @@
<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.KeyVault" Version="1.0.0" />
<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" />

View File

@@ -9,7 +9,6 @@ using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.Identity.Web; using Microsoft.Identity.Web;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
using System.Security.Cryptography; using System.Security.Cryptography;
@@ -87,8 +86,6 @@ namespace Core.Thalos.BuildingBlocks.Configuration
options.Audience = jwtIssuerOptions?.Audience; options.Audience = jwtIssuerOptions?.Audience;
options.SigningCredentials = new SigningCredentials(rsaPrivateKey, SecurityAlgorithms.RsaSha256); options.SigningCredentials = new SigningCredentials(rsaPrivateKey, SecurityAlgorithms.RsaSha256);
}); });
services.AddSingleton<IOptions<JwtIssuerOptions>>(Microsoft.Extensions.Options.Options.Create(jwtIssuerOptions));
} }
public static void AddAzureAuthentication(AuthSettings authSettings, IConfiguration configuration, IServiceCollection services) 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) public static void AddGoogleAuthentication(IServiceCollection services, GoogleAuthSettings googleAuthSettings)
{ {
services.AddSingleton<GoogleAuthSettings>(googleAuthSettings);
services.AddAuthentication(options => services.AddAuthentication(options =>
{ {
options.DefaultAuthenticateScheme = Schemes.GoogleScheme; options.DefaultAuthenticateScheme = Schemes.GoogleScheme;

View File

@@ -10,6 +10,7 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Any; using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen; using Swashbuckle.AspNetCore.SwaggerGen;
using Swashbuckle.AspNetCore.SwaggerUI; using Swashbuckle.AspNetCore.SwaggerUI;
@@ -114,6 +115,12 @@ namespace Core.Thalos.BuildingBlocks.Configuration
c.AddSecurityDefinition(googleScheme, new OpenApiSecurityScheme c.AddSecurityDefinition(googleScheme, new OpenApiSecurityScheme
{ {
Type = SecuritySchemeType.OAuth2, Type = SecuritySchemeType.OAuth2,
Extensions = new Dictionary<string, IOpenApiExtension>
{
["x-tokenName"] = new OpenApiString("id_token")
},
Flows = new OpenApiOAuthFlows Flows = new OpenApiOAuthFlows
{ {
AuthorizationCode = new OpenApiOAuthFlow AuthorizationCode = new OpenApiOAuthFlow

View File

@@ -11,7 +11,10 @@ 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()
{ {
@@ -31,7 +34,7 @@ namespace Core.Thalos.BuildingBlocks
idToken, idToken,
new GoogleJsonWebSignature.ValidationSettings new GoogleJsonWebSignature.ValidationSettings
{ {
Audience = new[] { config["Authentication:Google:ClientId"]! } Audience = new[] { googleSettings.ClientId! }
}); });
} }
catch (InvalidJwtException) catch (InvalidJwtException)

View File

@@ -112,7 +112,7 @@ namespace Core.Thalos.BuildingBlocks
{ {
googleSettings.ClientId = (await keyVaultProvider.GetSecretAsync(Secrets.GoogleClientId, new CancellationToken { })).Secret.Value; ; googleSettings.ClientId = (await keyVaultProvider.GetSecretAsync(Secrets.GoogleClientId, new CancellationToken { })).Secret.Value; ;
googleSettings.ClientSecret = (await keyVaultProvider.GetSecretAsync(Secrets.GoogleClientSecret, 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; googleSettings.RedirectUri = builder.Configuration.GetSection(Secrets.GoogleLocalRedirectUri).Value;
} }
else else
{ {

View File

@@ -4,12 +4,12 @@ using Microsoft.Extensions.Configuration;
namespace Core.Thalos.BuildingBlocks 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 };
} }

9
nuget.config Normal file
View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<!-- Tu BaGet primero -->
<add key="BaGet" value="https://nuget.dream-views.com/v3/index.json" protocolVersion="3" />
<!-- NuGet oficial como fallback (si quieres) -->
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>