add go-recaptcha dependency

This commit is contained in:
Chris Camel 2020-06-02 15:29:10 +02:00
parent 2a5757874e
commit 7511603de4
No known key found for this signature in database
GPG Key ID: 125EFEF60AEF6949
7 changed files with 156 additions and 0 deletions

1
go.mod
View File

@ -3,6 +3,7 @@ module hugo-mx-gateway
go 1.13
require (
github.com/dpapathanasiou/go-recaptcha v0.0.0-20190121160230-be5090b17804
github.com/gorilla/mux v1.7.4
github.com/sirupsen/logrus v1.2.0
github.com/spf13/viper v1.7.0

2
go.sum
View File

@ -36,6 +36,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/dpapathanasiou/go-recaptcha v0.0.0-20190121160230-be5090b17804 h1:gFnPvL9HX+Nrb4M2AwzFYqcwGStxYZpuDpFAqpViBG4=
github.com/dpapathanasiou/go-recaptcha v0.0.0-20190121160230-be5090b17804/go.mod h1:eovtlS/D2AGk8vy2a9sO4XzOyHMHb8jM+WPsf9pkgFo=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=

View File

@ -0,0 +1,4 @@
*~
*.a
*.swp
example/example

20
vendor/github.com/dpapathanasiou/go-recaptcha/LICENSE generated vendored Normal file
View File

@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright (c) 2012-2016 Denis Papathanasiou
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.

View File

@ -0,0 +1,56 @@
go-recaptcha
============
https://godoc.org/github.com/dpapathanasiou/go-recaptcha
About
-----
This package handles [reCaptcha](https://www.google.com/recaptcha) (API versions [2](https://developers.google.com/recaptcha/intro) and [3](https://developers.google.com/recaptcha/docs/v3)) form submissions in [Go](http://golang.org/).
Usage
-----
Install the package in your environment:
```
go get github.com/dpapathanasiou/go-recaptcha
```
To use it within your own code, import <tt>github.com/dpapathanasiou/go-recaptcha</tt> and call:
```
recaptcha.Init (recaptchaPrivateKey)
```
once, to set the reCaptcha private key for your domain, then:
```
recaptcha.Confirm (clientIpAddress, recaptchaResponse)
```
### [reCAPTCHA v2](https://developers.google.com/recaptcha/intro)
For each reCaptcha form input you need to check, using the values obtained by reading the form's POST parameters (the <tt>recaptchaResponse</tt> in the above corresponds to the value of <tt>g-recaptcha-response</tt> sent by the reCaptcha server.)
The recaptcha.Confirm() function returns either true (i.e., the captcha was completed correctly) or false, along with any errors (from the HTTP io read or the attempt to unmarshal the JSON reply).
### [reCAPTCHA v3](https://developers.google.com/recaptcha/docs/v3)
Version 3 works differently: instead of interrupting page visitors with a prompt, it runs in the background, computing a score.
This repo has been updated to handle the [score and action in the response](recaptcha.go#L20), but the usage example is still in terms of version 2.
Usage Example
-------------
Included with this repo is [example.go](example/example.go), a simple HTTP server which creates the reCaptcha form and tests the input.
See the [instructions](example/README.md) for running the example for more details.
## Donate
If you find this work useful, please consider making a donation:
<a href="bitcoin:14TM4ADKJbaGEi8Qr8dh4KfPBQmjTshkZ2">Bitcoin Donate</a> `14TM4ADKJbaGEi8Qr8dh4KfPBQmjTshkZ2`
![QR code](https://bitref.com/qr.php?data=14TM4ADKJbaGEi8Qr8dh4KfPBQmjTshkZ2)

View File

@ -0,0 +1,71 @@
// Package recaptcha handles reCaptcha (http://www.google.com/recaptcha) form submissions
//
// This package is designed to be called from within an HTTP server or web framework
// which offers reCaptcha form inputs and requires them to be evaluated for correctness
//
// Edit the recaptchaPrivateKey constant before building and using
package recaptcha
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"net/url"
"time"
)
type RecaptchaResponse struct {
Success bool `json:"success"`
Score float64 `json:"score"`
Action string `json:"action"`
ChallengeTS time.Time `json:"challenge_ts"`
Hostname string `json:"hostname"`
ErrorCodes []string `json:"error-codes"`
}
const recaptchaServerName = "https://www.google.com/recaptcha/api/siteverify"
var recaptchaPrivateKey string
// check uses the client ip address, the challenge code from the reCaptcha form,
// and the client's response input to that challenge to determine whether or not
// the client answered the reCaptcha input question correctly.
// It returns a boolean value indicating whether or not the client answered correctly.
func check(remoteip, response string) (r RecaptchaResponse, err error) {
resp, err := http.PostForm(recaptchaServerName,
url.Values{"secret": {recaptchaPrivateKey}, "remoteip": {remoteip}, "response": {response}})
if err != nil {
log.Printf("Post error: %s\n", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("Read error: could not read body: %s", err)
return
}
err = json.Unmarshal(body, &r)
if err != nil {
log.Println("Read error: got invalid JSON: %s", err)
return
}
return
}
// Confirm is the public interface function.
// It calls check, which the client ip address, the challenge code from the reCaptcha form,
// and the client's response input to that challenge to determine whether or not
// the client answered the reCaptcha input question correctly.
// It returns a boolean value indicating whether or not the client answered correctly.
func Confirm(remoteip, response string) (result bool, err error) {
resp, err := check(remoteip, response)
result = resp.Success
return
}
// Init allows the webserver or code evaluating the reCaptcha form input to set the
// reCaptcha private key (string) value, which will be different for every domain.
func Init(key string) {
recaptchaPrivateKey = key
}

2
vendor/modules.txt vendored
View File

@ -1,3 +1,5 @@
# github.com/dpapathanasiou/go-recaptcha v0.0.0-20190121160230-be5090b17804
github.com/dpapathanasiou/go-recaptcha
# github.com/fsnotify/fsnotify v1.4.7
github.com/fsnotify/fsnotify
# github.com/gorilla/mux v1.7.4