summaryrefslogtreecommitdiffstats
path: root/vendor
diff options
context:
space:
mode:
authorJohn Olheiser <john.olheiser@gmail.com>2020-10-02 22:37:53 -0500
committerGitHub <noreply@github.com>2020-10-02 23:37:53 -0400
commit72636fd6642fcae6e7447dd499cb097c5c65ab32 (patch)
tree2138dde0d4e1fc4d0d90943b34d50f40942fada3 /vendor
parent5460bf89031a77ac9e0cde685c1bff00c29ee883 (diff)
downloadgitea-72636fd6642fcae6e7447dd499cb097c5c65ab32.tar.gz
gitea-72636fd6642fcae6e7447dd499cb097c5c65ab32.zip
hCaptcha Support (#12594)
* Initial work on hCaptcha Signed-off-by: jolheiser <john.olheiser@gmail.com> * Use module Signed-off-by: jolheiser <john.olheiser@gmail.com> * Format Signed-off-by: jolheiser <john.olheiser@gmail.com> * At least return and debug log a captcha error Signed-off-by: jolheiser <john.olheiser@gmail.com> * Pass context to hCaptcha Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add context to recaptcha Signed-off-by: jolheiser <john.olheiser@gmail.com> * fix lint Signed-off-by: Andrew Thornton <art27@cantab.net> * Finish hcaptcha Signed-off-by: jolheiser <john.olheiser@gmail.com> * Update example config Signed-off-by: jolheiser <john.olheiser@gmail.com> * Apply error fix for recaptcha Signed-off-by: jolheiser <john.olheiser@gmail.com> * Change recaptcha ChallengeTS to string Signed-off-by: jolheiser <john.olheiser@gmail.com> Co-authored-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'vendor')
-rw-r--r--vendor/go.jolheiser.com/hcaptcha/.gitignore2
-rw-r--r--vendor/go.jolheiser.com/hcaptcha/LICENSE7
-rw-r--r--vendor/go.jolheiser.com/hcaptcha/Makefile13
-rw-r--r--vendor/go.jolheiser.com/hcaptcha/README.md9
-rw-r--r--vendor/go.jolheiser.com/hcaptcha/error.go41
-rw-r--r--vendor/go.jolheiser.com/hcaptcha/go.mod3
-rw-r--r--vendor/go.jolheiser.com/hcaptcha/hcaptcha.go105
-rw-r--r--vendor/go.jolheiser.com/hcaptcha/response.go10
-rw-r--r--vendor/modules.txt3
9 files changed, 193 insertions, 0 deletions
diff --git a/vendor/go.jolheiser.com/hcaptcha/.gitignore b/vendor/go.jolheiser.com/hcaptcha/.gitignore
new file mode 100644
index 0000000000..a4539540fa
--- /dev/null
+++ b/vendor/go.jolheiser.com/hcaptcha/.gitignore
@@ -0,0 +1,2 @@
+# GoLand
+.idea/ \ No newline at end of file
diff --git a/vendor/go.jolheiser.com/hcaptcha/LICENSE b/vendor/go.jolheiser.com/hcaptcha/LICENSE
new file mode 100644
index 0000000000..0f02641b29
--- /dev/null
+++ b/vendor/go.jolheiser.com/hcaptcha/LICENSE
@@ -0,0 +1,7 @@
+Copyright 2020 John Olheiser
+
+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. \ No newline at end of file
diff --git a/vendor/go.jolheiser.com/hcaptcha/Makefile b/vendor/go.jolheiser.com/hcaptcha/Makefile
new file mode 100644
index 0000000000..5b4272f3ae
--- /dev/null
+++ b/vendor/go.jolheiser.com/hcaptcha/Makefile
@@ -0,0 +1,13 @@
+GO ?= go
+
+.PHONY: fmt
+fmt:
+ $(GO) fmt ./...
+
+.PHONY: test
+test:
+ $(GO) test -race ./...
+
+.PHONY: vet
+vet:
+ $(GO) vet ./... \ No newline at end of file
diff --git a/vendor/go.jolheiser.com/hcaptcha/README.md b/vendor/go.jolheiser.com/hcaptcha/README.md
new file mode 100644
index 0000000000..b13dc2d467
--- /dev/null
+++ b/vendor/go.jolheiser.com/hcaptcha/README.md
@@ -0,0 +1,9 @@
+# hCaptcha
+
+This library was based on the hCaptcha server-side verification [docs](https://docs.hcaptcha.com/#server).
+
+[Example](example_test.go)
+
+## License
+
+[MIT](LICENSE) \ No newline at end of file
diff --git a/vendor/go.jolheiser.com/hcaptcha/error.go b/vendor/go.jolheiser.com/hcaptcha/error.go
new file mode 100644
index 0000000000..6e4d165523
--- /dev/null
+++ b/vendor/go.jolheiser.com/hcaptcha/error.go
@@ -0,0 +1,41 @@
+package hcaptcha
+
+const (
+ ErrMissingInputSecret ErrorCode = "missing-input-secret"
+ ErrInvalidInputSecret ErrorCode = "invalid-input-secret"
+ ErrMissingInputResponse ErrorCode = "missing-input-response"
+ ErrInvalidInputResponse ErrorCode = "invalid-input-response"
+ ErrBadRequest ErrorCode = "bad-request"
+ ErrInvalidOrAlreadySeenResponse ErrorCode = "invalid-or-already-seen-response"
+ ErrSitekeySecretMismatch ErrorCode = "sitekey-secret-mismatch"
+)
+
+// ErrorCode is any possible error from hCaptcha
+type ErrorCode string
+
+// String fulfills the Stringer interface
+func (err ErrorCode) String() string {
+ switch err {
+ case ErrMissingInputSecret:
+ return "Your secret key is missing."
+ case ErrInvalidInputSecret:
+ return "Your secret key is invalid or malformed."
+ case ErrMissingInputResponse:
+ return "The response parameter (verification token) is missing."
+ case ErrInvalidInputResponse:
+ return "The response parameter (verification token) is invalid or malformed."
+ case ErrBadRequest:
+ return "The request is invalid or malformed."
+ case ErrInvalidOrAlreadySeenResponse:
+ return "The response parameter has already been checked, or has another issue."
+ case ErrSitekeySecretMismatch:
+ return "The sitekey is not registered with the provided secret."
+ default:
+ return ""
+ }
+}
+
+// Error fulfills the error interface
+func (err ErrorCode) Error() string {
+ return err.String()
+}
diff --git a/vendor/go.jolheiser.com/hcaptcha/go.mod b/vendor/go.jolheiser.com/hcaptcha/go.mod
new file mode 100644
index 0000000000..7306dc347d
--- /dev/null
+++ b/vendor/go.jolheiser.com/hcaptcha/go.mod
@@ -0,0 +1,3 @@
+module go.jolheiser.com/hcaptcha
+
+go 1.15
diff --git a/vendor/go.jolheiser.com/hcaptcha/hcaptcha.go b/vendor/go.jolheiser.com/hcaptcha/hcaptcha.go
new file mode 100644
index 0000000000..1f21f84008
--- /dev/null
+++ b/vendor/go.jolheiser.com/hcaptcha/hcaptcha.go
@@ -0,0 +1,105 @@
+package hcaptcha
+
+import (
+ "context"
+ "encoding/json"
+ "io/ioutil"
+ "net/http"
+ "net/url"
+ "strings"
+)
+
+const verifyURL = "https://hcaptcha.com/siteverify"
+
+// Client is an hCaptcha client
+type Client struct {
+ ctx context.Context
+ http *http.Client
+
+ secret string
+}
+
+// PostOptions are optional post form values
+type PostOptions struct {
+ RemoteIP string
+ Sitekey string
+}
+
+// ClientOption is a func to modify a new Client
+type ClientOption func(*Client)
+
+// WithHTTP sets the http.Client of a Client
+func WithHTTP(httpClient *http.Client) func(*Client) {
+ return func(hClient *Client) {
+ hClient.http = httpClient
+ }
+}
+
+// WithContext sets the context.Context of a Client
+func WithContext(ctx context.Context) func(*Client) {
+ return func(hClient *Client) {
+ hClient.ctx = ctx
+ }
+}
+
+// New returns a new hCaptcha Client
+func New(secret string, options ...ClientOption) (*Client, error) {
+ if strings.TrimSpace(secret) == "" {
+ return nil, ErrMissingInputSecret
+ }
+
+ client := &Client{
+ ctx: context.Background(),
+ http: http.DefaultClient,
+ secret: secret,
+ }
+
+ for _, opt := range options {
+ opt(client)
+ }
+
+ return client, nil
+}
+
+// Verify checks the response against the hCaptcha API
+func (c *Client) Verify(token string, opts PostOptions) (*Response, error) {
+ if strings.TrimSpace(token) == "" {
+ return nil, ErrMissingInputResponse
+ }
+
+ post := url.Values{
+ "secret": []string{c.secret},
+ "response": []string{token},
+ }
+ if strings.TrimSpace(opts.RemoteIP) != "" {
+ post.Add("remoteip", opts.RemoteIP)
+ }
+ if strings.TrimSpace(opts.Sitekey) != "" {
+ post.Add("sitekey", opts.Sitekey)
+ }
+
+ // Basically a copy of http.PostForm, but with a context
+ req, err := http.NewRequestWithContext(c.ctx, http.MethodPost, verifyURL, strings.NewReader(post.Encode()))
+ if err != nil {
+ return nil, err
+ }
+ req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
+
+ resp, err := c.http.Do(req)
+ if err != nil {
+ return nil, err
+ }
+
+ body, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ return nil, err
+ }
+ defer resp.Body.Close()
+
+ var response *Response
+ if err := json.Unmarshal(body, &response); err != nil {
+ return nil, err
+ }
+
+ return response, nil
+}
diff --git a/vendor/go.jolheiser.com/hcaptcha/response.go b/vendor/go.jolheiser.com/hcaptcha/response.go
new file mode 100644
index 0000000000..007276e6c1
--- /dev/null
+++ b/vendor/go.jolheiser.com/hcaptcha/response.go
@@ -0,0 +1,10 @@
+package hcaptcha
+
+// Response is an hCaptcha response
+type Response struct {
+ Success bool `json:"success"`
+ ChallengeTS string `json:"challenge_ts"`
+ Hostname string `json:"hostname"`
+ Credit bool `json:"credit,omitempty"`
+ ErrorCodes []ErrorCode `json:"error-codes"`
+}
diff --git a/vendor/modules.txt b/vendor/modules.txt
index e4cee3b31f..2604219e86 100644
--- a/vendor/modules.txt
+++ b/vendor/modules.txt
@@ -761,6 +761,9 @@ github.com/yuin/goldmark-highlighting
github.com/yuin/goldmark-meta
# go.etcd.io/bbolt v1.3.5
go.etcd.io/bbolt
+# go.jolheiser.com/hcaptcha v0.0.4
+## explicit
+go.jolheiser.com/hcaptcha
# go.jolheiser.com/pwn v0.0.3
## explicit
go.jolheiser.com/pwn