-
Notifications
You must be signed in to change notification settings - Fork 0
/
hcaptcha.go
63 lines (54 loc) · 1.68 KB
/
hcaptcha.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package foxkit
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
)
type HCaptchaResponse struct {
Success bool `json:"success"`
ChallengeTS time.Time `json:"challenge_ts"`
Hostname string `json:"hostname"`
Credit bool `json:"credit"`
ErrorCode []string `json:"error-codes"`
}
// retrieves the HCaptcha response
func (hresp *HCaptchaResponse) Get(secret, response *string) error {
// get response from hcaptcha
resp, err := http.Post("https://hcaptcha.com/siteverify", "application/x-www-form-urlencoded; charset=utf-8", strings.NewReader(fmt.Sprintf("secret=%s&response=%s", *secret, *response)))
if err != nil {
return err
}
defer resp.Body.Close()
// parse response body
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
// put response into struct
err = json.Unmarshal([]byte(bodyBytes), &hresp)
if err != nil {
return err
}
// everything worked
return nil
}
// checks if the hcaptcha response is valid, returns error if the response is invalid
func (captcha *HCaptchaResponse) Valid(hostname string, validFor time.Duration) (bool, error) {
// check response
if !captcha.Success {
if captcha.ErrorCode != nil {
return false, fmt.Errorf("captcha is invalid, error code: %v", captcha.ErrorCode)
} else {
return false, fmt.Errorf("captcha is invalid without error code")
}
} else if captcha.Hostname != hostname {
return false, fmt.Errorf("wrong hostname for captcha challenge, got %s instead of %s", captcha.Hostname, hostname)
} else if !captcha.ChallengeTS.After(time.Now().Add(-validFor)) {
return false, fmt.Errorf("challenge to old therefore invalid")
}
// challenge valid
return true, nil
}