mirror of https://github.com/go-gitea/gitea
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>pull/13022/head
parent
5460bf8903
commit
72636fd664
@ -0,0 +1,34 @@ |
||||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package hcaptcha |
||||
|
||||
import ( |
||||
"context" |
||||
|
||||
"code.gitea.io/gitea/modules/setting" |
||||
|
||||
"go.jolheiser.com/hcaptcha" |
||||
) |
||||
|
||||
// Verify calls hCaptcha API to verify token
|
||||
func Verify(ctx context.Context, response string) (bool, error) { |
||||
client, err := hcaptcha.New(setting.Service.HcaptchaSecret, hcaptcha.WithContext(ctx)) |
||||
if err != nil { |
||||
return false, err |
||||
} |
||||
|
||||
resp, err := client.Verify(response, hcaptcha.PostOptions{ |
||||
Sitekey: setting.Service.HcaptchaSitekey, |
||||
}) |
||||
if err != nil { |
||||
return false, err |
||||
} |
||||
|
||||
var respErr error |
||||
if len(resp.ErrorCodes) > 0 { |
||||
respErr = resp.ErrorCodes[0] |
||||
} |
||||
return resp.Success, respErr |
||||
} |
@ -0,0 +1,2 @@ |
||||
# GoLand |
||||
.idea/ |
@ -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. |
@ -0,0 +1,13 @@ |
||||
GO ?= go
|
||||
|
||||
.PHONY: fmt |
||||
fmt: |
||||
$(GO) fmt ./...
|
||||
|
||||
.PHONY: test |
||||
test: |
||||
$(GO) test -race ./...
|
||||
|
||||
.PHONY: vet |
||||
vet: |
||||
$(GO) vet ./...
|
@ -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) |
@ -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() |
||||
} |
@ -0,0 +1,3 @@ |
||||
module go.jolheiser.com/hcaptcha |
||||
|
||||
go 1.15 |
@ -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 |
||||
} |
@ -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"` |
||||
} |
Loading…
Reference in new issue