Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chanioxaris/go-recaptcha
A package to handle verification for Google reCAPTCHA (v2 and v3)
https://github.com/chanioxaris/go-recaptcha
golang golang-library golang-package google-recaptcha google-recaptcha-v2 google-recaptcha-v3 recaptcha recaptcha-api recaptcha-v2 recaptcha-v3 recaptcha-verification
Last synced: 11 days ago
JSON representation
A package to handle verification for Google reCAPTCHA (v2 and v3)
- Host: GitHub
- URL: https://github.com/chanioxaris/go-recaptcha
- Owner: chanioxaris
- License: mit
- Created: 2020-06-22T20:50:45.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-09-28T18:09:15.000Z (about 4 years ago)
- Last Synced: 2024-06-20T05:23:27.310Z (5 months ago)
- Topics: golang, golang-library, golang-package, google-recaptcha, google-recaptcha-v2, google-recaptcha-v3, recaptcha, recaptcha-api, recaptcha-v2, recaptcha-v3, recaptcha-verification
- Language: Go
- Homepage:
- Size: 22.5 KB
- Stars: 11
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: CODEOWNERS
Awesome Lists containing this project
README
# go-recaptcha
[Google reCAPTCHA](https://www.google.com/recaptcha/intro/v3.html) (v2 and v3) verification in Golang.
## Install
To get the package:
`go get github.com/chanioxaris/go-recaptcha`
## Examples
reCAPTCHA v2:
- Simple usage with default values (httpClient: http.DefaultClient)
package main
import (
"fmt"
"github.com/chanioxaris/go-recaptcha"
)
func main() {
rec, err := recaptcha.New(, recaptcha.WithVersion(2))
if err != nil {
panic(err)
}
if err = rec.Verify(); err != nil {
panic(err)
}
fmt.Println("Success")
}
- Simple usage with custom http clientpackage main
import (
"fmt"
"github.com/chanioxaris/go-recaptcha"
)
func main() {
customClient := &http.Client{Timeout: time.Second * 10}
rec, err := recaptcha.New(
,
recaptcha.WithVersion(2),
recaptcha.WithHTTPClient(customClient)
)
if err != nil {
panic(err)
}
if err = rec.Verify(); err != nil {
panic(err)
}
fmt.Println("Success")
}
- Get reCAPTCHA token from request body (`g-recaptcha-response` field)import (
"fmt"
"net/http"
"github.com/chanioxaris/go-recaptcha"
)
func Handler(w http.ResponseWriter, r *http.Request) {
rec, err := recaptcha.New(, recaptcha.WithVersion(2))
if err != nil {
panic(err)
}
response, err := rec.GetRequestToken(r)
if err != nil {
panic(err)
}
if err = rec.Verify(response); err != nil {
panic(err)
}
fmt.Println("Success")
}reCAPTCHA v3:
- Simple usage with default values (version: 3, action: "", score: 0.5, httpClient: http.DefaultClient)
package main
import (
"fmt"
"github.com/chanioxaris/go-recaptcha"
)
func main() {
rec, err := recaptcha.New()
if err != nil {
panic(err)
}
if err = rec.Verify(); err != nil {
panic(err)
}
fmt.Println("Success")
}
- Simple usage with custom valuespackage main
import (
"fmt"
"github.com/chanioxaris/go-recaptcha"
)
func main() {
customClient := &http.Client{Timeout: time.Second * 10}
customAction := "custom-action"
customScore := 0.7
rec, err := recaptcha.New(
,
recaptcha.WithHTTPClient(customClient),
recaptcha.WithAction(customAction),
recaptcha.WithScore(customScore),
)
if err != nil {
panic(err)
}
if err = rec.Verify(); err != nil {
panic(err)
}
fmt.Println("Success")
}- Get reCAPTCHA token from request body (`g-recaptcha-response` field)
package main
import (
"fmt"
"net/http"
"github.com/chanioxaris/go-recaptcha"
)
func Handler(w http.ResponseWriter, r *http.Request) {
rec, err := recaptcha.New()
if err != nil {
panic(err)
}
response, err := rec.GetRequestToken(r)
if err != nil {
panic(err)
}
if err = rec.Verify(response); err != nil {
panic(err)
}
fmt.Println("Success")
}Middleware:
- Use middleware in REST API
package main
import (
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/chanioxaris/go-recaptcha"
)
func main() {
// Create a new recaptcha instance.
rec, err := recaptcha.New()
if err != nil {
panic(err)
}
// Setup router.
router := mux.NewRouter().StrictSlash(true)
// Use the recaptcha middleware.
router.Use(recaptcha.Middleware(rec))
// Setup endpoint handler.
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("A Google reCAPTCHA protected endpoint"))
})
// Start server.
log.Fatal(http.ListenAndServe(":8080", router))
}## License
go-recaptcha is [MIT licensed](LICENSE)