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.

hcaptcha.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package hcaptcha
  4. import (
  5. "context"
  6. "io"
  7. "net/http"
  8. "net/url"
  9. "strings"
  10. "code.gitea.io/gitea/modules/json"
  11. "code.gitea.io/gitea/modules/setting"
  12. )
  13. const verifyURL = "https://hcaptcha.com/siteverify"
  14. // Client is an hCaptcha client
  15. type Client struct {
  16. ctx context.Context
  17. http *http.Client
  18. secret string
  19. }
  20. // PostOptions are optional post form values
  21. type PostOptions struct {
  22. RemoteIP string
  23. Sitekey string
  24. }
  25. // ClientOption is a func to modify a new Client
  26. type ClientOption func(*Client)
  27. // WithHTTP sets the http.Client of a Client
  28. func WithHTTP(httpClient *http.Client) func(*Client) {
  29. return func(hClient *Client) {
  30. hClient.http = httpClient
  31. }
  32. }
  33. // WithContext sets the context.Context of a Client
  34. func WithContext(ctx context.Context) func(*Client) {
  35. return func(hClient *Client) {
  36. hClient.ctx = ctx
  37. }
  38. }
  39. // New returns a new hCaptcha Client
  40. func New(secret string, options ...ClientOption) (*Client, error) {
  41. if strings.TrimSpace(secret) == "" {
  42. return nil, ErrMissingInputSecret
  43. }
  44. client := &Client{
  45. ctx: context.Background(),
  46. http: http.DefaultClient,
  47. secret: secret,
  48. }
  49. for _, opt := range options {
  50. opt(client)
  51. }
  52. return client, nil
  53. }
  54. // Response is an hCaptcha response
  55. type Response struct {
  56. Success bool `json:"success"`
  57. ChallengeTS string `json:"challenge_ts"`
  58. Hostname string `json:"hostname"`
  59. Credit bool `json:"credit,omitempty"`
  60. ErrorCodes []ErrorCode `json:"error-codes"`
  61. }
  62. // Verify checks the response against the hCaptcha API
  63. func (c *Client) Verify(token string, opts PostOptions) (*Response, error) {
  64. if strings.TrimSpace(token) == "" {
  65. return nil, ErrMissingInputResponse
  66. }
  67. post := url.Values{
  68. "secret": []string{c.secret},
  69. "response": []string{token},
  70. }
  71. if strings.TrimSpace(opts.RemoteIP) != "" {
  72. post.Add("remoteip", opts.RemoteIP)
  73. }
  74. if strings.TrimSpace(opts.Sitekey) != "" {
  75. post.Add("sitekey", opts.Sitekey)
  76. }
  77. // Basically a copy of http.PostForm, but with a context
  78. req, err := http.NewRequestWithContext(c.ctx, http.MethodPost, verifyURL, strings.NewReader(post.Encode()))
  79. if err != nil {
  80. return nil, err
  81. }
  82. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  83. resp, err := c.http.Do(req)
  84. if err != nil {
  85. return nil, err
  86. }
  87. body, err := io.ReadAll(resp.Body)
  88. if err != nil {
  89. return nil, err
  90. }
  91. defer resp.Body.Close()
  92. var response *Response
  93. if err := json.Unmarshal(body, &response); err != nil {
  94. return nil, err
  95. }
  96. return response, nil
  97. }
  98. // Verify calls hCaptcha API to verify token
  99. func Verify(ctx context.Context, response string) (bool, error) {
  100. client, err := New(setting.Service.HcaptchaSecret, WithContext(ctx))
  101. if err != nil {
  102. return false, err
  103. }
  104. resp, err := client.Verify(response, PostOptions{
  105. Sitekey: setting.Service.HcaptchaSitekey,
  106. })
  107. if err != nil {
  108. return false, err
  109. }
  110. var respErr error
  111. if len(resp.ErrorCodes) > 0 {
  112. respErr = resp.ErrorCodes[0]
  113. }
  114. return resp.Success, respErr
  115. }