Skip to content

Commit

Permalink
v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Xinguang committed Mar 21, 2020
1 parent bb3d882 commit 0674806
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@

# Dependency directories (remove the comment below to include it)
# vendor/
out.html
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ This package handles [reCaptcha](https://www.google.com/recaptcha) (API versions
### Usage <a name="Usage" />

* Install the package in your environment:

``` sh
go get github.com/xinguang/go-recaptcha
```
To use it within your own code, import github.com/xinguang/go-recaptcha and call:

* To use it within your own code, import github.com/xinguang/go-recaptcha and call:

```go
package main
Expand All @@ -43,15 +45,16 @@ func main() {
// get your secret from https://www.google.com/recaptcha/admin
// Using environment variables
// export ReCAPTCHA_SECRET="reCaptcha Secret Key"
recaptcha, err := NewReCAPTCHA()
recaptcha, err := New()

// OR
const Secret = "reCaptcha Secret Key"
recaptcha, err := NewReCAPTCHAWithSecert(Secret)
recaptcha, err := NewWithSecert(Secret)
// .....
}
```
Now everytime you need to verify a API client with no special options request use

* Now everytime you need to verify a API client with no special options request use

```go
// the recaptchaResponse corresponds to
Expand All @@ -76,8 +79,10 @@ Available options:
ResponseTime float64
RemoteIP string
```

Note that as reCaptcha v3 use score for challenge validation, if no threshold option is set the default value is 0.5


```go
err := recaptcha.VerifyWithOptions(recaptchaResponse, VerifyOption{RemoteIP: "127.0.0.1"})
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions recaptcha.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ type ReCAPTCHA struct {
Secret string
}

// NewReCAPTCHA new ReCAPTCHA instance
func NewReCAPTCHA() (*ReCAPTCHA, error) {
return NewReCAPTCHAWithSecert(os.Getenv("ReCAPTCHA_SECRET"))
// New new ReCAPTCHA instance
func New() (*ReCAPTCHA, error) {
return NewWithSecert(os.Getenv("ReCAPTCHA_SECRET"))
}

// NewReCAPTCHAWithSecert new ReCAPTCHA instance
// NewWithSecert new ReCAPTCHA instance
// get your secret from https://www.google.com/recaptcha/admin
func NewReCAPTCHAWithSecert(secret string) (*ReCAPTCHA, error) {
func NewWithSecert(secret string) (*ReCAPTCHA, error) {
if len(secret) == 0 {
return nil, fmt.Errorf("recaptcha secret cannot be blank")
}
Expand Down
8 changes: 4 additions & 4 deletions recaptcha_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ type ReCaptchaSuite struct{}

var _ = Suite(&ReCaptchaSuite{})

func (s *ReCaptchaSuite) TestNewReCAPTCHA(c *C) {
captcha, err := NewReCAPTCHAWithSecert("my secret")
func (s *ReCaptchaSuite) TestNew(c *C) {
captcha, err := NewWithSecert("my secret")
c.Assert(err, IsNil)
c.Check(captcha.Secret, Equals, "my secret")

captcha, err = NewReCAPTCHAWithSecert("")
captcha, err = NewWithSecert("")
c.Assert(err, NotNil)

captcha, err = NewReCAPTCHA()
captcha, err = New()
c.Assert(err, NotNil)
}

Expand Down

0 comments on commit 0674806

Please sign in to comment.