Skip to content

Commit

Permalink
fix issue with recaptcha verification
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzarghili committed Feb 19, 2018
1 parent 7001160 commit b4e5104
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions recaptcha.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ var recaptchaSecret string

const reCAPTCHALink = "https://www.google.com/recaptcha/api/siteverify"

type reCHAPTCHRequest struct {
type reCHAPTCHARequest struct {
Secret string `json:"secret"`
Response string `json:"response"`
RemoteIP string `json:"remoteip,omitempty"`
}

type reCHAPTCHResponse struct {
type reCHAPTCHAResponse struct {
Success bool `json:"success"`
ChallengeTS time.Time `json:"challenge_ts"`
Hostname string `json:"hostname"`
Expand All @@ -32,33 +32,35 @@ func Init(ReCAPTCHASecret string) {
}

// Verify returns (true, nil) if no error the client answered the challenge correctly and have correct remoteIP
func Verify(challenResponse string, remoteIP string) (bool, error) {
body := reCHAPTCHRequest{Secret: recaptchaSecret, Response: challenResponse, RemoteIP: remoteIP}
func Verify(challengeResponse string, remoteIP string) (bool, error) {
body := reCHAPTCHARequest{Secret: recaptchaSecret, Response: challengeResponse, RemoteIP: remoteIP}
return confirm(body)
}

// VerifyNoRemoteIP returns (true, nil) if no error and the client answered the challenge correctly
func VerifyNoRemoteIP(challenResponse string) (bool, error) {
body := reCHAPTCHRequest{Secret: recaptchaSecret, Response: challenResponse}
func VerifyNoRemoteIP(challengeResponse string) (bool, error) {
body := reCHAPTCHARequest{Secret: recaptchaSecret, Response: challengeResponse}
return confirm(body)
}

func confirm(recaptach reCHAPTCHRequest) (Ok bool, Err error) {
func confirm(recaptcha reCHAPTCHARequest) (Ok bool, Err error) {
Ok, Err = false, nil
if recaptach.Secret == "" {
if recaptcha.Secret == "" {
Err = fmt.Errorf("recaptcha secret has not been set, please set recaptcha.Init(secret) before calling verification functions")
return
}
// Go http client does not set a default timeout for request, so we need
// to set one for worse cases when the server hang, we need to make this available in the API
// to make it possible this library's users to change it, for now a 10s timeout seems resonable
// to make it possible this library's users to change it, for now a 10s timeout seems reasonable
netClient := &http.Client{
Timeout: 10 * time.Second,
}
jsonValue, _ := json.Marshal(recaptach)

formValue := []byte(`secret=` + recaptcha.Secret + `&response=` + recaptcha.Response)
response, err := netClient.Post(
reCAPTCHALink, "application/json",
bytes.NewBuffer(jsonValue),
reCAPTCHALink,
"application/x-www-form-urlencoded; charset=utf-8",
bytes.NewBuffer(formValue),
)
if err != nil {
Err = fmt.Errorf("error posting to recaptcha endpoint: %s", err)
Expand All @@ -70,7 +72,7 @@ func confirm(recaptach reCHAPTCHRequest) (Ok bool, Err error) {
Err = fmt.Errorf("couldn't read response body: %s", err)
return
}
var result reCHAPTCHResponse
var result reCHAPTCHAResponse
err = json.Unmarshal(resultBody, &result)
if err != nil {
Err = fmt.Errorf("invalid response body json: %s", err)
Expand Down

0 comments on commit b4e5104

Please sign in to comment.