diff options
| -rw-r--r-- | LICENSE | 21 | ||||
| -rw-r--r-- | README.md | 40 | ||||
| -rw-r--r-- | recaptcha.go | 15 |
3 files changed, 71 insertions, 5 deletions
@@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Felix Hanley + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..6255501 --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +go-recaptcha +============ + +About +----- + +This package handles reCaptcha (http://www.google.com/recaptcha) form +submissions in Go (http://golang.org/). + +Usage +----- + +Install the package in your environment using `go get`: + +``` +go get github.com/felix/go-recaptcha +``` + +or as part of your source if using [gb](http://getgb.io/). + +To use it within your own code, import "github.com/felix/go-recaptcha" and call: + +``` +recaptcha.Verify(secret, response, remoteip) +``` + +for each reCaptcha form input you need to check, using the values obtained by +reading the form's POST parameters. + +The recaptcha.Verify() function returns a Response struct defined as: + +``` +type Response struct { + Success bool `json:"success"` + // Errors from reCaptcha service + ErrorCodes []string `json:"error-codes"` + // Our errors to caller + Errors []error `json:"-"` +} +``` diff --git a/recaptcha.go b/recaptcha.go index a5b35c6..6d5f974 100644 --- a/recaptcha.go +++ b/recaptcha.go @@ -1,3 +1,4 @@ +// Package recaptcha interacts with the reCaptcha verification API package recaptcha import ( @@ -12,10 +13,10 @@ const ( ) var ( - ErrSecretMissing = errors.New("reCaptcha secret is missing.") - ErrSecretInvalid = errors.New("reCaptcha secret is invalid.") - ErrResponseMissing = errors.New("reCaptcha response is missing.") - ErrResponseInvalid = errors.New("reCaptcha response is invalid.") + ErrSecretMissing = errors.New("The secret parameter is missing.") + ErrSecretInvalid = errors.New("The secret parameter is invalid or malformed.") + ErrResponseMissing = errors.New("The response parameter is missing.") + ErrResponseInvalid = errors.New("The response parameter is invalid or malformed.") ) type Response struct { @@ -49,13 +50,17 @@ func (r *Response) populateErrors() { } } +// Public verfication function +// It requires the reCaptcha secret, the response from the widget. +// It returns the reCaptcha response as a struct. func Verify(secret string, response string, remoteip string) (*Response, error) { values := make(url.Values) values.Set("secret", secret) values.Set("response", response) + values.Set("remoteip", remoteip) - resp, err := http.Get(verifyURL + "?" + values.Encode()) + resp, err := http.PostForm(verifyURL, values) if err != nil { return nil, err |
