-
Notifications
You must be signed in to change notification settings - Fork 28
/
create_authenticator_password.go
86 lines (71 loc) · 2.45 KB
/
create_authenticator_password.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
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/interaction"
)
func init() {
interaction.RegisterNode(&NodeCreateAuthenticatorPassword{})
}
type InputCreateAuthenticatorPassword interface {
GetPassword() string
}
type EdgeCreateAuthenticatorPassword struct {
NewAuthenticatorID string
Stage authn.AuthenticationStage
IsDefault bool
}
func (e *EdgeCreateAuthenticatorPassword) AuthenticatorType() model.AuthenticatorType {
return model.AuthenticatorTypePassword
}
func (e *EdgeCreateAuthenticatorPassword) IsDefaultAuthenticator() bool {
return false
}
func (e *EdgeCreateAuthenticatorPassword) Instantiate(ctx *interaction.Context, graph *interaction.Graph, rawInput interface{}) (interaction.Node, error) {
var stageInput InputAuthenticationStage
if !interaction.Input(rawInput, &stageInput) {
return nil, interaction.ErrIncompatibleInput
}
stage := stageInput.GetAuthenticationStage()
if stage != e.Stage {
return nil, interaction.ErrIncompatibleInput
}
var input InputCreateAuthenticatorPassword
if !interaction.Input(rawInput, &input) {
return nil, interaction.ErrIncompatibleInput
}
userID := graph.MustGetUserID()
spec := &authenticator.Spec{
UserID: userID,
IsDefault: e.IsDefault,
Kind: stageToAuthenticatorKind(e.Stage),
Type: model.AuthenticatorTypePassword,
Password: &authenticator.PasswordSpec{
PlainPassword: input.GetPassword(),
},
}
info, err := ctx.Authenticators.NewWithAuthenticatorID(e.NewAuthenticatorID, spec)
if err != nil {
return nil, err
}
return &NodeCreateAuthenticatorPassword{Stage: e.Stage, Authenticator: info}, nil
}
type NodeCreateAuthenticatorPassword struct {
Stage authn.AuthenticationStage `json:"stage"`
Authenticator *authenticator.Info `json:"authenticator"`
}
func (n *NodeCreateAuthenticatorPassword) Prepare(ctx *interaction.Context, graph *interaction.Graph) error {
return nil
}
func (n *NodeCreateAuthenticatorPassword) GetEffects() ([]interaction.Effect, error) {
return nil, nil
}
func (n *NodeCreateAuthenticatorPassword) DeriveEdges(graph *interaction.Graph) ([]interaction.Edge, error) {
return []interaction.Edge{
&EdgeCreateAuthenticatorEnd{
Stage: n.Stage,
Authenticators: []*authenticator.Info{n.Authenticator},
},
}, nil
}