-
Notifications
You must be signed in to change notification settings - Fork 27
/
create_authenticator_whatsapp_otp_setup.go
153 lines (130 loc) · 4.87 KB
/
create_authenticator_whatsapp_otp_setup.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
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/feature/verification"
"github.com/authgear/authgear-server/pkg/lib/interaction"
)
func init() {
interaction.RegisterNode(&NodeCreateAuthenticatorWhatsappOTPSetup{})
}
type InputCreateAuthenticatorWhatsappOTPSetup interface {
GetWhatsappPhone() string
}
type EdgeCreateAuthenticatorWhatsappOTPSetup struct {
NewAuthenticatorID string
Stage authn.AuthenticationStage
IsDefault bool
}
type InputCreateAuthenticatorWhatsappOTPSetupSelect interface {
SetupPrimaryAuthenticatorWhatsappOTP()
}
func (e *EdgeCreateAuthenticatorWhatsappOTPSetup) IsDefaultAuthenticator() bool {
return false
}
func (e *EdgeCreateAuthenticatorWhatsappOTPSetup) AuthenticatorType() model.AuthenticatorType {
return model.AuthenticatorTypeOOBSMS
}
func (e *EdgeCreateAuthenticatorWhatsappOTPSetup) Instantiate(ctx *interaction.Context, graph *interaction.Graph, rawInput interface{}) (interaction.Node, error) {
var userID string
var phone string
if e.Stage == authn.AuthenticationStagePrimary {
var input InputCreateAuthenticatorWhatsappOTPSetupSelect
matchedInput := interaction.Input(rawInput, &input)
if !matchedInput && !interaction.IsAdminAPI(rawInput) {
return nil, interaction.ErrIncompatibleInput
}
identityInfo := graph.MustGetUserLastIdentity()
userID = identityInfo.UserID
phone = identityInfo.LoginID.LoginID
} else {
var input InputCreateAuthenticatorWhatsappOTPSetup
if !interaction.Input(rawInput, &input) {
return nil, interaction.ErrIncompatibleInput
}
userID = graph.MustGetUserID()
phone = input.GetWhatsappPhone()
}
spec := &authenticator.Spec{
UserID: userID,
IsDefault: e.IsDefault,
Kind: stageToAuthenticatorKind(e.Stage),
Type: e.AuthenticatorType(),
OOBOTP: &authenticator.OOBOTPSpec{
Phone: phone,
},
}
info, err := ctx.Authenticators.NewWithAuthenticatorID(e.NewAuthenticatorID, spec)
if err != nil {
return nil, err
}
phone = info.OOBOTP.ToTarget()
var skipInput interface{ SkipVerification() bool }
if interaction.Input(rawInput, &skipInput) && skipInput.SkipVerification() {
// Admin skip verify whatsapp otp and create OOB authenticator directly
return &NodeCreateAuthenticatorOOB{Stage: e.Stage, Authenticator: info}, nil
}
// Skip checking whatsapp otp if the phone number is verified
// Create OOB authenticator directly
aStatus, err := ctx.Verification.GetAuthenticatorVerificationStatus(info)
if err != nil {
return nil, err
}
if aStatus == verification.AuthenticatorStatusVerified {
return &NodeCreateAuthenticatorOOB{Stage: e.Stage, Authenticator: info}, nil
}
result, err := NewSendWhatsappCode(ctx, otp.KindOOBOTP, phone, false).Do()
if err != nil {
return nil, err
}
return &NodeCreateAuthenticatorWhatsappOTPSetup{
Stage: e.Stage,
Authenticator: info,
WhatsappOTPLength: result.CodeLength,
Phone: phone,
}, nil
}
type NodeCreateAuthenticatorWhatsappOTPSetup struct {
Stage authn.AuthenticationStage `json:"stage"`
Authenticator *authenticator.Info `json:"authenticator"`
WhatsappOTPLength int `json:"whatsapp_otp_length"`
Phone string `json:"phone"`
}
// GetWhatsappOTPLength implements WhatsappOTPNode.
func (n *NodeCreateAuthenticatorWhatsappOTPSetup) GetWhatsappOTPLength() int {
return n.WhatsappOTPLength
}
// GetPhone implements WhatsappOTPNode.
func (n *NodeCreateAuthenticatorWhatsappOTPSetup) GetPhone() string {
return n.Phone
}
// GetOTPKindFactory implements WhatsappOTPNode.
func (n *NodeCreateAuthenticatorWhatsappOTPSetup) GetOTPKindFactory() otp.KindFactory {
return otp.KindOOBOTP
}
// GetCreateAuthenticatorStage implements CreateAuthenticatorPhoneOTPNode
func (n *NodeCreateAuthenticatorWhatsappOTPSetup) GetCreateAuthenticatorStage() authn.AuthenticationStage {
return n.Stage
}
// GetSelectedPhoneNumberForPhoneOTP implements CreateAuthenticatorPhoneOTPNode
func (n *NodeCreateAuthenticatorWhatsappOTPSetup) GetSelectedPhoneNumberForPhoneOTP() string {
return n.Phone
}
func (n *NodeCreateAuthenticatorWhatsappOTPSetup) Prepare(ctx *interaction.Context, graph *interaction.Graph) error {
return nil
}
func (n *NodeCreateAuthenticatorWhatsappOTPSetup) GetEffects() ([]interaction.Effect, error) {
return nil, nil
}
func (n *NodeCreateAuthenticatorWhatsappOTPSetup) DeriveEdges(graph *interaction.Graph) ([]interaction.Edge, error) {
edges := []interaction.Edge{
&EdgeWhatsappOTPResendCode{
Target: n.Phone,
OTPKindFactory: n.GetOTPKindFactory(),
},
&EdgeCreateAuthenticatorWhatsappOTP{Stage: n.Stage, Authenticator: n.Authenticator},
}
return edges, nil
}