Skip to content

Commit

Permalink
added configurable reCAPTCHA API keys, in app settings
Browse files Browse the repository at this point in the history
  • Loading branch information
half-ogre committed Feb 7, 2012
1 parent d95cdd9 commit 35d10bb
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 8 deletions.
2 changes: 2 additions & 0 deletions PoliteCaptcha/Const.cs
Expand Up @@ -11,6 +11,8 @@ public static class Const
public const string ReCaptchControlId = "RudeCaptcha";
public const string ReCaptchaLocalhostPrivateKey = "6LehOM0SAAAAAC5LsEpHoyyMqJcz7f_zEfqm66um";
public const string ReCaptchaLocalhostPublicKey = "6LehOM0SAAAAAPgsjOy-6_grqy1JiB_W_jJa_aCw";
public const string ReCaptchaPrivateKeyAppSettingKey = "reCAPTCHA::PrivateKey";
public const string ReCaptchaPublicKeyAppSettingKey = "reCAPTCHA::PublicKey";
public const string ReCaptchaResponseField = "recaptcha_response_field";
}
}
9 changes: 9 additions & 0 deletions PoliteCaptcha/ErrorMessage.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions PoliteCaptcha/ErrorMessage.resx
Expand Up @@ -117,6 +117,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="DefaultReCaptchApiKeysOnlyAllowedForLocalRequest" xml:space="preserve">
<value>The default reCAPTCHA API keys may only be used for local requests. Configure your app's own API keys in app settings.</value>
</data>
<data name="HttpContextMustNotBeNull" xml:space="preserve">
<value>The HTTP context must not be null.</value>
</data>
Expand Down
1 change: 1 addition & 0 deletions PoliteCaptcha/PoliteCaptcha.csproj
Expand Up @@ -37,6 +37,7 @@
<HintPath>..\packages\recaptcha.1.0.5.0\lib\.NetFramework 4.0\Recaptcha.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
Expand Down
26 changes: 24 additions & 2 deletions PoliteCaptcha/ReCaptchaGenerator.cs
@@ -1,4 +1,5 @@
using System;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Mvc;
Expand All @@ -11,11 +12,32 @@ public class ReCaptchaGenerator : ICaptchaGenerator
{
public IHtmlString Generate(HtmlHelper htmlHelper)
{
if (htmlHelper == null)
throw new ArgumentNullException("htmlHelper");

var publicApiKey = ConfigurationManager.AppSettings[Const.ReCaptchaPublicKeyAppSettingKey];
if (publicApiKey == null)
{
if (!htmlHelper.ViewContext.HttpContext.Request.IsLocal)
throw new InvalidOperationException(ErrorMessage.DefaultReCaptchApiKeysOnlyAllowedForLocalRequest);

publicApiKey = Const.ReCaptchaLocalhostPublicKey;
}

var privateApiKey = ConfigurationManager.AppSettings[Const.ReCaptchaPrivateKeyAppSettingKey];
if (privateApiKey == null)
{
if (!htmlHelper.ViewContext.HttpContext.Request.IsLocal)
throw new InvalidOperationException(ErrorMessage.DefaultReCaptchApiKeysOnlyAllowedForLocalRequest);

privateApiKey = Const.ReCaptchaLocalhostPrivateKey;
}

var recaptchaControl = new RecaptchaControl
{
ID = Const.ReCaptchControlId,
PublicKey = Const.ReCaptchaLocalhostPublicKey,
PrivateKey = Const.ReCaptchaLocalhostPrivateKey,
PublicKey = publicApiKey,
PrivateKey = privateApiKey,
};

var htmlWriter = new HtmlTextWriter(new StringWriter());
Expand Down
18 changes: 13 additions & 5 deletions PoliteCaptcha/ReCaptchaValidator.cs
@@ -1,4 +1,5 @@
using System;
using System.Configuration;
using System.Web;
using Recaptcha;

Expand All @@ -10,17 +11,23 @@ public class ReCaptchaValidator : ICaptchaValidator

public ReCaptchaValidator()
{
recaptchaValidator = new RecaptchaValidator
{
PrivateKey = Const.ReCaptchaLocalhostPrivateKey,
};
recaptchaValidator = new RecaptchaValidator();
}

public bool Validate(HttpContextBase httpContext)
{
if (httpContext == null)
throw new ArgumentNullException("httpContext");

var privateApiKey = ConfigurationManager.AppSettings[Const.ReCaptchaPrivateKeyAppSettingKey];
if (privateApiKey == null)
{
if (!httpContext.Request.IsLocal)
throw new InvalidOperationException(ErrorMessage.DefaultReCaptchApiKeysOnlyAllowedForLocalRequest);

privateApiKey = Const.ReCaptchaLocalhostPrivateKey;
}

var challenge = httpContext.Request.Form[Const.ReCaptchaChallengeField];
if (string.IsNullOrWhiteSpace(challenge))
return false;
Expand All @@ -29,9 +36,10 @@ public bool Validate(HttpContextBase httpContext)
if (string.IsNullOrWhiteSpace(response))
return false;

recaptchaValidator.PrivateKey = privateApiKey;
recaptchaValidator.RemoteIP = httpContext.Request.UserHostAddress;
recaptchaValidator.Challenge = challenge;
recaptchaValidator.Response = response;
recaptchaValidator.RemoteIP = httpContext.Request.UserHostAddress;

return recaptchaValidator.Validate().IsValid;
}
Expand Down
2 changes: 1 addition & 1 deletion Sample/RequestModels/SendFeedbackRequest.cs
Expand Up @@ -8,7 +8,7 @@ public class SendFeedbackRequest
[Required, DataType(DataType.EmailAddress)]
public string EmailAddress { get; set; }

[Required]
[Required, DataType(DataType.MultilineText)]
public string Feedback { get; set; }
}
}

0 comments on commit 35d10bb

Please sign in to comment.