New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

xxx.engineer/gaptcha

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xxx.engineer/gaptcha

  • v0.1.1
  • Go
  • Socket score

Version published
Created
Source

Gaptcha

Google reCAPTCHA (v2 and v3) verification in Golang.

Install

To get the package:

go get -u xxx.engineer.com/gaptcha

Examples

reCAPTCHA v2:

  • Simple usage with default values (httpClient: http.DefaultClient)

      package main
      
      import (
          "fmt"
      
          "xxx.engineer.com/gaptcha"
      )
      
      func main() {
          rec, err := gaptcha.New(<secret>, gaptcha.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"
      
          "xxx.engineer.com/gaptcha"
      )
      
      func main() {
          customClient := &http.Client{Timeout: time.Second * 10}
          
          rec, err := gaptcha.New(
                  <secret>, 
                  gaptcha.WithVersion(2), 
                  gaptcha.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"
      
      	"xxx.engineer.com/gaptcha"
      )
      
      func Handler(w http.ResponseWriter, r *http.Request) {
      	rec, err := gaptcha.New(<secret>, gaptcha.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"
    
          "xxx.engineer.com/gaptcha"
      )
    
      func main() {
          rec, err := gaptcha.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"
    
          "xxx.engineer.com/gaptcha"
      )
      
      func main() {
           customClient := &http.Client{Timeout: time.Second * 10}
           customAction := "custom-action"
           customScore := 0.7
      
          rec, err := gaptcha.New(
                  <secret>, 
          		gaptcha.WithHTTPClient(customClient), 
          		gaptcha.WithAction(customAction), 
          		gaptcha.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"
      
          "xxx.engineer.com/gaptcha"
      )
      
      func Handler(w http.ResponseWriter, r *http.Request) {
          rec, err := gaptcha.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"
          "xxx.engineer.com/gaptcha"
      )
      
      func main() {
          // Create a new gaptcha instance.
          rec, err := gaptcha.New(<secret>)
          if err != nil {
              panic(err)
          }
      
          // Setup router.
          router := mux.NewRouter().StrictSlash(true)
          // Use the gaptcha middleware.
          router.Use(gaptcha.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))
      }
    

FAQs

Package last updated on 22 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc