HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Google reCAPTCHA Validator: Iteration II

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
recaptchaiterationvalidatorgoogle

Problem

This is a follow-up to the other post I made (less than an hour ago) about my Google reCAPTCHA C# implementation: Google reCAPTCHA Validator

This adds support for error messages, so that you can determine why a reCAPTCHA request failed (if an error was returned).

This also fixes a bug if you were to use the ReCaptchaLocationInclude before having set the ExtraClasses to a non-null list.

I added an enumeration for errors:

/// 
/// Indicates errors that could be returned by the reCAPTCHA API.
/// 
/// 
/// See: https://developers.google.com/recaptcha/docs/verify
/// 
[Flags]
public enum ReCaptchaErrors
{
    /// 
    /// No errors occurred.
    /// 
    None = 0x00,
    /// 
    /// The secret parameter is missing.
    /// 
    MissingInputSecret = 0x01,
    /// 
    /// The secret parameter is invalid or malformed.
    /// 
    InvalidInputSecret = 0x02,
    /// 
    /// The response parameter is missing.
    /// 
    MissingInputResponse = 0x04,
    /// 
    /// The response parameter is invalid or malformed.
    /// 
    InvalidInputResponse = 0x08,
}


A class for reCAPTCHA responses:

```
///
/// This class is used by the to return a proper response to a reCAPTCHA validation request.
///
public class ReCaptchaResponse
{
private bool _Success;
private ReCaptchaErrors _Errors;

///
/// Returns a value indicating if the succeeded in validating the reCAPTCHA response or not.
///
public bool Success { get { return _Success; } }

///
/// Returns any that occurred during the reCAPTCHA response validation.
///
public ReCaptchaErrors Errors { get { return _Errors; } }

///
/// Creates a new from the specified JSON string.
///
/// The JSON string to transform.
public ReCaptchaResponse(string jsonResponse)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
dynamic deserializedJson = jss.DeserializeObject(jsonResponse);

_Success = deserializedJson["success

Solution

[Flags]
public enum ReCaptchaErrors
{
    /// 
    /// No errors occurred.
    /// 
    None = 0x00,
    /// 
    /// The secret parameter is missing.
    /// 
    MissingInputSecret = 0x01,
    /// 
    /// The secret parameter is invalid or malformed.
    /// 
    InvalidInputSecret = 0x02,
    /// 
    /// The response parameter is missing.
    /// 
    MissingInputResponse = 0x04,
    /// 
    /// The response parameter is invalid or malformed.
    /// 
    InvalidInputResponse = 0x08,
}


It appears you are assigning these values so the binary values of the enum options are 0001, 0010, 0100, 1000, etc... This is so you an assign multiple errors to a single variable, correct?

It would be clearer what exactly you are trying to do if you used bitshifting, like this:

[Flags]
public enum ReCaptchaErrors
{
    /// 
    /// No errors occurred.
    /// 
    None = 0,
    /// 
    /// The secret parameter is missing.
    /// 
    MissingInputSecret = 1 
    /// The secret parameter is invalid or malformed.
    /// 
    InvalidInputSecret = 1 
    /// The response parameter is missing.
    /// 
    MissingInputResponse = 1 
    /// The response parameter is invalid or malformed.
    /// 
    InvalidInputResponse = 1 << 3,
}


Now, you can't accidentally break your code by typing 0x09 instead of 0x08, and it is immediately clear that you want each flag to be its own bit. For more information, you can read this blog post by our own @nhgrif.

Code Snippets

[Flags]
public enum ReCaptchaErrors
{
    /// <summary>
    /// No errors occurred.
    /// </summary>
    None = 0x00,
    /// <summary>
    /// The secret parameter is missing.
    /// </summary>
    MissingInputSecret = 0x01,
    /// <summary>
    /// The secret parameter is invalid or malformed.
    /// </summary>
    InvalidInputSecret = 0x02,
    /// <summary>
    /// The response parameter is missing.
    /// </summary>
    MissingInputResponse = 0x04,
    /// <summary>
    /// The response parameter is invalid or malformed.
    /// </summary>
    InvalidInputResponse = 0x08,
}
[Flags]
public enum ReCaptchaErrors
{
    /// <summary>
    /// No errors occurred.
    /// </summary>
    None = 0,
    /// <summary>
    /// The secret parameter is missing.
    /// </summary>
    MissingInputSecret = 1 << 0,
    /// <summary>
    /// The secret parameter is invalid or malformed.
    /// </summary>
    InvalidInputSecret = 1 << 1,
    /// <summary>
    /// The response parameter is missing.
    /// </summary>
    MissingInputResponse = 1 << 2,
    /// <summary>
    /// The response parameter is invalid or malformed.
    /// </summary>
    InvalidInputResponse = 1 << 3,
}

Context

StackExchange Code Review Q#100604, answer score: 4

Revisions (0)

No revisions yet.