-
Notifications
You must be signed in to change notification settings - Fork 28
/
utils_whatsapp.go
77 lines (67 loc) · 2.01 KB
/
utils_whatsapp.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
package nodes
import (
"github.com/authgear/authgear-server/pkg/api/apierrors"
"github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/lib/authn/otp"
"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/lib/interaction"
"github.com/authgear/authgear-server/pkg/lib/ratelimit"
)
type SendWhatsappCodeResult struct {
Target string
CodeLength int
Kind otp.Kind
}
type SendWhatsappCode struct {
KindFactory func(config *config.AppConfig, channel model.AuthenticatorOOBChannel) otp.Kind
Context *interaction.Context
Target string
IsResend bool
}
func (s *SendWhatsappCode) Do() (*SendWhatsappCodeResult, error) {
channel := model.AuthenticatorOOBChannelWhatsapp
form := otp.FormCode
kind := s.KindFactory(s.Context.Config, channel)
result := &SendWhatsappCodeResult{
Target: s.Target,
CodeLength: form.CodeLength(),
Kind: kind,
}
msg, err := s.Context.OTPSender.Prepare(channel, s.Target, form, otp.MessageTypeWhatsappCode)
if !s.IsResend && apierrors.IsKind(err, ratelimit.RateLimited) {
// Ignore the rate limit error and do NOT send the code.
return result, nil
} else if err != nil {
return nil, err
}
defer msg.Close()
code, err := s.Context.OTPCodeService.GenerateOTP(
kind,
s.Target,
form,
&otp.GenerateOptions{WebSessionID: s.Context.WebSessionID},
)
if !s.IsResend && apierrors.IsKind(err, ratelimit.RateLimited) {
// Ignore the rate limit error and do NOT send the code.
return result, nil
} else if err != nil {
return nil, err
}
err = s.Context.OTPSender.Send(msg, otp.SendOptions{OTP: code})
if err != nil {
return nil, err
}
return result, nil
}
func NewSendWhatsappCode(
ctx *interaction.Context,
kindFactory func(config *config.AppConfig, channel model.AuthenticatorOOBChannel) otp.Kind,
target string,
isResend bool) *SendWhatsappCode {
return &SendWhatsappCode{
Context: ctx,
KindFactory: kindFactory,
Target: target,
IsResend: isResend,
}
}