Skip to content

Commit 837b727

Browse files
committed
Just doing small code improvements, cleanup and typo fixes.
1 parent 675bfcb commit 837b727

File tree

5 files changed

+25
-89
lines changed

5 files changed

+25
-89
lines changed

docs/Griesoft.AspNetCore.ReCaptcha.xml

Lines changed: 0 additions & 64 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ReCaptcha/Configuration/RecaptchaServiceConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ internal class RecaptchaServiceConstants
77
{
88
internal const string GoogleRecaptchaEndpoint = "https://www.google.com/recaptcha/api/siteverify";
99
internal const string TokenKeyName = "G-Recaptcha-Response";
10+
internal const string TokenKeyNameLower = "g-recaptcha-response";
1011
internal const string SettingsSectionKey = "RecaptchaSettings";
1112
}
1213
}

src/ReCaptcha/Extensions/TagHelperOutputExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ internal static class TagHelperOutputExtensions
1818
{
1919
private static readonly char[] SpaceChars = { '\u0020', '\u0009', '\u000A', '\u000C', '\u000D' };
2020

21-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1307:Specify StringComparison", Justification = "<Pending>")]
2221
internal static string AddQueryString(string uri, IEnumerable<KeyValuePair<string, string>> queryString)
2322
{
2423
if (uri == null)

src/ReCaptcha/Filters/ValidateRecaptchaFilter.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE
3939

4040
ValidationResponse validationResponse;
4141

42-
if (!TryGetRecaptchaToken(context.HttpContext.Request, out string? token))
42+
if (!ValidateRecaptchaFilter.TryGetRecaptchaToken(context.HttpContext.Request, out string? token))
4343
{
4444
_logger.LogWarning(Resources.RecaptchaResponseTokenMissing);
4545

@@ -57,7 +57,7 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE
5757
validationResponse = await _recaptchaService.ValidateRecaptchaResponse(token, GetRemoteIp(context)).ConfigureAwait(true);
5858
}
5959

60-
TryAddResponseToActionAguments(context, validationResponse);
60+
ValidateRecaptchaFilter.TryAddResponseToActionAguments(context, validationResponse);
6161

6262
if (!ShouldShortCircuit(context, validationResponse))
6363
{
@@ -71,19 +71,34 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE
7171
context.HttpContext.Connection.RemoteIpAddress.ToString() :
7272
null;
7373
}
74-
private bool TryGetRecaptchaToken(HttpRequest request, [NotNullWhen(true)] out string? token)
74+
private bool ShouldShortCircuit(ActionExecutingContext context, ValidationResponse response)
75+
{
76+
if (!response.Success)
77+
{
78+
_logger.LogInformation(Resources.InvalidResponseTokenMessage);
79+
80+
if (OnValidationFailedAction == ValidationFailedAction.BlockRequest)
81+
{
82+
context.Result = new RecaptchaValidationFailedResult();
83+
return true;
84+
}
85+
}
86+
87+
return false;
88+
}
89+
private static bool TryGetRecaptchaToken(HttpRequest request, [NotNullWhen(true)] out string? token)
7590
{
7691
if (request.Headers.ContainsKey(RecaptchaServiceConstants.TokenKeyName))
7792
{
7893
token = request.Headers[RecaptchaServiceConstants.TokenKeyName];
7994
}
80-
else if (request.HasFormContentType && request.Form.ContainsKey(RecaptchaServiceConstants.TokenKeyName.ToLowerInvariant()))
95+
else if (request.HasFormContentType && request.Form.ContainsKey(RecaptchaServiceConstants.TokenKeyNameLower))
8196
{
82-
token = request.Form[RecaptchaServiceConstants.TokenKeyName.ToLowerInvariant()];
97+
token = request.Form[RecaptchaServiceConstants.TokenKeyNameLower];
8398
}
84-
else if (request.Query.ContainsKey(RecaptchaServiceConstants.TokenKeyName.ToLowerInvariant()))
99+
else if (request.Query.ContainsKey(RecaptchaServiceConstants.TokenKeyNameLower))
85100
{
86-
token = request.Query[RecaptchaServiceConstants.TokenKeyName.ToLowerInvariant()];
101+
token = request.Query[RecaptchaServiceConstants.TokenKeyNameLower];
87102
}
88103
else
89104
{
@@ -92,22 +107,7 @@ private bool TryGetRecaptchaToken(HttpRequest request, [NotNullWhen(true)] out s
92107

93108
return token != null;
94109
}
95-
private bool ShouldShortCircuit(ActionExecutingContext context, ValidationResponse response)
96-
{
97-
if (!response.Success)
98-
{
99-
_logger.LogInformation(Resources.InvalidResponseTokenMessage);
100-
101-
if (OnValidationFailedAction == ValidationFailedAction.BlockRequest)
102-
{
103-
context.Result = new RecaptchaValidationFailedResult();
104-
return true;
105-
}
106-
}
107-
108-
return false;
109-
}
110-
private void TryAddResponseToActionAguments(ActionExecutingContext context, ValidationResponse response)
110+
private static void TryAddResponseToActionAguments(ActionExecutingContext context, ValidationResponse response)
111111
{
112112
if (context.ActionArguments.Any(pair => pair.Value is ValidationResponse))
113113
{

src/ReCaptcha/Models/ValidationResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class ValidationResponse
4242
public string Hostname { get; set; } = string.Empty;
4343

4444
/// <summary>
45-
/// List of <see cref="ValidationError"/>'s, if any occured.
45+
/// List of <see cref="ValidationError"/>'s, if any occurred.
4646
/// </summary>
4747
[JsonIgnore]
4848
public IEnumerable<ValidationError> Errors => GetValidationErrors();

0 commit comments

Comments
 (0)