-
Notifications
You must be signed in to change notification settings - Fork 405
/
Copy pathcaptcha.go
51 lines (42 loc) · 1.03 KB
/
captcha.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"context"
"flag"
"fmt"
"github.com/gnolang/gno/tm2/pkg/commands"
)
type captchaCfg struct {
rootCfg *serveCfg
captchaSecret string
}
var errCaptchaMissing = fmt.Errorf("captcha secret is required")
func (c *captchaCfg) RegisterFlags(fs *flag.FlagSet) {
fs.StringVar(
&c.captchaSecret,
"captcha-secret",
"",
"recaptcha secret key (if empty, captcha are disabled)",
)
}
func newCaptchaCmd(rootCfg *serveCfg) *commands.Command {
cfg := &captchaCfg{
rootCfg: rootCfg,
}
return commands.NewCommand(
commands.Metadata{
Name: "captcha",
ShortUsage: "captcha [flags]",
LongHelp: "applies captcha middleware to the gno.land faucet",
},
cfg,
func(ctx context.Context, args []string) error {
return execCaptcha(ctx, cfg, commands.NewDefaultIO())
},
)
}
func execCaptcha(ctx context.Context, cfg *captchaCfg, io commands.IO) error {
if cfg.captchaSecret == "" {
return errCaptchaMissing
}
return serveFaucet(ctx, cfg.rootCfg, io, getCaptchaMiddleware(cfg.captchaSecret))
}