-
Notifications
You must be signed in to change notification settings - Fork 28
/
create_authenticator_begin.go
464 lines (408 loc) · 14.1 KB
/
create_authenticator_begin.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package nodes
import (
"fmt"
"github.com/authgear/authgear-server/pkg/api"
"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/config"
"github.com/authgear/authgear-server/pkg/lib/interaction"
"github.com/authgear/authgear-server/pkg/util/uuid"
)
func init() {
interaction.RegisterNode(&NodeCreateAuthenticatorBegin{})
}
type EdgeCreateAuthenticatorBegin struct {
Stage authn.AuthenticationStage
AuthenticatorType *model.AuthenticatorType
}
func (e *EdgeCreateAuthenticatorBegin) Instantiate(ctx *interaction.Context, graph *interaction.Graph, input interface{}) (interaction.Node, error) {
skipMFASetup := false
var skipMFASetupInput interface{ SkipMFASetup() bool }
if interaction.Input(input, &skipMFASetupInput) {
skipMFASetup = skipMFASetupInput.SkipMFASetup()
}
requestedByUser := false
var requestedByUserInput interface{ RequestedByUser() bool }
if interaction.Input(input, &requestedByUserInput) {
requestedByUser = requestedByUserInput.RequestedByUser()
}
return &NodeCreateAuthenticatorBegin{
NewAuthenticatorID: uuid.New(),
Stage: e.Stage,
AuthenticatorType: e.AuthenticatorType,
SkipMFASetup: skipMFASetup,
RequestedByUser: requestedByUser,
}, nil
}
type NodeCreateAuthenticatorBegin struct {
NewAuthenticatorID string `json:"new_authenticator_id"`
Stage authn.AuthenticationStage `json:"stage"`
AuthenticatorType *model.AuthenticatorType `json:"authenticator_type"`
SkipMFASetup bool `json:"skip_mfa_setup"`
RequestedByUser bool `json:"requested_by_user"`
Identity *identity.Info `json:"-"`
PrimaryAuthenticator *authenticator.Info `json:"-"`
AuthenticationConfig *config.AuthenticationConfig `json:"-"`
AuthenticatorConfig *config.AuthenticatorConfig `json:"-"`
Authenticators []*authenticator.Info `json:"-"`
}
func (n *NodeCreateAuthenticatorBegin) Prepare(ctx *interaction.Context, graph *interaction.Graph) error {
ais, err := ctx.Authenticators.List(graph.MustGetUserID())
if err != nil {
return err
}
if iden, ok := graph.GetUserLastIdentity(); ok {
n.Identity = iden
}
if prim, ok := graph.GetUserAuthenticator(authn.AuthenticationStagePrimary); ok {
n.PrimaryAuthenticator = prim
}
n.AuthenticationConfig = ctx.Config.Authentication
n.AuthenticatorConfig = ctx.Config.Authenticator
n.Authenticators = ais
return nil
}
func (n *NodeCreateAuthenticatorBegin) GetEffects() ([]interaction.Effect, error) {
return nil, nil
}
func (n *NodeCreateAuthenticatorBegin) DeriveEdges(graph *interaction.Graph) ([]interaction.Edge, error) {
return n.deriveEdges()
}
// IsOOBAuthenticatorTypeAllowed implements SetupOOBOTPNode.
func (n *NodeCreateAuthenticatorBegin) IsOOBAuthenticatorTypeAllowed(oobAuthenticatorType model.AuthenticatorType) (bool, error) {
edges, err := n.deriveEdges()
if err != nil {
return false, err
}
for _, edge := range edges {
switch edge := edge.(type) {
case *EdgeCreateAuthenticatorOOBSetup:
if edge.OOBAuthenticatorType == oobAuthenticatorType {
return true, nil
}
}
}
return false, nil
}
// GetCreateAuthenticatorEdges implements CreateAuthenticatorBeginNode.
func (n *NodeCreateAuthenticatorBegin) GetCreateAuthenticatorEdges() ([]interaction.Edge, error) {
return n.deriveEdges()
}
func (n *NodeCreateAuthenticatorBegin) GetCreateAuthenticatorStage() authn.AuthenticationStage {
return n.Stage
}
func (n *NodeCreateAuthenticatorBegin) deriveEdges() ([]interaction.Edge, error) {
var edges []interaction.Edge
var err error
switch n.Stage {
case authn.AuthenticationStagePrimary:
if n.AuthenticatorType == nil {
edges, err = n.derivePrimary()
if err != nil {
return nil, err
}
} else {
edges, err = n.derivePrimaryWithType(*n.AuthenticatorType)
if err != nil {
return nil, err
}
}
case authn.AuthenticationStageSecondary:
edges = n.deriveSecondary()
default:
panic("interaction: unknown authentication stage: " + n.Stage)
}
// No authenticators needed, skip the stage
if len(edges) == 0 {
edges = append(edges, &EdgeCreateAuthenticatorEnd{Stage: n.Stage, Authenticators: nil})
}
return edges, nil
}
func (n *NodeCreateAuthenticatorBegin) derivePrimaryWithType(typ model.AuthenticatorType) ([]interaction.Edge, error) {
// derivePrimary() assumes the presence of n.Identity
// n.Identity is absent when we are adding passkey in settings.
types := *n.AuthenticationConfig.PrimaryAuthenticators
var edges []interaction.Edge
for _, t := range types {
if t == typ {
if t == model.AuthenticatorTypePasskey {
edges = append(edges, &EdgeCreateAuthenticatorPasskey{
NewAuthenticatorID: n.NewAuthenticatorID,
Stage: n.Stage,
IsDefault: false,
})
}
}
}
interaction.SortAuthenticators(
types,
edges,
func(i int) interaction.SortableAuthenticator {
edge := edges[i]
a, ok := edge.(interaction.SortableAuthenticator)
if !ok {
panic(fmt.Sprintf("interaction: unknown edge: %T", edge))
}
return a
},
)
return edges, nil
}
// nolint:gocognit
func (n *NodeCreateAuthenticatorBegin) derivePrimary() ([]interaction.Edge, error) {
// Determine whether we need to create primary authenticator.
// 1. Check whether the identity actually requires primary authenticator.
// If it does not, then no primary authenticator is needed.
identityRequiresPrimaryAuthentication := len(n.Identity.PrimaryAuthenticatorTypes()) > 0
if !identityRequiresPrimaryAuthentication {
return nil, nil
}
// 2. Check what primary authenticator the developer prefers.
// Here we check if the configuration is non-sense.
types := *n.AuthenticationConfig.PrimaryAuthenticators
if len(types) == 0 {
return nil, api.InvalidConfiguration.New("identity requires primary authenticator but none is enabled")
}
// 3. Find out whether the identity has the preferred primary authenticator.
// If it does not, creation is needed.
ais := authenticator.ApplyFilters(
n.Authenticators,
authenticator.KeepType(types...),
authenticator.KeepPrimaryAuthenticatorOfIdentity(n.Identity),
)
if len(ais) != 0 {
return nil, nil
}
// Primary authenticator is default if it is the first primary authenticator of the user.
isDefault := len(authenticator.ApplyFilters(n.Authenticators, authenticator.KeepKind(authenticator.KindPrimary))) == 0
var edges []interaction.Edge
for _, t := range types {
switch t {
case model.AuthenticatorTypePassword:
edges = append(edges, &EdgeCreateAuthenticatorPassword{
NewAuthenticatorID: n.NewAuthenticatorID,
Stage: n.Stage,
IsDefault: isDefault,
})
case model.AuthenticatorTypePasskey:
edges = append(edges, &EdgeCreateAuthenticatorPasskey{
NewAuthenticatorID: n.NewAuthenticatorID,
Stage: n.Stage,
IsDefault: isDefault,
})
case model.AuthenticatorTypeOOBSMS:
// check if identity login id type match oob type
if n.Identity.LoginID != nil {
if n.Identity.LoginID.LoginIDType == model.LoginIDKeyTypePhone {
if n.AuthenticatorConfig.OOB.SMS.PhoneOTPMode.IsWhatsappEnabled() {
edges = append(edges, &EdgeCreateAuthenticatorWhatsappOTPSetup{
NewAuthenticatorID: n.NewAuthenticatorID,
Stage: n.Stage,
IsDefault: isDefault,
})
}
if n.AuthenticatorConfig.OOB.SMS.PhoneOTPMode.IsSMSEnabled() {
edges = append(edges, &EdgeCreateAuthenticatorOOBSetup{
NewAuthenticatorID: n.NewAuthenticatorID,
Stage: n.Stage,
IsDefault: isDefault,
OOBAuthenticatorType: model.AuthenticatorTypeOOBSMS,
})
}
}
}
case model.AuthenticatorTypeOOBEmail:
// check if identity login id type match oob type
if n.Identity.LoginID != nil {
if n.Identity.LoginID.LoginIDType == model.LoginIDKeyTypeEmail {
if n.AuthenticatorConfig.OOB.Email.EmailOTPMode.IsLoginLinkEnabled() {
edges = append(edges, &EdgeCreateAuthenticatorLoginLinkOTPSetup{
NewAuthenticatorID: n.NewAuthenticatorID,
Stage: n.Stage,
IsDefault: isDefault,
})
}
if n.AuthenticatorConfig.OOB.Email.EmailOTPMode.IsCodeEnabled() {
edges = append(edges, &EdgeCreateAuthenticatorOOBSetup{
NewAuthenticatorID: n.NewAuthenticatorID,
Stage: n.Stage,
IsDefault: isDefault,
OOBAuthenticatorType: model.AuthenticatorTypeOOBEmail,
})
}
}
}
default:
panic(fmt.Sprintf("interaction: unknown authenticator type: %s", t))
}
}
if len(edges) == 0 {
// A new authenticator is required, but no authenticator can be created:
// Configuration is invalid.
return nil, api.InvalidConfiguration.New("no primary authenticator can be created for identity")
}
interaction.SortAuthenticators(
types,
edges,
func(i int) interaction.SortableAuthenticator {
edge := edges[i]
a, ok := edge.(interaction.SortableAuthenticator)
if !ok {
panic(fmt.Sprintf("interaction: unknown edge: %T", edge))
}
return a
},
)
return edges, nil
}
// nolint: gocognit
func (n *NodeCreateAuthenticatorBegin) deriveSecondary() (edges []interaction.Edge) {
// Determine whether we need to create secondary authenticator.
ais := authenticator.ApplyFilters(
n.Authenticators,
authenticator.KeepKind(authenticator.KindSecondary),
)
// Skip setup if explicitly requested
if n.SkipMFASetup {
return nil
}
// Skip setup if MFA is disabled
mode := n.AuthenticationConfig.SecondaryAuthenticationMode
if mode.IsDisabled() {
return nil
}
if !n.RequestedByUser {
// Skip setup if the primary authenticator being used cannot use MFA.
if n.PrimaryAuthenticator == nil || !n.PrimaryAuthenticator.CanHaveMFA() {
return nil
}
// Check secondary authentication mode.
switch mode {
case config.SecondaryAuthenticationModeIfExists:
// secondary authentication is optional.
return nil
case config.SecondaryAuthenticationModeRequired:
// secondary authentication is required.
// Skip setup if the user has at least one secondary authenticator.
if len(ais) > 0 {
return nil
}
}
}
// The created authenticator is default if no other default authenticator
// exists
isDefault := len(authenticator.ApplyFilters(ais, authenticator.KeepDefault)) == 0
// 3. Determine what secondary authenticator we allow the user to create.
// We have the following conditions to hold:
// A. The secondary authenticator is allowed in the configuration.
// B. The user does not have that type of secondary authenticator yet. (This is always true since we have 2)
// C. The number of the secondary authenticator the user is less than maximum.
// D. The secondary authenticator is required by the caller.
passwordCount := 0
totpCount := 0
oobSMSCount := 0
oobEmailCount := 0
for _, a := range ais {
switch a.Type {
case model.AuthenticatorTypePassword:
passwordCount++
case model.AuthenticatorTypeTOTP:
totpCount++
case model.AuthenticatorTypeOOBEmail:
oobEmailCount++
case model.AuthenticatorTypeOOBSMS:
oobSMSCount++
default:
panic("interaction: unknown authenticator type: " + a.Type)
}
}
// Condition A.
for _, typ := range *n.AuthenticationConfig.SecondaryAuthenticators {
switch typ {
case model.AuthenticatorTypePassword:
// Condition B.
edges = append(edges, &EdgeCreateAuthenticatorPassword{
NewAuthenticatorID: n.NewAuthenticatorID,
Stage: n.Stage,
IsDefault: isDefault,
})
case model.AuthenticatorTypeTOTP:
// Condition B and C.
if totpCount < *n.AuthenticatorConfig.TOTP.Maximum {
edges = append(edges, &EdgeCreateAuthenticatorTOTPSetup{
NewAuthenticatorID: n.NewAuthenticatorID,
Stage: n.Stage,
IsDefault: isDefault,
})
}
case model.AuthenticatorTypeOOBEmail:
// Condition B and C.
if oobEmailCount < *n.AuthenticatorConfig.OOB.Email.Maximum {
if n.AuthenticatorConfig.OOB.Email.EmailOTPMode.IsCodeEnabled() {
edges = append(edges, &EdgeCreateAuthenticatorOOBSetup{
NewAuthenticatorID: n.NewAuthenticatorID,
Stage: n.Stage,
IsDefault: isDefault,
OOBAuthenticatorType: model.AuthenticatorTypeOOBEmail,
})
}
if n.AuthenticatorConfig.OOB.Email.EmailOTPMode.IsLoginLinkEnabled() {
edges = append(edges, &EdgeCreateAuthenticatorLoginLinkOTPSetup{
NewAuthenticatorID: n.NewAuthenticatorID,
Stage: n.Stage,
IsDefault: isDefault,
})
}
}
case model.AuthenticatorTypeOOBSMS:
// Condition B and C.
if oobSMSCount < *n.AuthenticatorConfig.OOB.SMS.Maximum {
if n.AuthenticatorConfig.OOB.SMS.PhoneOTPMode.IsWhatsappEnabled() {
edges = append(edges, &EdgeCreateAuthenticatorWhatsappOTPSetup{
NewAuthenticatorID: n.NewAuthenticatorID,
Stage: n.Stage,
IsDefault: isDefault,
})
}
if n.AuthenticatorConfig.OOB.SMS.PhoneOTPMode.IsSMSEnabled() {
edges = append(edges, &EdgeCreateAuthenticatorOOBSetup{
NewAuthenticatorID: n.NewAuthenticatorID,
Stage: n.Stage,
IsDefault: isDefault,
OOBAuthenticatorType: model.AuthenticatorTypeOOBSMS,
})
}
}
default:
panic("interaction: unknown authenticator type: " + typ)
}
}
// Condition D.
if n.AuthenticatorType != nil {
t := *n.AuthenticatorType
n := 0
for _, e := range edges {
edge := e.(interaction.SortableAuthenticator)
if edge.AuthenticatorType() == t {
edges[n] = e
n++
}
}
edges = edges[:n]
}
interaction.SortAuthenticators(
*n.AuthenticationConfig.SecondaryAuthenticators,
edges,
func(i int) interaction.SortableAuthenticator {
edge := edges[i]
a, ok := edge.(interaction.SortableAuthenticator)
if !ok {
panic(fmt.Sprintf("interaction: unknown edge: %T", edge))
}
return a
},
)
return edges
}