GeezerVison is showing github.com — refactor: remove hcaptcha (#263) · gitLibs/server@198e26a
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: internal/config/config.go +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -45,7 +45,8 @@ type AppConfig struct { | 45 | 45 | MeiliSearchURL string `yaml:"meilisearch_url" env:"MEILISEARCH_URL"` | 46 | 46 | MeiliSearchKey string `yaml:"meilisearch_key" env:"MEILISEARCH_KEY"` | 47 | 47 |
| 48 | | - HCaptchaSecretKey string `yaml:"hcaptcha_secret_key" env:"HCAPTCHA_SECRET_KEY"` | | 48 | + TurnstileSecretKey string `env:"TURNSTILE_SECRET_KEY" env-default:"1x0000000000000000000000000000000AA"` | | 49 | + TurnstileSiteKey string `env:"TURNSTILE_SITE_KEY" env-default:"1x00000000000000000000AA"` | 49 | 50 |
| 50 | 51 | SlowSQLDuration time.Duration `yaml:"slow_sql_duration" env:"SLOW_SQL_DURATION"` | 51 | 52 |
| |
Collapse file Copy file name to clipboardExpand all lines: internal/config/new.go -13Lines changed: 0 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,7 +16,6 @@ package config | 16 | 16 |
| 17 | 17 | import ( | 18 | 18 | "os" | 19 | | - "strings" | 20 | 19 |
| 21 | 20 | "github.com/ilyakaznacheev/cleanenv" | 22 | 21 | "github.com/spf13/pflag" | @@ -52,17 +51,5 @@ func NewAppConfig() (AppConfig, error) { | 52 | 51 | return AppConfig{}, err | 53 | 52 | } | 54 | 53 |
| 55 | | - // 太长了 | 56 | | - cfg.HCaptchaSecretKey = setDefault(cfg.HCaptchaSecretKey, "0x0000000000000000000000000000000000000000") | 57 | | - | 58 | | - cfg.AppType = strings.ToLower(cfg.AppType) | 59 | | - | 60 | 54 | return cfg, nil | 61 | 55 | } | 62 | | - | 63 | | -func setDefault(s string, defaultValue string) string { | 64 | | - if s == "" { | 65 | | - return defaultValue | 66 | | - } | 67 | | - return s | 68 | | -} |
Collapse file Copy file name to clipboardExpand all lines: internal/web/captcha/manager.go +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,17 +20,17 @@ import ( | 20 | 20 | "github.com/go-resty/resty/v2" | 21 | 21 |
| 22 | 22 | "github.com/bangumi/server/internal/config" | 23 | | - "github.com/bangumi/server/internal/web/captcha/hcaptcha" | | 23 | + "github.com/bangumi/server/internal/web/captcha/turnstile" | 24 | 24 | ) | 25 | 25 |
| 26 | 26 | type Manager interface { | 27 | 27 | Verify(ctx context.Context, response string) (bool, error) | 28 | 28 | } | 29 | 29 |
| 30 | 30 | func New(cfg config.AppConfig, http *resty.Client) (Manager, error) { | 31 | | - if cfg.HCaptchaSecretKey == "0x0000000000000000000000000000000000000000" { | | 31 | + if cfg.TurnstileSecretKey == "1x0000000000000000000000000000000AA" { | 32 | 32 | return nopeManager{}, nil | 33 | 33 | } | 34 | 34 |
| 35 | | - return hcaptcha.New(cfg, http), nil | | 35 | + return turnstile.New(cfg, http), nil | 36 | 36 | } |
Collapse file Copy file name to clipboardExpand all lines: internal/web/captcha/turnstile/manager.go +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 13 | 13 | // along with this program. If not, see <https://www.gnu.org/licenses/> | 14 | 14 |
| 15 | 15 | //nolint:tagliatelle | 16 | | -package hcaptcha | | 16 | +package turnstile | 17 | 17 |
| 18 | 18 | import ( | 19 | 19 | "context" | @@ -26,15 +26,15 @@ import ( | 26 | 26 | "github.com/bangumi/server/internal/pkg/errgo" | 27 | 27 | ) | 28 | 28 |
| 29 | | -const VerifyURL = "https://hcaptcha.com/siteverify" | | 29 | +const VerifyURL = "https://challenges.cloudflare.com/turnstile/v0/siteverify" | 30 | 30 |
| 31 | 31 | type Manager struct { | 32 | 32 | http *resty.Client | 33 | 33 | secret string | 34 | 34 | } | 35 | 35 |
| 36 | 36 | func New(cfg config.AppConfig, http *resty.Client) Manager { | 37 | | - return Manager{secret: cfg.HCaptchaSecretKey, http: http} | | 37 | + return Manager{secret: cfg.TurnstileSecretKey, http: http} | 38 | 38 | } | 39 | 39 |
| 40 | 40 | func (m Manager) Verify(ctx context.Context, response string) (bool, error) { | |
Collapse file Copy file name to clipboardExpand all lines: internal/web/captcha/turnstile/manager_test.go +8-8Lines changed: 8 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 12 | 12 | // You should have received a copy of the GNU Affero General Public License | 13 | 13 | // along with this program. If not, see <https://www.gnu.org/licenses/> | 14 | 14 |
| 15 | | -package hcaptcha_test | | 15 | +package turnstile_test | 16 | 16 |
| 17 | 17 | import ( | 18 | 18 | "context" | @@ -23,20 +23,20 @@ import ( | 23 | 23 |
| 24 | 24 | "github.com/bangumi/server/internal/config" | 25 | 25 | "github.com/bangumi/server/internal/pkg/test" | 26 | | - "github.com/bangumi/server/internal/web/captcha/hcaptcha" | | 26 | + "github.com/bangumi/server/internal/web/captcha/turnstile" | 27 | 27 | ) | 28 | 28 |
| 29 | 29 | func TestManager_Verify(t *testing.T) { | 30 | 30 | t.Parallel() | 31 | 31 | test.RequireEnv(t, test.EnvExternalHTTP) | 32 | 32 | // testing key, checkout https://docs.hcaptcha.com/#integration-testing-test-keys | 33 | | - manager := hcaptcha.New(config.AppConfig{ | 34 | | - HCaptchaSecretKey: "0x0000000000000000000000000000000000000000", | | 33 | + manager := turnstile.New(config.AppConfig{ | | 34 | + TurnstileSecretKey: "1x0000000000000000000000000000000AA", | 35 | 35 | }, resty.New()) | 36 | 36 |
| 37 | 37 | ok, err := manager.Verify(context.Background(), "10000000-aaaa-bbbb-cccc-000000000001") | 38 | 38 | if err != nil { | 39 | | - t.Fatal("unexpected hCaptcha error, you may need to set a proxy with `HTTPS_PROXY`") | | 39 | + t.Fatal("unexpected turnstile error, you may need to set a proxy with `HTTPS_PROXY`") | 40 | 40 | } | 41 | 41 |
| 42 | 42 | require.True(t, ok) | @@ -46,13 +46,13 @@ func TestManager_Verify_fail(t *testing.T) { | 46 | 46 | t.Parallel() | 47 | 47 | test.RequireEnv(t, test.EnvExternalHTTP) | 48 | 48 | // testing key, checkout https://docs.hcaptcha.com/#integration-testing-test-keys | 49 | | - manager := hcaptcha.New(config.AppConfig{ | 50 | | - HCaptchaSecretKey: "0x0000000000000000000000000000000000000000", | | 49 | + manager := turnstile.New(config.AppConfig{ | | 50 | + TurnstileSecretKey: "2x0000000000000000000000000000000AA", | 51 | 51 | }, resty.New()) | 52 | 52 |
| 53 | 53 | ok, err := manager.Verify(context.Background(), "10000000-aaaa-bbbb-cccc-000000000002") | 54 | 54 | if err != nil { | 55 | | - t.Fatal("unexpected hCaptcha error, you may need to set a proxy with `HTTPS_PROXY`") | | 55 | + t.Fatal("unexpected turnstile error, you may need to set a proxy with `HTTPS_PROXY`") | 56 | 56 | } | 57 | 57 |
| 58 | 58 | require.False(t, ok) | |
Collapse file Copy file name to clipboardExpand all lines: internal/web/frontend/template.go +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -57,8 +57,9 @@ var _ interface { | 57 | 57 | } = devEngine{} | 58 | 58 |
| 59 | 59 | type Login struct { | 60 | | - Title string | 61 | | - User model.User | | 60 | + Title string | | 61 | + User model.User | | 62 | + TurnstileSiteKey string | 62 | 63 | } | 63 | 64 |
| 64 | 65 | type ListAccessToken struct { | |
Collapse file Copy file name to clipboardExpand all lines: internal/web/frontend/templates/login.gohtml +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 26 | 26 | } | 27 | 27 | </script> | 28 | 28 | {{else}} | 29 | | -<script src="https://js.hcaptcha.com/1/api.js?hl=zh-CN" defer></script> | | 29 | +<script src='https://challenges.cloudflare.com/turnstile/v0/api.js' async defer></script> | | 30 | + | 30 | 31 | <div class="container mt-5"> | 31 | 32 | <div class="row"> | 32 | 33 | <ul class="list-group error-details"></ul> | | 46 | 47 | </div> | 47 | 48 | <div class="mb-3"> | 48 | 49 | <div class="h-captcha" data-sitekey="4874acee-9c6e-4e47-99ad-e2ea1606961f"></div> | | 50 | + <div class='cf-turnstile' data-action='login' data-sitekey='{{ .TurnstileSiteKey }}' data-theme='light'></div> | 49 | 51 | </div> | 50 | 52 | <button type="submit" class="btn btn-primary">Submit</button> | 51 | 53 | </form> | | 72 | 74 | body: JSON.stringify(data) | 73 | 75 | }); | 74 | 76 | if (res.status > 300) { | 75 | | - hcaptcha.reset() | | 77 | + turnstile.reset() | 76 | 78 | const d = await res.json() | 77 | 79 | if (res.status === 401) { | 78 | 80 | $(".error-details") | |
Collapse file Copy file name to clipboardExpand all lines: internal/web/handler/auth.go +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -42,7 +42,7 @@ func (h Handler) PrivateLogin(c *fiber.Ctx) error { | 42 | 42 | return h.ValidationError(c, err) | 43 | 43 | } | 44 | 44 |
| 45 | | - ok, err := h.captcha.Verify(c.UserContext(), r.HCaptchaResponse) | | 45 | + ok, err := h.captcha.Verify(c.UserContext(), r.CaptchaResponse) | 46 | 46 | if err != nil { | 47 | 47 | return res.FromError(c, err, http.StatusBadGateway, "Failed to connect to hCaptcha server") | 48 | 48 | } | @@ -51,7 +51,7 @@ func (h Handler) PrivateLogin(c *fiber.Ctx) error { | 51 | 51 | return res.JSON(c.Status(http.StatusBadRequest), res.Error{ | 52 | 52 | Title: utils.StatusMessage(http.StatusBadRequest), | 53 | 53 | Description: "can't validate request body", | 54 | | - Details: []string{"未通过hCaptcha验证"}, | | 54 | + Details: []string{"未通过captcha验证"}, | 55 | 55 | }) | 56 | 56 | } | 57 | 57 |
| |
Collapse file Copy file name to clipboardExpand all lines: internal/web/handler/auth_test.go +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -59,9 +59,9 @@ func TestHandler_PrivateLogin(t *testing.T) { | 59 | 59 | ) | 60 | 60 |
| 61 | 61 | resp := test.New(t).Post("/p/login").JSON(fiber.Map{ | 62 | | - | 63 | | - "password": "p", | 64 | | - "h-captcha-response": "req", | | 62 | + | | 63 | + "password": "p", | | 64 | + "cf-turnstile-response": "req", | 65 | 65 | }).Execute(app, 3000) | 66 | 66 |
| 67 | 67 | require.Equal(t, http.StatusOK, resp.StatusCode, resp.BodyString()) | |
Collapse file Copy file name to clipboardExpand all lines: internal/web/handler/common/common.go +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,6 +20,7 @@ import ( | 20 | 20 | "github.com/gofiber/fiber/v2" | 21 | 21 | "go.uber.org/zap" | 22 | 22 |
| | 23 | + "github.com/bangumi/server/internal/config" | 23 | 24 | "github.com/bangumi/server/internal/domain" | 24 | 25 | "github.com/bangumi/server/internal/web/accessor" | 25 | 26 | "github.com/bangumi/server/internal/web/handler/internal/ctxkey" | @@ -30,6 +31,7 @@ func New( | 30 | 31 | log *zap.Logger, | 31 | 32 | auth domain.AuthService, | 32 | 33 | session session.Manager, | | 34 | + config config.AppConfig, | 33 | 35 | ) (Common, error) { | 34 | 36 | validate, trans, err := getValidator() | 35 | 37 | if err != nil { | @@ -38,6 +40,7 @@ func New( | 38 | 40 |
| 39 | 41 | log = log.Named("handler.Common") | 40 | 42 | return Common{ | | 43 | + Config: config, | 41 | 44 | session: session, | 42 | 45 | auth: auth, | 43 | 46 | log: log, | @@ -48,6 +51,7 @@ func New( | 48 | 51 | } | 49 | 52 |
| 50 | 53 | type Common struct { | | 54 | + Config config.AppConfig | 51 | 55 | auth domain.AuthService | 52 | 56 | skip1Log *zap.Logger | 53 | 57 | log *zap.Logger | |
|