GithubHelp home page GithubHelp logo

lithdew / recaptcha Goto Github PK

View Code? Open in Web Editor NEW
4.0 3.0 1.0 8 KB

Quickly verify reCAPTCHA v2/v3 submissions in Go.

License: MIT License

Go 100.00%
golang google recaptcha fasthttp fastjson client

recaptcha's Introduction

recaptcha

MIT License go.dev reference Discord Chat

recaptcha is a package that handles verifying reCAPTCHA v2/v3 submissions in Go.

  • Validating a verification request, sending a verification request, and parsing a verification response are separated into individual functions.
  • Validates and double-checks all outgoing requests and incoming responses for errors.
  • Interoperable and made easy to work with both reCAPTCHA v2 and v3.
  • Uses valyala/fasthttp for sending reCAPTCHA request with an optional timeout.
  • Uses valyala/fastjson for parsing responses from the reCAPTCHA API.

Inspiration

Someone told me they were looking through reCAPTCHA packages online in Go and couldn't find a simple, idiomatic one.

This one's a bit overly optimized and uses two popular 3rd party libraries over the standard library, but here you go ยฏ_(ใƒ„)_/.

Usage

go get github.com/lithdew/recaptcha
package main

import (
    "github.com/lithdew/recaptcha"
    "time"
)

func main() {
    req := recaptcha.Request{
        Secret: "", // Your reCAPTCHA secret.
        Response: "", // The reCAPTCHA response sent by the reCAPTCHA API.
        RemoteIP : "", // (optional) The remote IP of the user submitting the reCAPTCHA response.
    }

    // Verify the reCAPTCHA request.
    
    res, err := recaptcha.Do(req) 
    if err != nil {
    	panic(err)
    }

    if res.Success {
        println("reCAPTCHA attempt successfully verified!")
    } else {
        println("reCAPTCHA attempt failed!")
    }

    // Verify the reCAPTCHA request, and timeout after 3 seconds.

    res, err = recaptcha.DoTimeout(req, 3 * time.Second) 
    if err != nil {
    	panic(err)
    }

    if res.Success {
        println("reCAPTCHA attempt successfully verified!")
    } else {
        println("reCAPTCHA attempt failed!")
    }
}

Benchmarks

Take these with a grain of salt; network latency should sum up the majority of the benchmark results.

go test -bench=. -benchmem -benchtime=10s

goos: linux
goarch: amd64
pkg: github.com/lithdew/recaptcha
BenchmarkDo-8                        187          55273288 ns/op            1513 B/op         17 allocs/op
BenchmarkDoTimeout-8                 205          55503923 ns/op            1482 B/op         19 allocs/op
BenchmarkParallelDo-8               1500           7060534 ns/op            1386 B/op         17 allocs/op
BenchmarkParallelDoTimeout-8        1740           6752978 ns/op            1405 B/op         18 allocs/op

recaptcha's People

Contributors

lithdew avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

forkkit

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.