Skip to content

Commit

Permalink
Ioptions snapshot (#65)
Browse files Browse the repository at this point in the history
* Consistently use IOptionsSnapshot instead of IOptions

* Update version to 1.8.1
  • Loading branch information
sleeuwen authored Jan 10, 2024
1 parent 70a797c commit 1cfe488
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 10 deletions.
1 change: 1 addition & 0 deletions AspNetCore.ReCaptcha.Tests/ReCaptchaHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public ReCaptchaHelperTests()

var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<IOptions<ReCaptchaSettings>>(reCaptchaSettingsSnapshot);
serviceCollection.AddSingleton<IOptionsSnapshot<ReCaptchaSettings>>(reCaptchaSettingsSnapshot);

ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();

Expand Down
2 changes: 1 addition & 1 deletion AspNetCore.ReCaptcha.Tests/ReCaptchaServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private ReCaptchaService CreateService(HttpClient httpClient = null, ReCaptchaSe
};
}

var reCaptchaSettingsMock = new Mock<IOptions<ReCaptchaSettings>>();
var reCaptchaSettingsMock = new Mock<IOptionsSnapshot<ReCaptchaSettings>>();
reCaptchaSettingsMock.Setup(x => x.Value).Returns(reCaptchaSettings);

logger ??= new NullLogger<ReCaptchaService>();
Expand Down
27 changes: 25 additions & 2 deletions AspNetCore.ReCaptcha.Tests/ValidateReCaptchaAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,20 @@ public async Task VerifyAsyncLocalizesErrorMessage()
serviceProviderMock.Setup(x => x.GetService(typeof(IStringLocalizerFactory)))
.Returns(stringLocalizerFactory.Object);

var reCaptchaSettings = new ReCaptchaSettings
{
LocalizerProvider = (type, factory) => factory.Create(type),
};

serviceProviderMock.Setup(x => x.GetService(typeof(IOptions<ReCaptchaSettings>)))
.Returns(new OptionsWrapper<ReCaptchaSettings>(new ReCaptchaSettings { LocalizerProvider = (type, factory) => factory.Create(type) }));
.Returns(new OptionsWrapper<ReCaptchaSettings>(reCaptchaSettings));

var reCaptchaSettingsMock = new Mock<IOptionsSnapshot<ReCaptchaSettings>>();
reCaptchaSettingsMock.Setup(x => x.Value)
.Returns(reCaptchaSettings);

serviceProviderMock.Setup(x => x.GetService(typeof(IOptionsSnapshot<ReCaptchaSettings>)))
.Returns(reCaptchaSettingsMock.Object);

var httpContextMock = new Mock<HttpContext>();
httpContextMock.Setup(x => x.RequestServices)
Expand Down Expand Up @@ -361,8 +373,19 @@ public async Task VerifyAsyncLocalizesErrorMessage()
serviceProviderMock.Setup(x => x.GetService(typeof(IStringLocalizerFactory)))
.Returns(stringLocalizerFactory.Object);

var reCaptchaSettings = new ReCaptchaSettings
{
LocalizerProvider = (type, factory) => factory.Create(type),
};
serviceProviderMock.Setup(x => x.GetService(typeof(IOptions<ReCaptchaSettings>)))
.Returns(new OptionsWrapper<ReCaptchaSettings>(new ReCaptchaSettings { LocalizerProvider = (type, factory) => factory.Create(type) }));
.Returns(new OptionsWrapper<ReCaptchaSettings>(reCaptchaSettings));

var reCaptchaSettingsMock = new Mock<IOptionsSnapshot<ReCaptchaSettings>>();
reCaptchaSettingsMock.Setup(x => x.Value)
.Returns(reCaptchaSettings);

serviceProviderMock.Setup(x => x.GetService(typeof(IOptionsSnapshot<ReCaptchaSettings>)))
.Returns(reCaptchaSettingsMock.Object);

var httpContextMock = new Mock<HttpContext>();
httpContextMock.Setup(x => x.RequestServices)
Expand Down
2 changes: 1 addition & 1 deletion AspNetCore.ReCaptcha/AspNetCore.ReCaptcha.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<PackageId>AspNetCore.ReCaptcha</PackageId>
<Version>1.8.0</Version>
<Version>1.8.1</Version>
<Authors>Michaelvs97,sleeuwen</Authors>
<Description>Google ReCAPTCHA v2/v3 Library for .NET 6 and above</Description>
<PackageDescription>Google ReCAPTCHA v2/v3 Library for .NET 6 and above</PackageDescription>
Expand Down
13 changes: 9 additions & 4 deletions AspNetCore.ReCaptcha/ReCaptchaHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,22 @@ public static IHtmlContent ReCaptcha(
language ??= helper.ViewContext.HttpContext.Features.Get<IRequestCultureFeature>()?.RequestCulture
.UICulture.TwoLetterISOLanguageName;

var settings = helper.ViewContext.HttpContext.RequestServices.GetRequiredService<IOptions<ReCaptchaSettings>>().Value;
// Url can't be updated while the app is running as it is used as BaseUri in the HttpClient registration.
var staticSettings = helper.ViewContext.HttpContext.RequestServices.GetRequiredService<IOptions<ReCaptchaSettings>>().Value;
var settings = helper.ViewContext.HttpContext.RequestServices.GetRequiredService<IOptionsSnapshot<ReCaptchaSettings>>().Value;

if (!settings.Enabled)
return HtmlString.Empty;

switch (settings.Version)
{
default:
case ReCaptchaVersion.V2:
return ReCaptchaGenerator.ReCaptchaV2(settings.RecaptchaBaseUrl, settings.SiteKey, size, theme, language, callback, errorCallback, expiredCallback, autoTheme, nonce);
return ReCaptchaGenerator.ReCaptchaV2(staticSettings.RecaptchaBaseUrl, settings.SiteKey, size, theme, language, callback, errorCallback, expiredCallback, autoTheme, nonce);
case ReCaptchaVersion.V2Invisible:
return ReCaptchaGenerator.ReCaptchaV2Invisible(settings.RecaptchaBaseUrl, settings.SiteKey, text, className, language, callback, badge);
return ReCaptchaGenerator.ReCaptchaV2Invisible(staticSettings.RecaptchaBaseUrl, settings.SiteKey, text, className, language, callback, badge);
case ReCaptchaVersion.V3:
return ReCaptchaGenerator.ReCaptchaV3(settings.RecaptchaBaseUrl, settings.SiteKey, action, language, callback, ReCaptchaGenerator.GenerateId(helper.ViewContext), nonce);
return ReCaptchaGenerator.ReCaptchaV3(staticSettings.RecaptchaBaseUrl, settings.SiteKey, action, language, callback, ReCaptchaGenerator.GenerateId(helper.ViewContext), nonce);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion AspNetCore.ReCaptcha/ReCaptchaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class ReCaptchaService : IReCaptchaService
private readonly ILogger<ReCaptchaService> _logger;
private readonly ReCaptchaSettings _reCaptchaSettings;

public ReCaptchaService(HttpClient client, IOptions<ReCaptchaSettings> reCaptchaSettings, ILogger<ReCaptchaService> logger)
public ReCaptchaService(HttpClient client, IOptionsSnapshot<ReCaptchaSettings> reCaptchaSettings, ILogger<ReCaptchaService> logger)
{
_client = client;
_logger = logger;
Expand Down
2 changes: 1 addition & 1 deletion AspNetCore.ReCaptcha/ValidateReCaptchaAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private string GetErrorMessage(ActionContext context)
var localizerFactory = context.HttpContext.RequestServices.GetService<IStringLocalizerFactory>();
if (localizerFactory != null)
{
var settings = context.HttpContext.RequestServices.GetRequiredService<IOptions<ReCaptchaSettings>>();
var settings = context.HttpContext.RequestServices.GetRequiredService<IOptionsSnapshot<ReCaptchaSettings>>();

IStringLocalizer localizer = null;
if (context.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor)
Expand Down

0 comments on commit 1cfe488

Please sign in to comment.