-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhandler.go
More file actions
68 lines (58 loc) · 1.53 KB
/
handler.go
File metadata and controls
68 lines (58 loc) · 1.53 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"github.com/go-resty/resty/v2"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/redis/rueidis"
"app/dal"
"app/dto"
)
func newHandler(db *pgxpool.Pool, r rueidis.Client, q *dal.Queries, config Config, template Template) *handler {
return &handler{
db: db,
r: r,
config: config,
callbackURL: fmt.Sprintf("%s/callback", strings.TrimSuffix(config.ExternalHttpAddress, "/")),
q: q,
client: resty.New().SetHeader("User-Agent", "trim21/submit-patch").SetJSONEscapeHTML(false),
template: template,
//tmpl: tmpl,
}
}
type handler struct {
q *dal.Queries
config Config
callbackURL string
db *pgxpool.Pool
r rueidis.Client
client *resty.Client
template Template
}
func (h *handler) validateCaptcha(ctx context.Context, turnstileResponseToken string) error {
var result dto.TurnstileResponse
resp, err := h.client.R().
SetContext(ctx).
SetFormData(map[string]string{
"secret": h.config.TurnstileSecretKey,
"response": turnstileResponseToken,
}).
SetResult(&result).
Post("https://challenges.cloudflare.com/turnstile/v0/siteverify")
if err != nil {
return errors.New("failed to contact captcha verification service")
}
if resp.StatusCode() >= 300 {
return errors.New("captcha verification failed (API error)")
}
if !result.Success {
return &HttpError{
StatusCode: http.StatusBadGateway,
Message: "captcha verification failed",
}
}
return nil
}