|
1 | | -<p align="center"><img src="https://arcaptcha.ir/logo.png" height="150px"></p> |
| 1 | +<p align="center"><img src="https://arcaptcha.ir/_nuxt/023053fecdcdf20e40bdc993c754d487.svg" height="100px"></p> |
2 | 2 |
|
3 | 3 | [](https://pkg.go.dev/github.com/arcaptcha/arcaptcha-go) |
4 | 4 | [](https://raw.githubusercontent.com/arcaptcha/arcaptcha-go/master/LICENSE) |
|
7 | 7 |
|
8 | 8 | Arcaptcha |
9 | 9 | ===================== |
10 | | -Arcaptcha library implementation in Golang to validate captcha. |
| 10 | +Arcaptcha library implementation in Golang to verify captcha. |
11 | 11 |
|
12 | 12 | ## [Guide](https://arcaptcha.ir/guide) |
| 13 | + |
13 | 14 | ### Installation |
14 | 15 |
|
15 | 16 | ``` |
16 | 17 | go get -u github.com/arcaptcha/arcaptcha-go |
17 | 18 | ``` |
18 | 19 |
|
19 | 20 | ### Usage |
| 21 | + |
20 | 22 | Register on [Arcaptcha](https://arcaptcha.ir/), create website and get your own SiteKey and SecretKey |
| 23 | + |
| 24 | +```go |
| 25 | +website := arcaptcha.NewWebsite("YOUR_SITE_KEY", "YOUR_SECRET_KEY") |
| 26 | +//'arcaptcha-token' is created for each captcha |
| 27 | +//After you put captcha widget in your website, you can get 'arcaptcha-token' from form |
| 28 | +result, err := website.Verify("arcaptcha-token") |
| 29 | +if err != nil { |
| 30 | + // error in sending or receiving API request |
| 31 | + // handle error |
| 32 | +} |
| 33 | +if !result.Success { |
| 34 | + // captcha not verified |
| 35 | + // can see result.ErrorCodes to find what's wrong |
| 36 | + // throw specific error |
| 37 | +} |
| 38 | +// it's ok |
| 39 | +``` |
| 40 | + |
| 41 | +#### Use in middleware: |
| 42 | + |
21 | 43 | ```go |
22 | 44 | package main |
23 | 45 |
|
24 | 46 | import ( |
25 | | - "log" |
| 47 | + "net/http" |
26 | 48 |
|
27 | 49 | "github.com/arcaptcha/arcaptcha-go" |
28 | 50 | ) |
29 | 51 |
|
| 52 | +var website *arcaptcha.Website |
| 53 | + |
30 | 54 | func main() { |
31 | | - website := arcaptcha.NewWebsite("YOUR_SITE_KEY", "YOUR_SECRET_KEY") |
32 | | - //ChallengeID is created for each captcha |
33 | | - //After you put captcha widget in your website, you can get challengeID |
34 | | - if err := website.ValidateCaptcha("challengeID"); err != nil { |
35 | | - log.Fatal(err) |
36 | | - } |
37 | | - //Its OK |
38 | | - log.Printf("Captcha %v is valid and challenge succeeded", "challengeID") |
| 55 | + website = arcaptcha.NewWebsite("YOUR_SITE_KEY", "YOUR_SECRET_KEY") |
| 56 | + |
| 57 | + myHandler := http.HandlerFunc(handler) |
| 58 | + http.Handle("/", verifyCaptcha(myHandler)) |
| 59 | + http.ListenAndServe(":8000", nil) |
| 60 | +} |
| 61 | + |
| 62 | +func handler(w http.ResponseWriter, r *http.Request) { |
| 63 | + // handle request |
| 64 | +} |
| 65 | + |
| 66 | +func verifyCaptcha(next http.Handler) http.Handler { |
| 67 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 68 | + token := r.FormValue("arcaptcha-token") |
| 69 | + result, err := website.Verify(token) |
| 70 | + if err != nil { |
| 71 | + // error in sending or receiving API request |
| 72 | + // handle error |
| 73 | + } |
| 74 | + if !result.Success { |
| 75 | + // captcha not verified |
| 76 | + // can see result.ErrorCodes to find what's wrong |
| 77 | + // throw specific error |
| 78 | + return |
| 79 | + } |
| 80 | + // it's ok |
| 81 | + next.ServeHTTP(w, r) |
| 82 | + }) |
39 | 83 | } |
40 | 84 | ``` |
0 commit comments