-
Notifications
You must be signed in to change notification settings - Fork 27
/
reset_password.go
112 lines (90 loc) · 3.09 KB
/
reset_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
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
package nodes
import (
"net/http"
"github.com/authgear/authgear-server/pkg/lib/interaction"
"github.com/authgear/authgear-server/pkg/lib/successpage"
)
func init() {
interaction.RegisterNode(&NodeResetPasswordBegin{})
interaction.RegisterNode(&NodeResetPasswordEnd{})
}
type NodeResetPasswordBegin struct{}
func (n *NodeResetPasswordBegin) Prepare(ctx *interaction.Context, graph *interaction.Graph) error {
return nil
}
func (n *NodeResetPasswordBegin) GetEffects() ([]interaction.Effect, error) {
return nil, nil
}
func (n *NodeResetPasswordBegin) DeriveEdges(graph *interaction.Graph) ([]interaction.Edge, error) {
return []interaction.Edge{&EdgeResetPassword{}}, nil
}
type InputResetPassword interface {
GetResetPasswordUserID() string
GetNewPassword() string
}
type InputResetPasswordByCode interface {
GetCode() string
GetNewPassword() string
}
type EdgeResetPassword struct{}
func (e *EdgeResetPassword) Instantiate(ctx *interaction.Context, graph *interaction.Graph, rawInput interface{}) (interaction.Node, error) {
var resetInput InputResetPassword
var codeInput InputResetPasswordByCode
successPageCookie := ctx.CookieManager.ValueCookie(successpage.PathCookieDef, "/flows/reset_password/success")
if interaction.Input(rawInput, &resetInput) {
return &NodeResetPasswordEnd{
InputResetPassword: resetInput,
SuccessPageCookie: successPageCookie,
}, nil
} else if interaction.Input(rawInput, &codeInput) {
return &NodeResetPasswordEnd{
InputResetPasswordByCode: codeInput,
SuccessPageCookie: successPageCookie,
}, nil
} else {
return nil, interaction.ErrIncompatibleInput
}
}
type NodeResetPasswordEnd struct {
InputResetPassword InputResetPassword `json:"-"`
InputResetPasswordByCode InputResetPasswordByCode `json:"-"`
SuccessPageCookie *http.Cookie `json:"success_page_cookie,omitempty"`
}
// GetCookies implements CookiesGetter
func (n *NodeResetPasswordEnd) GetCookies() []*http.Cookie {
if n.SuccessPageCookie == nil {
return nil
}
return []*http.Cookie{n.SuccessPageCookie}
}
func (n *NodeResetPasswordEnd) Prepare(ctx *interaction.Context, graph *interaction.Graph) error {
return nil
}
func (n *NodeResetPasswordEnd) GetEffects() ([]interaction.Effect, error) {
return []interaction.Effect{
interaction.EffectOnCommit(func(ctx *interaction.Context, graph *interaction.Graph, nodeIndex int) error {
if n.InputResetPassword != nil {
resetInput := n.InputResetPassword
userID := resetInput.GetResetPasswordUserID()
newPassword := resetInput.GetNewPassword()
err := ctx.ResetPassword.SetPassword(userID, newPassword)
if err != nil {
return err
}
}
if n.InputResetPasswordByCode != nil {
codeInput := n.InputResetPasswordByCode
code := codeInput.GetCode()
newPassword := codeInput.GetNewPassword()
err := ctx.ResetPassword.ResetPassword(code, newPassword)
if err != nil {
return err
}
}
return nil
}),
}, nil
}
func (n *NodeResetPasswordEnd) DeriveEdges(graph *interaction.Graph) ([]interaction.Edge, error) {
return graph.Intent.DeriveEdgesForNode(graph, n)
}