-
Notifications
You must be signed in to change notification settings - Fork 28
/
authn_whatsapp_trigger.go
127 lines (106 loc) · 4.01 KB
/
authn_whatsapp_trigger.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
package nodes
import (
"github.com/authgear/authgear-server/pkg/api/model"
"github.com/authgear/authgear-server/pkg/lib/authn"
"github.com/authgear/authgear-server/pkg/lib/authn/authenticator"
"github.com/authgear/authgear-server/pkg/lib/authn/otp"
"github.com/authgear/authgear-server/pkg/lib/interaction"
)
func init() {
interaction.RegisterNode(&NodeAuthenticationWhatsappTrigger{})
}
type InputAuthenticationWhatsappTrigger interface {
GetWhatsappAuthenticatorIndex() int
}
type EdgeAuthenticationWhatsappTrigger struct {
Stage authn.AuthenticationStage
Authenticators []*authenticator.Info
}
func (e *EdgeAuthenticationWhatsappTrigger) getAuthenticator(idx int) (*authenticator.Info, error) {
if idx < 0 || idx >= len(e.Authenticators) {
return nil, authenticator.ErrAuthenticatorNotFound
}
return e.Authenticators[idx], nil
}
func (e *EdgeAuthenticationWhatsappTrigger) GetPhone(idx int) string {
info, err := e.getAuthenticator(idx)
if err != nil {
return ""
}
return info.OOBOTP.Phone
}
func (e *EdgeAuthenticationWhatsappTrigger) AuthenticatorType() model.AuthenticatorType {
return model.AuthenticatorTypeOOBSMS
}
func (e *EdgeAuthenticationWhatsappTrigger) IsDefaultAuthenticator() bool {
filtered := authenticator.ApplyFilters(e.Authenticators, authenticator.KeepDefault)
return len(filtered) > 0
}
func (e *EdgeAuthenticationWhatsappTrigger) Instantiate(ctx *interaction.Context, graph *interaction.Graph, rawInput interface{}) (interaction.Node, error) {
var input InputAuthenticationWhatsappTrigger
if !interaction.Input(rawInput, &input) {
return nil, interaction.ErrIncompatibleInput
}
idx := input.GetWhatsappAuthenticatorIndex()
if idx < 0 || idx >= len(e.Authenticators) {
return nil, authenticator.ErrAuthenticatorNotFound
}
targetInfo := e.Authenticators[idx]
phone := targetInfo.OOBOTP.Phone
result, err := NewSendWhatsappCode(ctx, otp.KindOOBOTP, phone, false).Do()
if err != nil {
return nil, err
}
return &NodeAuthenticationWhatsappTrigger{
Stage: e.Stage,
Authenticator: targetInfo,
Authenticators: e.Authenticators,
AuthenticatorIndex: idx,
WhatsappOTPLength: result.CodeLength,
Phone: phone,
}, nil
}
type NodeAuthenticationWhatsappTrigger struct {
Stage authn.AuthenticationStage `json:"stage"`
Authenticator *authenticator.Info `json:"authenticator"`
Authenticators []*authenticator.Info `json:"authenticators"`
AuthenticatorIndex int `json:"authenticator_index"`
WhatsappOTPLength int `json:"whatsapp_otp_length"`
Phone string `json:"phone"`
}
// GetWhatsappOTP implements WhatsappOTPNode.
func (n *NodeAuthenticationWhatsappTrigger) GetWhatsappOTPLength() int {
return n.WhatsappOTPLength
}
// GetPhone implements WhatsappOTPNode.
func (n *NodeAuthenticationWhatsappTrigger) GetPhone() string {
return n.Phone
}
// GetOTPKindFactory implements WhatsappOTPNode.
func (n *NodeAuthenticationWhatsappTrigger) GetOTPKindFactory() otp.KindFactory {
return otp.KindOOBOTP
}
// GetSelectedPhoneNumberForPhoneOTPAuthentication implements AuthenticationPhoneOTPTriggerNode
func (n *NodeAuthenticationWhatsappTrigger) GetSelectedPhoneNumberForPhoneOTPAuthentication() string {
return n.Phone
}
// GetAuthenticatorIndex implements WhatsappOTPAuthnNode.
func (n *NodeAuthenticationWhatsappTrigger) GetAuthenticatorIndex() int {
return n.AuthenticatorIndex
}
func (n *NodeAuthenticationWhatsappTrigger) Prepare(ctx *interaction.Context, graph *interaction.Graph) error {
return nil
}
func (n *NodeAuthenticationWhatsappTrigger) GetEffects() ([]interaction.Effect, error) {
return nil, nil
}
func (n *NodeAuthenticationWhatsappTrigger) DeriveEdges(graph *interaction.Graph) ([]interaction.Edge, error) {
edges := []interaction.Edge{
&EdgeWhatsappOTPResendCode{
Target: n.Phone,
OTPKindFactory: n.GetOTPKindFactory(),
},
&EdgeAuthenticationWhatsapp{Stage: n.Stage, Authenticator: n.Authenticator},
}
return edges, nil
}