/
utils.go
67 lines (57 loc) · 1.42 KB
/
utils.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
64
65
66
67
package metaservice
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/big"
"net/http"
"net/url"
"time"
"github.com/metadium/go-delegator/log"
)
type reCaptchaResponse struct {
Success bool `json:"success"`
ChallengeTS time.Time `json:"challenge_ts"` // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
Hostname string `json:"hostname"` // the hostname of the site where the reCAPTCHA was solved
ErrorCodes []string `json:"error-codes"` //optional
}
func checkRecaptcha(response string) error {
postStr := url.Values{"secret": {reCaptchaSecret}, "response": {response}}
responsePost, err := http.PostForm(reCaptchaURL, postStr)
if err != nil {
log.Error("Post Error", err)
return err
}
defer responsePost.Body.Close()
body, err := ioutil.ReadAll(responsePost.Body)
if err != nil {
log.Error("Read data Error", err)
return err
}
var result reCaptchaResponse
err = json.Unmarshal(body, &result)
if err != nil {
log.Error(" Read JSON Error", err)
return err
}
if !result.Success {
log.Debug(result.ErrorCodes)
return fmt.Errorf("Fail to Verify reCaptcha")
}
return nil
}
func intToByte32(_val *big.Int) [32]byte {
result := [32]byte{}
if _val != nil {
_bytes := _val.Bytes()
copy(result[32-len(_bytes):], _bytes)
}
return result
}
func concatBytes(args ...[]byte) []byte {
var ret []byte
for _, a := range args {
ret = append(ret, a...)
}
return ret
}