Skip to content

Commit 2a500d7

Browse files
committed
update validate captcha and verify response
1 parent 40ed70a commit 2a500d7

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

arcaptcha.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
const arcaptchaApi = "https://arcaptcha.ir/2/siteverify"
1212

1313
type Website struct {
14-
SiteKey string `json:"site_key"`
15-
SecretKey string `json:"secret_key"`
14+
SiteKey string
15+
SecretKey string
1616
}
1717

1818
func NewWebsite(siteKey, secretKey string) *Website {
@@ -21,7 +21,8 @@ func NewWebsite(siteKey, secretKey string) *Website {
2121
SecretKey: secretKey,
2222
}
2323
}
24-
func (w *Website) ValidateCaptcha(challengeID string) error {
24+
25+
func (w *Website) ValidateCaptcha(challengeID string) (success bool, err error) {
2526

2627
data := &verifyCaptchaRequest{
2728
SiteKey: w.SiteKey,
@@ -30,7 +31,7 @@ func (w *Website) ValidateCaptcha(challengeID string) error {
3031
}
3132
bin, err := json.Marshal(data)
3233
if err != nil {
33-
return err
34+
return
3435
}
3536

3637
req, err := http.NewRequest(
@@ -39,32 +40,30 @@ func (w *Website) ValidateCaptcha(challengeID string) error {
3940
bytes.NewBuffer(bin),
4041
)
4142
if err != nil {
42-
return err
43+
return
4344
}
4445

4546
req.Header.Add("Content-Type", "application/json")
4647
req.Header.Add("Content-Length", strconv.Itoa(len(string(bin))))
4748

4849
res, err := http.DefaultClient.Do(req)
4950
if err != nil {
50-
return err
51+
return
5152
}
5253
defer func() {
5354
_ = res.Body.Close()
5455
}()
5556

5657
bin, err = ioutil.ReadAll(res.Body)
5758
if err != nil {
58-
return err
59-
}
60-
var errRes Error
61-
if err = json.Unmarshal(bin, &errRes); err != nil {
62-
return err
59+
return
6360
}
6461

65-
if res.StatusCode != http.StatusOK {
66-
return errRes
62+
var response VerifyCaptchaResponse
63+
if err = json.Unmarshal(bin, &response); err != nil {
64+
return
6765
}
68-
return nil
6966

67+
success = response.Success
68+
return
7069
}

entity.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
package arcaptcha
22

3-
import "fmt"
4-
53
type verifyCaptchaRequest struct {
64
SiteKey string `json:"site_key"`
75
SecretKey string `json:"secret_key"`
86
ChallengeID string `json:"challenge_id"`
97
}
108

11-
type Error struct {
12-
Status int `json:"status"`
13-
Message string `json:"message"`
14-
}
15-
16-
func (e Error) Error() string {
17-
return fmt.Sprintf("return status %v: %v", e.Status, e.Message)
9+
type VerifyCaptchaResponse struct {
10+
Success bool `json:"success"`
11+
ErrorCodes []string `json:"error-codes"`
1812
}

0 commit comments

Comments
 (0)