Giter Club home page Giter Club logo

go-recaptcha's Introduction

go-recaptcha

Google reCAPTCHA (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(<secret>, recaptcha.WithVersion(2))
          if err != nil {
              panic(err)
          }
      
          if err = rec.Verify(<response>); err != nil {
              panic(err)
          }
      
          fmt.Println("Success")
      }
    
  • Simple usage with custom http client

      package main
      
      import (
          "fmt"
      
          "github.com/chanioxaris/go-recaptcha"
      )
      
      func main() {
          customClient := &http.Client{Timeout: time.Second * 10}
          
          rec, err := recaptcha.New(
                  <secret>, 
                  recaptcha.WithVersion(2), 
                  recaptcha.WithHTTPClient(customClient)
              )
          if err != nil {
              panic(err)
          }
      
          if err = rec.Verify(<response>); 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(<secret>, 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(<secret>)
          if err != nil {
              panic(err)
          }
      
          if err = rec.Verify(<response>); err != nil {
              panic(err)
          }
      
          fmt.Println("Success")
      }
    
  • Simple usage with custom values

      package 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(
                  <secret>, 
          		recaptcha.WithHTTPClient(customClient), 
          		recaptcha.WithAction(customAction), 
          		recaptcha.WithScore(customScore),
          	)
          if err != nil {
              panic(err)
          }
      
          if err = rec.Verify(<response>); 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(<secret>)
          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(<secret>)
          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

go-recaptcha's People

Contributors

chanioxaris avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

mralexzee

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.