Skip to content

IsaacBell/vibe_recaptcha

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vibe reCAPTCHA

Implement Google's reCaptcha service in Vibe.d.

Before You Get Started

You'll need to sign up for API Keys before you can use reCaptcha

Installation

To install, add vibe_recaptcha to your package.json file:

	"dependencies": {
		"vibe-d":">=0.7.18",
		"vibe_recaptcha":">=1.0"
	}

Import the module in your app.d file:

	import recaptcha;

Displaying the CAPTCHA image

The CAPTCHA image is generated in two steps

1. Call function recaptchaHTML(string publickey) in your app.d file, and make it visible to your view:

app.d

void index(HTTPServerRequest req, HTTPServerResponse res)
{
	string recaptcha = recaptchaHTML("your public key");
	res.renderCompat!("index.dt",
		HTTPServerRequest, "req",
		string, "recaptcha")
		(req, recaptcha);
}

2. From there, render the variable from within your view:

index.dt

form(action='/verify',method='post')
	// Your form fields here
	!= recaptcha
	input(type='submit')

Verifying the Solution

Check the response with verifyRecaptcha(string privateKey, string ip, string challenge, string response)

void verify(HTTPServerRequest req, HTTPServerResponse res)
{
	string challenge = req.form["recaptcha_challenge_field"];
	string response = req.form["recaptcha_response_field"];
	if ( verifyRecaptcha("private key", req.peer, challenge, response) )
		// Handle the success response
		res.redirect("/success");
	else
		// Handle the failure response
		res.writeBody(cast(ubyte[]) "Failure.", "text/plain");
}

Test API Keys

To check for errors during verification, call function testRecaptcha instead of verifyRecaptcha to see the response from reCAPTCHA when a response is inputted:

// import vibe.core.log
void verify(HTTPServerRequest req, HTTPServerResponse res)
{
	string challenge = req.form["recaptcha_challenge_field"];
	string response = req.form["recaptcha_response_field"];
// Returns either "true" or an error message
string recaptchaMessage = testRecaptcha("private key", req.peer, challenge, response);
logInfo("Recaptcha's Response: %s", recaptchaMessage);

}

About

Implement Google's reCaptcha service in Vibe.d

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages