diff options
author | Fluf <36822577+flufmonster@users.noreply.github.com> | 2018-07-05 00:13:05 -0400 |
---|---|---|
committer | techknowlogick <techknowlogick@users.noreply.github.com> | 2018-07-05 00:13:05 -0400 |
commit | f035dcd4f221a631cc499d90661237d6cf601843 (patch) | |
tree | 18f7cfd148dd36c85e4c82c8cd5fdef88c249606 /modules/recaptcha | |
parent | 54fedd4070be9819a6dd4e441b3c5689334eae9d (diff) | |
download | gitea-f035dcd4f221a631cc499d90661237d6cf601843.tar.gz gitea-f035dcd4f221a631cc499d90661237d6cf601843.zip |
Add Recaptcha functionality to Gitea (#4044)
Diffstat (limited to 'modules/recaptcha')
-rw-r--r-- | modules/recaptcha/recaptcha.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/modules/recaptcha/recaptcha.go b/modules/recaptcha/recaptcha.go new file mode 100644 index 0000000000..1009185961 --- /dev/null +++ b/modules/recaptcha/recaptcha.go @@ -0,0 +1,47 @@ +// Copyright 2018 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package recaptcha + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "net/url" + "time" + + "code.gitea.io/gitea/modules/setting" +) + +// Response is the structure of JSON returned from API +type Response struct { + Success bool `json:"success"` + ChallengeTS time.Time `json:"challenge_ts"` + Hostname string `json:"hostname"` + ErrorCodes []string `json:"error-codes"` +} + +const apiURL = "https://www.google.com/recaptcha/api/siteverify" + +// Verify calls Google Recaptcha API to verify token +func Verify(response string) (bool, error) { + resp, err := http.PostForm(apiURL, + url.Values{"secret": {setting.Service.RecaptchaSecret}, "response": {response}}) + if err != nil { + return false, fmt.Errorf("Failed to send CAPTCHA response: %s", err) + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return false, fmt.Errorf("Failed to read CAPTCHA response: %s", err) + } + var jsonResponse Response + err = json.Unmarshal(body, &jsonResponse) + if err != nil { + return false, fmt.Errorf("Failed to parse CAPTCHA response: %s", err) + } + + return jsonResponse.Success, nil +} |