Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Driedas opened this issue Feb 7, 2022 · 3 comments
Closed

Comments

@Driedas
Copy link

Driedas commented Feb 7, 2022

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; }
  1. 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
Copy link
Collaborator

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
Copy link
Owner

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
Copy link
Author

Driedas commented Feb 16, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants