Using reCAPTCHA with Golang

Overview

Google’s reCAPTCHA is one of the tool we can use to stop malicious internet bots from abusing our web applications.
It comes in two versions, reCAPTCHA v2 and v3. Version 3 uses a score based and no-interaction approach to handle bots from humans. Version 2 uses use a checkbox that will require users to answer a question. In this tutorial we will focus on reCAPTCHA v2

Prerequisite

This tutorial requires:

  • You are already a registered google webmaster
  • Added your website property into your Google webmaster account
  • You know how to build a basic web app using Golang and Rice

Registration Steps

1. Let’s begin by heading to https://www.google.com/recaptcha/ to register our site with reCAPTCHA.

2. Add a site for reCAPTCHA

3. Register site details

If you want to configure and test using your localhost just add localhost in the domain list

3.1 Generated Site Keys
Once you’ve successfully registered your website with reCAPTCHA. It will generate client and server side keys. We will use this later in our codes.

Nutshell of Google reCAPTCHA Process

Before we dig into the code this is an over simplified process of how Google reCAPTCHA works.

Code

1. Add reCAPTCHA Javascript library into the HTML head tag

2. Add the g-recaptcha div inside your form tag

3. Golang code

To compile this project you can use the following commands

Challenge

Challenge Answered!

Demo and Source Code

You can checkout the completed demo here
https://golangrecaptcha.johnpili.com/

Source code is available in Github.
https://github.com/johnpili/using-recaptcha-with-golang

Conclusion

In today’s Internet, using reCAPTCHA is important in ensuring real human interaction instead of an automated software. Machine learning and automated testing tools are sometimes being used with malicious intent and protecting our website is our responsibility as developers.

Originally published at https://johnpili.com on January 18, 2020.

I am currently working as a software development manager in Malaysia. I manage and help my team with technical software design and implementation.