diff --git a/.gitignore b/.gitignore index 1a5ca26..ffbdf30 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ # Dependency directories (remove the comment below to include it) # vendor/ +out.html diff --git a/README.md b/README.md index e1b8973..8fb4cdc 100644 --- a/README.md +++ b/README.md @@ -28,10 +28,12 @@ This package handles [reCaptcha](https://www.google.com/recaptcha) (API versions ### 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 @@ -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 @@ -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 { diff --git a/recaptcha.go b/recaptcha.go index 7dd3051..58ca6d4 100644 --- a/recaptcha.go +++ b/recaptcha.go @@ -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") } diff --git a/recaptcha_test.go b/recaptcha_test.go index f58f77a..b9abcf2 100644 --- a/recaptcha_test.go +++ b/recaptcha_test.go @@ -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) }