michaelvs97 / AspNetCore.ReCaptcha

Google ReCAPTCHA v2/v3 Library for .NET Core 3.x/5.x
https://www.nuget.org/packages/AspNetCore.ReCaptcha/
MIT License
73 stars 20 forks source link

Adding logging of (mainly) failed reCAPTCHA responses for easier debugging #32

Closed Driedas closed 2 years ago

Driedas commented 2 years ago

Is your feature request related to a problem? Please describe. While the library is definitely functional, debugging of failed captcha verifications is nigh impossible (as far as I can tell). The error-codes field in the response from Google reCAPTCHA is ignored and there is no out-of-the-box logging of responses

Describe the solution you'd like I've forked the source code and took a stab at implementing something, I've: 1) added an ErrorCodes property for the ReCaptchaResponse class to receive the error codes returned by the API

        [JsonPropertyName("error-codes")]
        public string[] ErrorCodes { get; set; }

2) changed the ReCaptchaService.GetVerifyResponseAsync method to include the following:

            var stringResult = await result.Content.ReadAsStringAsync();

            var obj = JsonSerializer.Deserialize<ReCaptchaResponse>(stringResult);

            _logger?.Log(obj.Success ? LogLevel.Information : LogLevel.Warning, stringResult);

            return obj;

and injected an optional ILogger<IReCaptchaService> logger into the ctor.

It seems to work, if the above changes look ok to you, would you accept a PR with this? Alternatively feel free to add yourself if you like it.

Describe alternatives you've considered One alternative that I've tried is decorating the registered IReCaptchaService service with a logging wrapper, but that is a bit clunky and does not really give access to the raw response from the reCAPTCHA API that still has the error codes property in it...

sleeuwen commented 2 years ago

Thanks for suggestion the change, this can be very useful to anyone. We've implemented handling of error codes in the latest version of the library v1.5.2.

Based on the type of error code returned we now either log a warning or debug level message, when the error code is one of missing-secret-key, invalid-secret-key, it will be logged as a warning, any other error will be logged at level debug. In addition to that, the full response will also be logged as a trace level message. This should enable you to debug issues more easily by setting the log level to debug or trace when debugging, and not spam the logs for normal operations (if a user did not pass the recaptcha it will log this at level debug which is lower than the default log level of Information).

michaelvs97 commented 2 years ago

Hi @Driedas ,

I'm closing this issue since we've implemented the error logging as mentioned by @sleeuwen. If you feel any need to reopen this issue, please feel free to do so when needed.

Driedas commented 2 years ago

@sleeuwen @michaelvs97 thanks a lot guys, looks even better than I hoped :). A really fast turnaround!