Skip to content

Commit

Permalink
feat: captcha is now optional
Browse files Browse the repository at this point in the history
  • Loading branch information
ingvaar committed May 18, 2022
1 parent e66f92b commit 61228b1
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 31 deletions.
13 changes: 10 additions & 3 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
FlagCaptchaSecret = "captcha-secret"
FlagCaptchaURL = "captcha-verify-url"
FlagCaptchaScore = "captcha-min-score"
FlagEnableCaptcha = "captcha"
)

var serverConfig server.Config
Expand Down Expand Up @@ -45,20 +46,26 @@ func NewStartCommand() *cobra.Command {
startCmd.Flags().StringVar(&addr, FlagAddress, ":8080", "rest api address")
startCmd.Flags().BoolVar(&serverConfig.EnableMetrics, FlagMetrics, false, "enable metrics endpoint")
startCmd.Flags().BoolVar(&serverConfig.EnableHealth, FlagHealth, false, "enable health endpoint")
startCmd.Flags().BoolVar(
&serverConfig.CaptchaConf.Enable,
FlagEnableCaptcha,
false,
"enable captcha verification",
)
startCmd.Flags().StringVar(
&serverConfig.CaptchaSecret,
&serverConfig.CaptchaConf.Secret,
FlagCaptchaSecret,
"",
"set Captcha secret",
)
startCmd.Flags().StringVar(
&serverConfig.CaptchaVerifyURL,
&serverConfig.CaptchaConf.VerifyURL,
FlagCaptchaURL,
"https://www.google.com/recaptcha/api/siteverify",
"set Captcha verify URL",
)
startCmd.Flags().Float64Var(
&serverConfig.CaptchaMinScore,
&serverConfig.CaptchaConf.MinScore,
FlagCaptchaScore,
0.5,
"set Captcha min score",
Expand Down
9 changes: 2 additions & 7 deletions graph/schema.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package graph

import (
"context"
"errors"
"okp4/cosmos-faucet/graph/generated"
"okp4/cosmos-faucet/graph/model"

Expand All @@ -14,12 +13,8 @@ import (
)

func (r *mutationResolver) Send(ctx context.Context, input model.SendInput) (*model.TxResponse, error) {
if input.CaptchaToken != nil {
if err := r.CaptchaResolver.CheckRecaptcha(ctx, *input.CaptchaToken); err != nil {
return nil, err
}
} else {
return nil, errors.New("captcha token not specified")
if err := r.CaptchaResolver.CheckRecaptcha(ctx, input.CaptchaToken); err != nil {
return nil, err
}

resp, err := r.Faucet.SendTxMsg(ctx, input.ToAddress)
Expand Down
2 changes: 1 addition & 1 deletion graph/schema.resolvers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func newMockCaptchaResolver() captcha.Resolver {
return mockCaptchaResolver{}
}

func (r mockCaptchaResolver) CheckRecaptcha(_ context.Context, _ string) error {
func (r mockCaptchaResolver) CheckRecaptcha(_ context.Context, _ *string) error {
return nil
}

Expand Down
20 changes: 14 additions & 6 deletions internal/captcha/captcha.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,24 @@ import (
)

type Resolver interface {
CheckRecaptcha(context.Context, string) error
CheckRecaptcha(context.Context, *string) error
}

func NewCaptchaResolver(secret, verifyURL string, minScore float64) Resolver {
if secret == "" {
type ResolverConfig struct {
Secret string `mapstructure:"captcha-secret"`
VerifyURL string `mapstructure:"captcha-verify-url"`
MinScore float64 `mapstructure:"captcha-min-score"`
Enable bool `mapstructure:"captcha"`
}

func NewCaptchaResolver(config ResolverConfig) Resolver {
if config.Enable && config.Secret == "" {
log.Error().Msg("Required Captcha secret not set")
}
return resolver{
secret: secret,
siteVerifyURL: verifyURL,
minScore: minScore,
secret: config.Secret,
siteVerifyURL: config.VerifyURL,
minScore: config.MinScore,
enable: config.Enable,
}
}
14 changes: 12 additions & 2 deletions internal/captcha/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,19 @@ type resolver struct {
secret string
siteVerifyURL string
minScore float64
enable bool
}

func (c resolver) CheckRecaptcha(ctx context.Context, response string) error {
func (c resolver) CheckRecaptcha(ctx context.Context, response *string) error {
if !c.enable {
return nil
}

if response == nil {
log.Debug().Msg("No captcha token specified")
return fmt.Errorf("no captcha token specified")
}

req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.siteVerifyURL, nil)
if err != nil {
log.Error().Err(err).Msgf("Error while creating Captcha verification request: %s", err.Error())
Expand All @@ -35,7 +45,7 @@ func (c resolver) CheckRecaptcha(ctx context.Context, response string) error {

q := req.URL.Query()
q.Add("secret", c.secret)
q.Add("response", response)
q.Add("response", *response)
req.URL.RawQuery = q.Encode()

resp, err := http.DefaultClient.Do(req)
Expand Down
8 changes: 2 additions & 6 deletions internal/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ func (s *httpServer) createRoutes(config Config) {
graphql.NewDefaultServer(
generated.NewExecutableSchema(generated.Config{
Resolvers: &graph.Resolver{
Faucet: config.Faucet,
CaptchaResolver: captcha.NewCaptchaResolver(
config.CaptchaSecret,
config.CaptchaVerifyURL,
config.CaptchaMinScore,
),
Faucet: config.Faucet,
CaptchaResolver: captcha.NewCaptchaResolver(config.CaptchaConf),
},
}),
),
Expand Down
11 changes: 5 additions & 6 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"net/http"
"okp4/cosmos-faucet/internal/captcha"
"okp4/cosmos-faucet/pkg/client"

"github.com/gorilla/mux"
Expand All @@ -10,12 +11,10 @@ import (

// Config holds config of the http server.
type Config struct {
EnableMetrics bool `mapstructure:"metrics"`
EnableHealth bool `mapstructure:"health"`
Faucet client.Faucet
CaptchaSecret string `mapstructure:"captcha-secret"`
CaptchaVerifyURL string `mapstructure:"captcha-verify-url"`
CaptchaMinScore float64 `mapstructure:"captcha-min-score"`
EnableMetrics bool `mapstructure:"metrics"`
EnableHealth bool `mapstructure:"health"`
Faucet client.Faucet
CaptchaConf captcha.ResolverConfig
}

// HTTPServer exposes server methods.
Expand Down

0 comments on commit 61228b1

Please sign in to comment.