GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — refactor: remove hcaptcha (#263) · gitLibs/server@198e26a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • internal
    • config
      • config.go
      • new.go
    • web
      • captcha
        • manager.go
        • turnstile
          • manager.go
          • manager_test.go
      • frontend
        • template.go
        • templates
          • login.gohtml
      • handler
        • auth.go
        • auth_test.go
        • common
          • common.go
        • page_login.go
      • req
        • auth.go
        • auth_test.go
  • openapi
    • private.yaml
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎internal/config/config.go‎

Copy file name to clipboardExpand all lines: internal/config/config.go
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ type AppConfig struct {
4545
MeiliSearchURL string `yaml:"meilisearch_url" env:"MEILISEARCH_URL"`
4646
MeiliSearchKey string `yaml:"meilisearch_key" env:"MEILISEARCH_KEY"`
4747

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"`
4950

5051
SlowSQLDuration time.Duration `yaml:"slow_sql_duration" env:"SLOW_SQL_DURATION"`
5152

Collapse file

‎internal/config/new.go‎

Copy file name to clipboardExpand all lines: internal/config/new.go
-13Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package config
1616

1717
import (
1818
"os"
19-
"strings"
2019

2120
"github.com/ilyakaznacheev/cleanenv"
2221
"github.com/spf13/pflag"
@@ -52,17 +51,5 @@ func NewAppConfig() (AppConfig, error) {
5251
return AppConfig{}, err
5352
}
5453

55-
// 太长了
56-
cfg.HCaptchaSecretKey = setDefault(cfg.HCaptchaSecretKey, "0x0000000000000000000000000000000000000000")
57-
58-
cfg.AppType = strings.ToLower(cfg.AppType)
59-
6054
return cfg, nil
6155
}
62-
63-
func setDefault(s string, defaultValue string) string {
64-
if s == "" {
65-
return defaultValue
66-
}
67-
return s
68-
}
Collapse file

‎internal/web/captcha/manager.go‎

Copy file name to clipboardExpand all lines: internal/web/captcha/manager.go
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ import (
2020
"github.com/go-resty/resty/v2"
2121

2222
"github.com/bangumi/server/internal/config"
23-
"github.com/bangumi/server/internal/web/captcha/hcaptcha"
23+
"github.com/bangumi/server/internal/web/captcha/turnstile"
2424
)
2525

2626
type Manager interface {
2727
Verify(ctx context.Context, response string) (bool, error)
2828
}
2929

3030
func New(cfg config.AppConfig, http *resty.Client) (Manager, error) {
31-
if cfg.HCaptchaSecretKey == "0x0000000000000000000000000000000000000000" {
31+
if cfg.TurnstileSecretKey == "1x0000000000000000000000000000000AA" {
3232
return nopeManager{}, nil
3333
}
3434

35-
return hcaptcha.New(cfg, http), nil
35+
return turnstile.New(cfg, http), nil
3636
}
Collapse file

‎internal/web/captcha/hcaptcha/manager.go‎ ‎internal/web/captcha/turnstile/manager.go‎internal/web/captcha/hcaptcha/manager.go renamed to internal/web/captcha/turnstile/manager.go

Copy file name to clipboardExpand all lines: internal/web/captcha/turnstile/manager.go
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// along with this program. If not, see <https://www.gnu.org/licenses/>
1414

1515
//nolint:tagliatelle
16-
package hcaptcha
16+
package turnstile
1717

1818
import (
1919
"context"
@@ -26,15 +26,15 @@ import (
2626
"github.com/bangumi/server/internal/pkg/errgo"
2727
)
2828

29-
const VerifyURL = "https://hcaptcha.com/siteverify"
29+
const VerifyURL = "https://challenges.cloudflare.com/turnstile/v0/siteverify"
3030

3131
type Manager struct {
3232
http *resty.Client
3333
secret string
3434
}
3535

3636
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}
3838
}
3939

4040
func (m Manager) Verify(ctx context.Context, response string) (bool, error) {
Collapse file

‎…nal/web/captcha/hcaptcha/manager_test.go‎ ‎…al/web/captcha/turnstile/manager_test.go‎internal/web/captcha/hcaptcha/manager_test.go renamed to internal/web/captcha/turnstile/manager_test.go internal/web/captcha/hcaptcha/manager_test.go renamed to internal/web/captcha/turnstile/manager_test.go

Copy file name to clipboardExpand all lines: internal/web/captcha/turnstile/manager_test.go
+8-8Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// You should have received a copy of the GNU Affero General Public License
1313
// along with this program. If not, see <https://www.gnu.org/licenses/>
1414

15-
package hcaptcha_test
15+
package turnstile_test
1616

1717
import (
1818
"context"
@@ -23,20 +23,20 @@ import (
2323

2424
"github.com/bangumi/server/internal/config"
2525
"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"
2727
)
2828

2929
func TestManager_Verify(t *testing.T) {
3030
t.Parallel()
3131
test.RequireEnv(t, test.EnvExternalHTTP)
3232
// 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",
3535
}, resty.New())
3636

3737
ok, err := manager.Verify(context.Background(), "10000000-aaaa-bbbb-cccc-000000000001")
3838
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`")
4040
}
4141

4242
require.True(t, ok)
@@ -46,13 +46,13 @@ func TestManager_Verify_fail(t *testing.T) {
4646
t.Parallel()
4747
test.RequireEnv(t, test.EnvExternalHTTP)
4848
// 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",
5151
}, resty.New())
5252

5353
ok, err := manager.Verify(context.Background(), "10000000-aaaa-bbbb-cccc-000000000002")
5454
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`")
5656
}
5757

5858
require.False(t, ok)
Collapse file

‎internal/web/frontend/template.go‎

Copy file name to clipboardExpand all lines: internal/web/frontend/template.go
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ var _ interface {
5757
} = devEngine{}
5858

5959
type Login struct {
60-
Title string
61-
User model.User
60+
Title string
61+
User model.User
62+
TurnstileSiteKey string
6263
}
6364

6465
type ListAccessToken struct {
Collapse file

‎internal/web/frontend/templates/login.gohtml‎

Copy file name to clipboardExpand all lines: internal/web/frontend/templates/login.gohtml
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
}
2727
</script>
2828
{{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+
3031
<div class="container mt-5">
3132
<div class="row">
3233
<ul class="list-group error-details"></ul>
@@ -46,6 +47,7 @@
4647
</div>
4748
<div class="mb-3">
4849
<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>
4951
</div>
5052
<button type="submit" class="btn btn-primary">Submit</button>
5153
</form>
@@ -72,7 +74,7 @@
7274
body: JSON.stringify(data)
7375
});
7476
if (res.status > 300) {
75-
hcaptcha.reset()
77+
turnstile.reset()
7678
const d = await res.json()
7779
if (res.status === 401) {
7880
$(".error-details")
Collapse file

‎internal/web/handler/auth.go‎

Copy file name to clipboardExpand all lines: internal/web/handler/auth.go
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (h Handler) PrivateLogin(c *fiber.Ctx) error {
4242
return h.ValidationError(c, err)
4343
}
4444

45-
ok, err := h.captcha.Verify(c.UserContext(), r.HCaptchaResponse)
45+
ok, err := h.captcha.Verify(c.UserContext(), r.CaptchaResponse)
4646
if err != nil {
4747
return res.FromError(c, err, http.StatusBadGateway, "Failed to connect to hCaptcha server")
4848
}
@@ -51,7 +51,7 @@ func (h Handler) PrivateLogin(c *fiber.Ctx) error {
5151
return res.JSON(c.Status(http.StatusBadRequest), res.Error{
5252
Title: utils.StatusMessage(http.StatusBadRequest),
5353
Description: "can't validate request body",
54-
Details: []string{"未通过hCaptcha验证"},
54+
Details: []string{"未通过captcha验证"},
5555
})
5656
}
5757

Collapse file

‎internal/web/handler/auth_test.go‎

Copy file name to clipboardExpand all lines: internal/web/handler/auth_test.go
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ func TestHandler_PrivateLogin(t *testing.T) {
5959
)
6060

6161
resp := test.New(t).Post("/p/login").JSON(fiber.Map{
62-
"email": "[email protected]",
63-
"password": "p",
64-
"h-captcha-response": "req",
62+
"email": "[email protected]",
63+
"password": "p",
64+
"cf-turnstile-response": "req",
6565
}).Execute(app, 3000)
6666

6767
require.Equal(t, http.StatusOK, resp.StatusCode, resp.BodyString())
Collapse file

‎internal/web/handler/common/common.go‎

Copy file name to clipboardExpand all lines: internal/web/handler/common/common.go
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/gofiber/fiber/v2"
2121
"go.uber.org/zap"
2222

23+
"github.com/bangumi/server/internal/config"
2324
"github.com/bangumi/server/internal/domain"
2425
"github.com/bangumi/server/internal/web/accessor"
2526
"github.com/bangumi/server/internal/web/handler/internal/ctxkey"
@@ -30,6 +31,7 @@ func New(
3031
log *zap.Logger,
3132
auth domain.AuthService,
3233
session session.Manager,
34+
config config.AppConfig,
3335
) (Common, error) {
3436
validate, trans, err := getValidator()
3537
if err != nil {
@@ -38,6 +40,7 @@ func New(
3840

3941
log = log.Named("handler.Common")
4042
return Common{
43+
Config: config,
4144
session: session,
4245
auth: auth,
4346
log: log,
@@ -48,6 +51,7 @@ func New(
4851
}
4952

5053
type Common struct {
54+
Config config.AppConfig
5155
auth domain.AuthService
5256
skip1Log *zap.Logger
5357
log *zap.Logger

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.