add custom selfhosted captcha (#259)

*  Add wicketkeeper captcha

*  Anom config

* 🍱 fix readme

* 🍱 fix lint

* 🍱 fix lint

* 🍱 normalize

* 🍱 fix lint

* 🍱 fix lint

*  Add env for RemediationStatusCode (#250)

*  Add env for defaultStatusCode

* 📝 doc

* change name of the parameter

* 🔧 Add config check

* fix lint

* 📈 Report traffic dropped metrics to LAPI (#223)

* Initial implementation

* fix

* fixes

* Fixes

* xx

* progress

* xx

* xx

* xx

* fix linter

* Progress

* Fixes

* xx

* xx

* Remove trace logger

* Last fix

* fix lint

* fix lint

* fix lint

---------

Co-authored-by: Max Lerebourg <maxlerebourg@gmail.com>

*  Anom config

* 🍱 fix readme

* 🍱 fix lint

* 🍱 normalize

* 🍱 fix lint

* 📝 Add documentation

* 📝 Fix example and makefile and doc for wicketkeeper

* 🍱 fix last things

* 🍱 add disclaimer to use maxlerebourg docker image

* 🍱 Use official wicketpeeker image

* 🍱 revert unnecessary code

* 🍱 fix

---------

Co-authored-by: David <deivid.garcia.garcia@gmail.com>
Co-authored-by: max.lerebourg <max.lerebourg@monisnap.com>
Co-authored-by: mhx <mathieu@hanotaux.fr>
This commit is contained in:
maxlerebourg
2025-09-01 19:41:45 +02:00
committed by GitHub
co-authored by David max.lerebourg mhx
parent 734975c206
commit 7c4f5163e9
13 changed files with 649 additions and 46 deletions
+36 -27
View File
@@ -17,7 +17,6 @@ import (
// Client Captcha client.
type Client struct {
Valid bool
provider string
siteKey string
secretKey string
remediationCustomHeader string
@@ -26,44 +25,54 @@ type Client struct {
cacheClient *cache.Client
httpClient *http.Client
log *logger.Log
infoProvider *infoProvider
}
// Information for self-hosted provider.
type infoProvider struct {
js string
key string
response string
validate string
}
var (
//nolint:gochecknoglobals
captcha = map[string]infoProvider{
configuration.HcaptchaProvider: {
js: "https://hcaptcha.com/1/api.js",
key: "h-captcha",
validate: "https://api.hcaptcha.com/siteverify",
},
configuration.RecaptchaProvider: {
js: "https://www.google.com/recaptcha/api.js",
key: "g-recaptcha",
validate: "https://www.google.com/recaptcha/api/siteverify",
},
configuration.TurnstileProvider: {
js: "https://challenges.cloudflare.com/turnstile/v0/api.js",
key: "cf-turnstile",
validate: "https://challenges.cloudflare.com/turnstile/v0/siteverify",
},
}
)
//nolint:gochecknoglobals
var infoProviders = map[string]*infoProvider{
configuration.HcaptchaProvider: {
js: "https://hcaptcha.com/1/api.js",
key: "h-captcha",
response: "h-captcha-response",
validate: "https://api.hcaptcha.com/siteverify",
},
configuration.RecaptchaProvider: {
js: "https://www.google.com/recaptcha/api.js",
key: "g-recaptcha",
response: "g-recaptcha-response",
validate: "https://www.google.com/recaptcha/api/siteverify",
},
configuration.TurnstileProvider: {
js: "https://challenges.cloudflare.com/turnstile/v0/api.js",
key: "cf-turnstile",
response: "cf-turnstile-response",
validate: "https://challenges.cloudflare.com/turnstile/v0/siteverify",
},
}
// New Initialize captcha client.
func (c *Client) New(log *logger.Log, cacheClient *cache.Client, httpClient *http.Client, provider, siteKey, secretKey, remediationCustomHeader, captchaTemplatePath string, gracePeriodSeconds int64) error {
func (c *Client) New(log *logger.Log, cacheClient *cache.Client, httpClient *http.Client, provider, js, key, response, validate, siteKey, secretKey, remediationCustomHeader, captchaTemplatePath string, gracePeriodSeconds int64) error {
c.Valid = provider != ""
if !c.Valid {
return nil
}
var info *infoProvider
if provider == configuration.CustomProvider {
info = &infoProvider{js: js, key: key, response: response, validate: validate}
} else {
info = infoProviders[provider]
}
c.infoProvider = info
c.siteKey = siteKey
c.secretKey = secretKey
c.provider = provider
c.remediationCustomHeader = remediationCustomHeader
html, _ := configuration.GetHTMLTemplate(captchaTemplatePath)
c.captchaTemplate = html
@@ -95,8 +104,8 @@ func (c *Client) ServeHTTP(rw http.ResponseWriter, r *http.Request, remoteIP str
rw.WriteHeader(http.StatusOK)
err = c.captchaTemplate.Execute(rw, map[string]string{
"SiteKey": c.siteKey,
"FrontendJS": captcha[c.provider].js,
"FrontendKey": captcha[c.provider].key,
"FrontendJS": c.infoProvider.js,
"FrontendKey": c.infoProvider.key,
})
if err != nil {
c.log.Info("captcha:ServeHTTP captchaTemplateServe " + err.Error())
@@ -121,7 +130,7 @@ func (c *Client) Validate(r *http.Request) (bool, error) {
c.log.Debug("captcha:Validate invalid method: " + r.Method)
return false, nil
}
var response = r.FormValue(captcha[c.provider].key + "-response")
var response = r.FormValue(c.infoProvider.response)
if response == "" {
c.log.Debug("captcha:Validate no captcha response found in request")
return false, nil
@@ -129,7 +138,7 @@ func (c *Client) Validate(r *http.Request) (bool, error) {
var body = url.Values{}
body.Add("secret", c.secretKey)
body.Add("response", response)
res, err := c.httpClient.PostForm(captcha[c.provider].validate, body)
res, err := c.httpClient.PostForm(c.infoProvider.validate, body)
if err != nil {
return false, err
}