Created
May 26, 2015 19:18
-
-
Save kiyor/1fbacc44f308d7c0e0fe to your computer and use it in GitHub Desktop.
// idea by 'github.com/graetzer/go-recaptcha' with optomize, but might not fit for everyone
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. | |
* File Name : recaptcha.go | |
* Purpose : | |
* Creation Date : 03-22-2015 | |
* Last Modified : Sun 22 Mar 2015 09:43:16 PM PDT | |
* Created By : Kiyor | |
_._._._._._._._._._._._._._._._._._._._._.*/ | |
// idea by 'github.com/graetzer/go-recaptcha' | |
// with optomize, but might not fit for everyone | |
package recaptcha | |
import ( | |
"encoding/json" | |
"errors" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"net/url" | |
) | |
// https://www.google.com/recaptcha/api/siteverify?secret=your_secret&response=response_string&remoteip=user_ip_address | |
const recaptcha_url = "https://www.google.com/recaptcha/api/siteverify?" | |
var recaptcha_secret string | |
// check uses the client ip address and the client's response input to determine whether or not | |
// the client answered the reCaptcha input question correctly. | |
// It returns a boolean value indicating whether or not the client answered correctly. | |
func check(args ...string) (body []byte, err error) { | |
response := args[0] | |
vals := url.Values{"secret": {recaptcha_secret}, "response": {response}} | |
if len(args) > 1 { | |
remoteip := args[1] | |
vals["remoteip"] = []string{remoteip} | |
} | |
resp, err := http.Get(recaptcha_url + vals.Encode()) | |
if err != nil { | |
return | |
} | |
defer resp.Body.Close() | |
body, err = ioutil.ReadAll(resp.Body) | |
if err != nil { | |
return | |
} | |
return | |
} | |
// Confirm is the public interface function. | |
// It calls check, which the client ip address, the challenge code from the reCaptcha form, | |
// and the client's response input to that challenge to determine whether or not | |
// the client answered the reCaptcha input question correctly. | |
// It returns a boolean value indicating whether or not the client answered correctly. | |
func Confirm(args ...string) (bool, error) { | |
var v map[string]interface{} | |
var body []byte | |
var err error | |
switch len(args) { | |
case 1: | |
body, err = check(args[0]) | |
case 2: | |
body, err = check(args[0], args[1]) | |
default: | |
return false, errors.New("args should be response, remoteip(Optional)") | |
} | |
if err != nil { | |
return false, err | |
} | |
if err := json.Unmarshal(body, &v); err != nil { | |
return false, err | |
} | |
if e, found := v["error-codes"]; found { | |
return false, errors.New(fmt.Sprint(e)) | |
} | |
success, ok := v["success"].(bool) | |
return ok && success, nil | |
} | |
// Init allows the webserver or code evaluating the reCaptcha form input to set the | |
// reCaptcha secret (string) value. | |
func Init(secret string) { | |
recaptcha_secret = secret | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment