2021-06-25 12:17:55 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package console
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
)
|
|
|
|
|
|
|
|
const recaptchaAPIURL = "https://www.google.com/recaptcha/api/siteverify"
|
2022-05-06 21:56:18 +01:00
|
|
|
const hcaptchaAPIURL = "https://hcaptcha.com/siteverify"
|
2021-06-25 12:17:55 +01:00
|
|
|
|
2022-05-06 21:56:18 +01:00
|
|
|
// CaptchaHandler is responsible for contacting a captcha API
|
2021-06-25 12:17:55 +01:00
|
|
|
// and returning whether the user response characterized by the given
|
|
|
|
// response token and IP is valid.
|
2022-05-06 21:56:18 +01:00
|
|
|
type CaptchaHandler interface {
|
2022-08-17 11:30:07 +01:00
|
|
|
Verify(ctx context.Context, responseToken string, userIP string) (bool, *float64, error)
|
2021-06-25 12:17:55 +01:00
|
|
|
}
|
|
|
|
|
2022-05-06 21:56:18 +01:00
|
|
|
// CaptchaType is a type of captcha.
|
|
|
|
type CaptchaType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Recaptcha is the type for reCAPTCHA.
|
|
|
|
Recaptcha CaptchaType = iota
|
|
|
|
// Hcaptcha is the type for hCaptcha.
|
|
|
|
Hcaptcha
|
|
|
|
)
|
|
|
|
|
|
|
|
// captchaHandler is a captcha handler that contacts a reCAPTCHA or hCaptcha API.
|
|
|
|
type captchaHandler struct {
|
2021-06-25 12:17:55 +01:00
|
|
|
SecretKey string
|
2022-05-06 21:56:18 +01:00
|
|
|
Endpoint string
|
2021-06-25 12:17:55 +01:00
|
|
|
}
|
|
|
|
|
2022-05-06 21:56:18 +01:00
|
|
|
// NewDefaultCaptcha returns a captcha handler that contacts a reCAPTCHA or hCaptcha API.
|
|
|
|
func NewDefaultCaptcha(kind CaptchaType, secretKey string) CaptchaHandler {
|
|
|
|
handler := captchaHandler{SecretKey: secretKey}
|
|
|
|
switch kind {
|
|
|
|
case Recaptcha:
|
|
|
|
handler.Endpoint = recaptchaAPIURL
|
|
|
|
case Hcaptcha:
|
|
|
|
handler.Endpoint = hcaptchaAPIURL
|
|
|
|
}
|
|
|
|
return handler
|
2021-06-25 12:17:55 +01:00
|
|
|
}
|
|
|
|
|
2022-05-06 21:56:18 +01:00
|
|
|
// Verify contacts the captcha API and returns whether the given response token is valid.
|
|
|
|
// The documentation can be found here for recaptcha: https://developers.google.com/recaptcha/docs/verify
|
|
|
|
// And here for hcaptcha: https://docs.hcaptcha.com/
|
2022-08-17 11:30:07 +01:00
|
|
|
func (r captchaHandler) Verify(ctx context.Context, responseToken string, userIP string) (valid bool, score *float64, err error) {
|
2021-06-25 12:17:55 +01:00
|
|
|
if responseToken == "" {
|
2022-08-17 11:30:07 +01:00
|
|
|
return false, nil, errs.New("the response token is empty")
|
2021-06-25 12:17:55 +01:00
|
|
|
}
|
|
|
|
if userIP == "" {
|
2022-08-17 11:30:07 +01:00
|
|
|
return false, nil, errs.New("the user's IP address is empty")
|
2021-06-25 12:17:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
reqBody := url.Values{
|
|
|
|
"secret": {r.SecretKey},
|
|
|
|
"response": {responseToken},
|
|
|
|
"remoteip": {userIP},
|
|
|
|
}.Encode()
|
|
|
|
|
2022-05-06 21:56:18 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, r.Endpoint, strings.NewReader(reqBody))
|
2021-06-25 12:17:55 +01:00
|
|
|
if err != nil {
|
2022-08-17 11:30:07 +01:00
|
|
|
return false, nil, err
|
2021-06-25 12:17:55 +01:00
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
2022-08-17 11:30:07 +01:00
|
|
|
return false, nil, err
|
2021-06-25 12:17:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, resp.Body.Close())
|
|
|
|
}()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2022-08-17 11:30:07 +01:00
|
|
|
return false, nil, errors.New(resp.Status)
|
2021-06-25 12:17:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var data struct {
|
2022-08-17 11:30:07 +01:00
|
|
|
Success bool `json:"success"`
|
|
|
|
Score float64 `json:"score"`
|
2021-06-25 12:17:55 +01:00
|
|
|
}
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&data)
|
|
|
|
if err != nil {
|
2022-08-17 11:30:07 +01:00
|
|
|
return false, nil, err
|
2021-06-25 12:17:55 +01:00
|
|
|
}
|
|
|
|
|
2022-08-17 11:30:07 +01:00
|
|
|
return data.Success, &data.Score, nil
|
2021-06-25 12:17:55 +01:00
|
|
|
}
|