You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

recaptcha.go 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package recaptcha
  5. import (
  6. "context"
  7. "fmt"
  8. "io"
  9. "net/http"
  10. "net/url"
  11. "strings"
  12. "code.gitea.io/gitea/modules/json"
  13. "code.gitea.io/gitea/modules/setting"
  14. "code.gitea.io/gitea/modules/util"
  15. )
  16. // Response is the structure of JSON returned from API
  17. type Response struct {
  18. Success bool `json:"success"`
  19. ChallengeTS string `json:"challenge_ts"`
  20. Hostname string `json:"hostname"`
  21. ErrorCodes []ErrorCode `json:"error-codes"`
  22. }
  23. const apiURL = "api/siteverify"
  24. // Verify calls Google Recaptcha API to verify token
  25. func Verify(ctx context.Context, response string) (bool, error) {
  26. post := url.Values{
  27. "secret": {setting.Service.RecaptchaSecret},
  28. "response": {response},
  29. }
  30. // Basically a copy of http.PostForm, but with a context
  31. req, err := http.NewRequestWithContext(ctx, http.MethodPost,
  32. util.URLJoin(setting.Service.RecaptchaURL, apiURL), strings.NewReader(post.Encode()))
  33. if err != nil {
  34. return false, fmt.Errorf("Failed to create CAPTCHA request: %v", err)
  35. }
  36. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  37. resp, err := http.DefaultClient.Do(req)
  38. if err != nil {
  39. return false, fmt.Errorf("Failed to send CAPTCHA response: %s", err)
  40. }
  41. defer resp.Body.Close()
  42. body, err := io.ReadAll(resp.Body)
  43. if err != nil {
  44. return false, fmt.Errorf("Failed to read CAPTCHA response: %s", err)
  45. }
  46. var jsonResponse Response
  47. err = json.Unmarshal(body, &jsonResponse)
  48. if err != nil {
  49. return false, fmt.Errorf("Failed to parse CAPTCHA response: %s", err)
  50. }
  51. var respErr error
  52. if len(jsonResponse.ErrorCodes) > 0 {
  53. respErr = jsonResponse.ErrorCodes[0]
  54. }
  55. return jsonResponse.Success, respErr
  56. }
  57. // ErrorCode is a reCaptcha error
  58. type ErrorCode string
  59. // String fulfills the Stringer interface
  60. func (e ErrorCode) String() string {
  61. switch e {
  62. case "missing-input-secret":
  63. return "The secret parameter is missing."
  64. case "invalid-input-secret":
  65. return "The secret parameter is invalid or malformed."
  66. case "missing-input-response":
  67. return "The response parameter is missing."
  68. case "invalid-input-response":
  69. return "The response parameter is invalid or malformed."
  70. case "bad-request":
  71. return "The request is invalid or malformed."
  72. case "timeout-or-duplicate":
  73. return "The response is no longer valid: either is too old or has been used previously."
  74. }
  75. return string(e)
  76. }
  77. // Error fulfills the error interface
  78. func (e ErrorCode) Error() string {
  79. return e.String()
  80. }