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.

turnstile.go 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package turnstile
  4. import (
  5. "context"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "net/url"
  10. "strings"
  11. "code.gitea.io/gitea/modules/json"
  12. "code.gitea.io/gitea/modules/setting"
  13. )
  14. // Response is the structure of JSON returned from API
  15. type Response struct {
  16. Success bool `json:"success"`
  17. ChallengeTS string `json:"challenge_ts"`
  18. Hostname string `json:"hostname"`
  19. ErrorCodes []ErrorCode `json:"error-codes"`
  20. Action string `json:"login"`
  21. Cdata string `json:"cdata"`
  22. }
  23. // Verify calls Cloudflare Turnstile API to verify token
  24. func Verify(ctx context.Context, response string) (bool, error) {
  25. // Cloudflare turnstile official access instruction address: https://developers.cloudflare.com/turnstile/get-started/server-side-validation/
  26. post := url.Values{
  27. "secret": {setting.Service.CfTurnstileSecret},
  28. "response": {response},
  29. }
  30. // Basically a copy of http.PostForm, but with a context
  31. req, err := http.NewRequestWithContext(ctx, http.MethodPost,
  32. "https://challenges.cloudflare.com/turnstile/v0/siteverify", strings.NewReader(post.Encode()))
  33. if err != nil {
  34. return false, fmt.Errorf("Failed to create CAPTCHA request: %w", 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: %w", 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: %w", err)
  45. }
  46. var jsonResponse Response
  47. if err := json.Unmarshal(body, &jsonResponse); err != nil {
  48. return false, fmt.Errorf("Failed to parse CAPTCHA response: %w", err)
  49. }
  50. var respErr error
  51. if len(jsonResponse.ErrorCodes) > 0 {
  52. respErr = jsonResponse.ErrorCodes[0]
  53. }
  54. return jsonResponse.Success, respErr
  55. }
  56. // ErrorCode is a reCaptcha error
  57. type ErrorCode string
  58. // String fulfills the Stringer interface
  59. func (e ErrorCode) String() string {
  60. switch e {
  61. case "missing-input-secret":
  62. return "The secret parameter was not passed."
  63. case "invalid-input-secret":
  64. return "The secret parameter was invalid or did not exist."
  65. case "missing-input-response":
  66. return "The response parameter was not passed."
  67. case "invalid-input-response":
  68. return "The response parameter is invalid or has expired."
  69. case "bad-request":
  70. return "The request was rejected because it was malformed."
  71. case "timeout-or-duplicate":
  72. return "The response parameter has already been validated before."
  73. case "internal-error":
  74. return "An internal error happened while validating the response. The request can be retried."
  75. }
  76. return string(e)
  77. }
  78. // Error fulfills the error interface
  79. func (e ErrorCode) Error() string {
  80. return e.String()
  81. }