Skip to content

Commit

Permalink
Merge pull request #20428 from abpframework/AttachCultureInfo
Browse files Browse the repository at this point in the history
Attach auth server current culture info to response.
  • Loading branch information
hikalkan authored Aug 28, 2024
2 parents a08044e + 27fa135 commit bf3f76c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.OAuth.Claims;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.RequestLocalization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Volo.Abp;
using Volo.Abp.AspNetCore.Authentication.OpenIdConnect;
using Volo.Abp.AspNetCore.MultiTenancy;
using Volo.Abp.Localization;
using Volo.Abp.Security.Claims;

namespace Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -52,15 +56,36 @@ public static AuthenticationBuilder AddAbpOpenIdConnect(this AuthenticationBuild
options.Events.OnTokenValidated = async (context) =>
{
var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<AbpAspNetCoreAuthenticationOpenIdConnectModule>>();
var client = context.HttpContext.RequestServices.GetRequiredService<IOpenIdLocalUserCreationClient>();
try
{
await client.CreateOrUpdateAsync(context);
}
catch (Exception ex)
{
var logger = context.HttpContext.RequestServices.GetService<ILogger<AbpAspNetCoreAuthenticationOpenIdConnectModule>>();
logger?.LogException(ex);
logger.LogException(ex);
}
var culture = context.ProtocolMessage.GetParameter("culture");
var uiCulture = context.ProtocolMessage.GetParameter("ui-culture");
if (CultureHelper.IsValidCultureCode(culture) && CultureHelper.IsValidCultureCode(uiCulture))
{
context.Response.OnStarting(() =>
{
logger.LogInformation($"Setting culture and ui-culture to the response. culture: {culture}, ui-culture: {uiCulture}");
AbpRequestCultureCookieHelper.SetCultureCookie(
context.HttpContext,
new RequestCulture(culture, uiCulture)
);
return Task.CompletedTask;
});
}
else
{
logger.LogWarning($"Invalid culture or ui-culture parameter in the OpenIdConnect response. culture: {culture}, ui-culture: {uiCulture}");
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Volo.Abp.AspNetCore.MultiTenancy;
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared;
using Volo.Abp.Modularity;
using Volo.Abp.OpenIddict.Globalization;
using Volo.Abp.OpenIddict.Scopes;
using Volo.Abp.OpenIddict.WildcardDomains;
using Volo.Abp.Security.Claims;
Expand Down Expand Up @@ -135,6 +136,7 @@ private void AddOpenIddictServer(IServiceCollection services)
builder.AddEventHandler(RemoveClaimsFromClientCredentialsGrantType.Descriptor);
builder.AddEventHandler(AttachScopes.Descriptor);
builder.AddEventHandler(AttachCultureInfo.Descriptor);
services.ExecutePreConfiguredActions(builder);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public class AbpOpenIddictAspNetCoreOptions
/// Default: true.
/// </summary>
public bool AddDevelopmentEncryptionAndSigningCertificate { get; set; } = true;

/// <summary>
/// Attach auth server current culture info to response.
/// </summary>
public bool AttachCultureInfo { get; set; } = true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Globalization;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using OpenIddict.Server;

namespace Volo.Abp.OpenIddict.Globalization;

public class AttachCultureInfo : IOpenIddictServerHandler<OpenIddictServerEvents.ApplyAuthorizationResponseContext>
{
public static OpenIddictServerHandlerDescriptor Descriptor { get; }
= OpenIddictServerHandlerDescriptor.CreateBuilder<OpenIddictServerEvents.ApplyAuthorizationResponseContext>()
.UseSingletonHandler<AttachCultureInfo>()
.SetOrder(OpenIddictServerHandlers.Authentication.AttachIssuer.Descriptor.Order + 1_000)
.SetType(OpenIddictServerHandlerType.Custom)
.Build();

protected IOptions<AbpOpenIddictAspNetCoreOptions> Options { get; }

public AttachCultureInfo(IOptions<AbpOpenIddictAspNetCoreOptions> options)
{
Options = options;
}

public ValueTask HandleAsync(OpenIddictServerEvents.ApplyAuthorizationResponseContext context)
{
Check.NotNull(context, nameof(context));

if (Options.Value.AttachCultureInfo)
{
if (!context.Response.HasParameter("culture"))
{
context.Response.SetParameter("culture", CultureInfo.CurrentCulture.Name);
}

if (!context.Response.HasParameter("ui-culture"))
{
context.Response.SetParameter("ui-culture", CultureInfo.CurrentUICulture.Name);
}
}

return default;
}
}

0 comments on commit bf3f76c

Please sign in to comment.