-
Notifications
You must be signed in to change notification settings - Fork 28
/
create_authenticator_login_link_otp_setup.go
176 lines (152 loc) · 5.54 KB
/
create_authenticator_login_link_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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/identity"
"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(&NodeCreateAuthenticatorLoginLinkOTPSetup{})
}
type InputCreateAuthenticatorLoginLinkOTPSetup interface {
GetLoginLinkOTPTarget() string
}
type EdgeCreateAuthenticatorLoginLinkOTPSetup struct {
NewAuthenticatorID string
Stage authn.AuthenticationStage
IsDefault bool
}
type InputCreateAuthenticatorLoginLinkOTPSetupSelect interface {
SetupPrimaryAuthenticatorLoginLinkOTP()
}
func (e *EdgeCreateAuthenticatorLoginLinkOTPSetup) IsDefaultAuthenticator() bool {
return false
}
func (e *EdgeCreateAuthenticatorLoginLinkOTPSetup) AuthenticatorType() model.AuthenticatorType {
// Currently only support send through email
return model.AuthenticatorTypeOOBEmail
}
func (e *EdgeCreateAuthenticatorLoginLinkOTPSetup) Instantiate(ctx *interaction.Context, graph *interaction.Graph, rawInput interface{}) (interaction.Node, error) {
var target string
if e.Stage == authn.AuthenticationStagePrimary {
var input InputCreateAuthenticatorLoginLinkOTPSetupSelect
matchedInput := interaction.Input(rawInput, &input)
if !matchedInput && !interaction.IsAdminAPI(rawInput) {
return nil, interaction.ErrIncompatibleInput
}
identityInfo := graph.MustGetUserLastIdentity()
target = identityInfo.LoginID.LoginID
} else {
var input InputCreateAuthenticatorLoginLinkOTPSetup
if !interaction.Input(rawInput, &input) {
return nil, interaction.ErrIncompatibleInput
}
target = input.GetLoginLinkOTPTarget()
}
var spec *authenticator.Spec
var identityInfo *identity.Info
if e.Stage == authn.AuthenticationStagePrimary {
// Primary OOB authenticators must be bound to login ID identity
identityInfo = graph.MustGetUserLastIdentity()
if identityInfo.Type != model.IdentityTypeLoginID {
panic("interaction: OOB authenticator identity must be login ID")
}
spec = &authenticator.Spec{
UserID: identityInfo.UserID,
IsDefault: e.IsDefault,
Kind: stageToAuthenticatorKind(e.Stage),
Type: model.AuthenticatorTypeOOBEmail,
OOBOTP: &authenticator.OOBOTPSpec{},
}
} else {
userID := graph.MustGetUserID()
spec = &authenticator.Spec{
UserID: userID,
IsDefault: e.IsDefault,
Kind: stageToAuthenticatorKind(e.Stage),
Type: model.AuthenticatorTypeOOBEmail,
OOBOTP: &authenticator.OOBOTPSpec{},
}
}
spec.OOBOTP.Email = target
info, err := ctx.Authenticators.NewWithAuthenticatorID(e.NewAuthenticatorID, spec)
if err != nil {
return nil, err
}
target = info.OOBOTP.ToTarget()
var skipInput interface{ SkipVerification() bool }
if interaction.Input(rawInput, &skipInput) && skipInput.SkipVerification() {
// Admin skip verify LoginLink otp and create OOB authenticator directly
return &NodeCreateAuthenticatorLoginLinkOTP{Stage: e.Stage, Authenticator: info}, nil
}
aStatus, err := ctx.Verification.GetAuthenticatorVerificationStatus(info)
if err != nil {
return nil, err
}
if aStatus == verification.AuthenticatorStatusVerified {
return &NodeCreateAuthenticatorLoginLinkOTP{Stage: e.Stage, Authenticator: info}, nil
}
result, err := (&SendOOBCode{
Context: ctx,
Stage: e.Stage,
IsAuthenticating: false,
AuthenticatorInfo: info,
IgnoreRatelimitError: true,
OTPForm: otp.FormLink,
}).Do()
if err != nil {
return nil, err
}
return &NodeCreateAuthenticatorLoginLinkOTPSetup{
Stage: e.Stage,
Authenticator: info,
Target: target,
Channel: result.Channel,
}, nil
}
type NodeCreateAuthenticatorLoginLinkOTPSetup struct {
Stage authn.AuthenticationStage `json:"stage"`
Authenticator *authenticator.Info `json:"authenticator"`
Target string `json:"target"`
Channel string `json:"channel"`
}
// GetLoginLinkOTPTarget implements LoginLinkOTPNode.
func (n *NodeCreateAuthenticatorLoginLinkOTPSetup) GetLoginLinkOTPTarget() string {
return n.Target
}
// GetLoginLinkOTPChannel implements LoginLinkOTPNode.
func (n *NodeCreateAuthenticatorLoginLinkOTPSetup) GetLoginLinkOTPChannel() string {
return n.Channel
}
// GetLoginLinkOTPOOBType implements LoginLinkOTPNode.
func (n *NodeCreateAuthenticatorLoginLinkOTPSetup) GetLoginLinkOTPOOBType() interaction.OOBType {
switch n.Stage {
case authn.AuthenticationStagePrimary:
return interaction.OOBTypeSetupPrimary
case authn.AuthenticationStageSecondary:
return interaction.OOBTypeSetupSecondary
default:
panic("interaction: unknown authentication stage: " + n.Stage)
}
}
func (n *NodeCreateAuthenticatorLoginLinkOTPSetup) Prepare(ctx *interaction.Context, graph *interaction.Graph) error {
return nil
}
func (n *NodeCreateAuthenticatorLoginLinkOTPSetup) GetEffects() ([]interaction.Effect, error) {
return nil, nil
}
func (n *NodeCreateAuthenticatorLoginLinkOTPSetup) DeriveEdges(graph *interaction.Graph) ([]interaction.Edge, error) {
edges := []interaction.Edge{
&EdgeOOBResendCode{
Stage: n.Stage,
IsAuthenticating: false,
Authenticator: n.Authenticator,
OTPForm: otp.FormLink,
},
&EdgeCreateAuthenticatorLoginLinkOTP{Stage: n.Stage, Authenticator: n.Authenticator},
}
return edges, nil
}