-
Notifications
You must be signed in to change notification settings - Fork 0
/
captcha.go
167 lines (147 loc) · 4.97 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package grpc_captcha_validation
import (
"context"
"errors"
"fmt"
"github.com/GoFarsi/grpc-captcha-validation/captcha"
"github.com/golang/protobuf/proto"
"google.golang.org/grpc"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/types/descriptorpb"
"strings"
)
const (
_default_captcha_key = "x-captcha-key"
)
const (
_default_google_validate_address = "https://www.google.com/recaptcha/api/siteverify"
_default_cloudflare_validate_address = "https://challenges.cloudflare.com/turnstile/v0/siteverify"
_default_hcaptcha_validate_address = "https://hcaptcha.com/siteverify"
)
type (
secret string
)
type Captcha struct {
googleSecret secret
cloudflareSecret secret
hCaptchaSecret secret
customHeader string
}
type CaptchaResponse struct {
Success bool `json:"success"`
ChallengeTs any `json:"challenge_ts"`
Hostname string `json:"hostname"`
ErrorCodes []string `json:"error-codes"`
Credit bool `json:"credit"`
Score float32 `json:"score"`
ScoreReason []any `json:"score_reason"`
}
// NewCaptcha create captcha object for access to middleware method
func NewCaptcha(googleRecaptchaSecret, cloudflareRecaptchaSecret, hCaptchaSecret, customHeaderKey string) *Captcha {
return &Captcha{
googleSecret: secret(googleRecaptchaSecret),
cloudflareSecret: secret(cloudflareRecaptchaSecret),
hCaptchaSecret: secret(hCaptchaSecret),
customHeader: customHeaderKey,
}
}
// UnaryServerInterceptor captcha validator for unary server interceptor
func (c *Captcha) UnaryServerInterceptor() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
cp, err := c.extractCaptchaOptionFromDescriptor(strings.Replace(info.FullMethod[1:], "/", ".", -1))
if err != nil {
return nil, err
}
if cp.CheckChallenge {
switch cp.Provider {
case captcha.Provider_GOOGLE:
if err = c.googleSecret.validate(); err != nil {
return nil, err
}
if err = challenge(ctx, _default_google_validate_address, c.googleSecret, c.customHeader); err != nil {
return nil, err
}
case captcha.Provider_CLOUDFLARE:
if err = c.cloudflareSecret.validate(); err != nil {
return nil, err
}
if err = challenge(ctx, _default_cloudflare_validate_address, c.cloudflareSecret, c.customHeader); err != nil {
return nil, err
}
case captcha.Provider_HCAPTCHA:
if err = c.hCaptchaSecret.validate(); err != nil {
return nil, err
}
if err = challenge(ctx, _default_hcaptcha_validate_address, c.hCaptchaSecret, c.customHeader); err != nil {
return nil, err
}
}
}
return handler(ctx, req)
}
}
// StreamServerInterceptor captcha validator for stream server interceptor
func (c *Captcha) StreamServerInterceptor() grpc.StreamServerInterceptor {
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
cp, err := c.extractCaptchaOptionFromDescriptor(strings.Replace(info.FullMethod[1:], "/", ".", -1))
if err != nil {
return err
}
if cp.CheckChallenge {
switch cp.Provider {
case captcha.Provider_GOOGLE:
if err = c.googleSecret.validate(); err != nil {
return err
}
if err = challenge(stream.Context(), _default_google_validate_address, c.googleSecret, c.customHeader); err != nil {
return err
}
case captcha.Provider_CLOUDFLARE:
if err = c.cloudflareSecret.validate(); err != nil {
return err
}
if err = challenge(stream.Context(), _default_cloudflare_validate_address, c.cloudflareSecret, c.customHeader); err != nil {
return err
}
case captcha.Provider_HCAPTCHA:
if err = c.hCaptchaSecret.validate(); err != nil {
return err
}
if err = challenge(stream.Context(), _default_hcaptcha_validate_address, c.hCaptchaSecret, c.customHeader); err != nil {
return err
}
}
}
return handler(srv, stream)
}
}
func (c *Captcha) extractCaptchaOptionFromDescriptor(methodName string) (*captcha.Captcha, error) {
desc, err := protoregistry.GlobalFiles.FindDescriptorByName(protoreflect.FullName(methodName))
if err != nil {
return nil, err
}
methodDesc, ok := desc.(protoreflect.MethodDescriptor)
if !ok {
return nil, errors.New("captcha: failed to assertion method descriptor")
}
options, ok := methodDesc.Options().(*descriptorpb.MethodOptions)
if !ok {
return nil, errors.New("captcha: failed to assertion descriptor options")
}
capExt, err := proto.GetExtension(options, captcha.E_Captcha)
if err != nil {
fmt.Errorf("captcha: failed to get proto extension, got error %s", err.Error())
}
cp, ok := capExt.(*captcha.Captcha)
if !ok {
return nil, errors.New("captcha: failed to assertion captcha object with proto extension")
}
return cp, nil
}
func (c secret) validate() error {
if len(c) == 0 {
return ERR_CAPTCHA_SECRET_IS_EMPTY
}
return nil
}