-
Notifications
You must be signed in to change notification settings - Fork 28
/
generate_recovery_code_begin.go
73 lines (58 loc) · 2.06 KB
/
generate_recovery_code_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
package nodes
import (
"github.com/authgear/authgear-server/pkg/lib/authn/authenticator"
"github.com/authgear/authgear-server/pkg/lib/interaction"
)
func init() {
interaction.RegisterNode(&NodeGenerateRecoveryCodeBegin{})
}
type EdgeGenerateRecoveryCode struct {
IsRegenerate bool
}
func (e *EdgeGenerateRecoveryCode) Instantiate(ctx *interaction.Context, graph *interaction.Graph, input interface{}) (interaction.Node, error) {
// Regenerate recovery codes if explicitly requested
doGenerate := e.IsRegenerate
if !doGenerate {
// List all secondary authenticators and see how many of them are new.
// If all of them are new, the user just enrolled into secondary authentication, we need to (re)generate recovery code for them.
userID := graph.MustGetUserID()
ais, err := ctx.Authenticators.List(
userID,
authenticator.KeepKind(authenticator.KindSecondary),
)
if err != nil {
return nil, err
}
newSecondary := authenticator.ApplyFilters(
graph.GetUserNewAuthenticators(),
authenticator.KeepKind(authenticator.KindSecondary),
)
doGenerate = len(newSecondary) != 0 && len(newSecondary) == len(ais)
}
if doGenerate && !*ctx.Config.Authentication.RecoveryCode.Disabled {
recoveryCodes := ctx.MFA.GenerateRecoveryCodes()
return &NodeGenerateRecoveryCodeBegin{
RecoveryCodes: recoveryCodes,
}, nil
}
// Otherwise just end it.
return &NodeGenerateRecoveryCodeEnd{}, nil
}
type NodeGenerateRecoveryCodeBegin struct {
RecoveryCodes []string `json:"recovery_codes"`
}
func (n *NodeGenerateRecoveryCodeBegin) Prepare(ctx *interaction.Context, graph *interaction.Graph) error {
return nil
}
func (n *NodeGenerateRecoveryCodeBegin) GetEffects() ([]interaction.Effect, error) {
return nil, nil
}
func (n *NodeGenerateRecoveryCodeBegin) DeriveEdges(graph *interaction.Graph) ([]interaction.Edge, error) {
return []interaction.Edge{
&EdgeGenerateRecoveryCodeEnd{RecoveryCodes: n.RecoveryCodes},
}, nil
}
// GetRecoveryCodes implements RecoveryCodeNodes.
func (n *NodeGenerateRecoveryCodeBegin) GetRecoveryCodes() []string {
return n.RecoveryCodes
}