Skip to content

Commit

Permalink
Insert SelectIdentity node before ForgotPassword node
Browse files Browse the repository at this point in the history
  • Loading branch information
andychow326 authored and louischan-oursky committed Nov 14, 2022
1 parent 37338e4 commit 2c4eed6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pkg/auth/handler/webapp/forgot_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (h *ForgotPasswordHandler) ServeHTTP(w http.ResponseWriter, r *http.Request

loginID := r.Form.Get("q_login_id")

input = &InputForgotPassword{
input = &InputUseLoginID{
LoginID: loginID,
}
return
Expand Down
8 changes: 0 additions & 8 deletions pkg/auth/handler/webapp/interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,6 @@ func (i *InputChangePassword) GetAuthenticationStage() authn.AuthenticationStage
func (i *InputChangePassword) GetOldPassword() string { return i.OldPassword }
func (i *InputChangePassword) GetNewPassword() string { return i.NewPassword }

type InputForgotPassword struct {
LoginID string
}

var _ nodes.InputForgotPasswordSelectLoginID = &InputForgotPassword{}

func (i *InputForgotPassword) GetLoginID() string { return i.LoginID }

type InputResetPassword struct {
Code string
Password string
Expand Down
16 changes: 14 additions & 2 deletions pkg/lib/interaction/intents/forgot_password.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package intents

import (
"fmt"

"github.com/authgear/authgear-server/pkg/lib/interaction"
"github.com/authgear/authgear-server/pkg/lib/interaction/nodes"
)
Expand All @@ -16,9 +18,19 @@ func NewIntentForgotPassword() *IntentForgotPassword {
}

func (i *IntentForgotPassword) InstantiateRootNode(ctx *interaction.Context, graph *interaction.Graph) (interaction.Node, error) {
return &nodes.NodeForgotPasswordBegin{}, nil
edge := nodes.EdgeSelectIdentityBegin{}
return edge.Instantiate(ctx, graph, i)
}

func (i *IntentForgotPassword) DeriveEdgesForNode(graph *interaction.Graph, node interaction.Node) ([]interaction.Edge, error) {
return nil, nil
switch node := node.(type) {
case *nodes.NodeSelectIdentityEnd:
return []interaction.Edge{
&nodes.EdgeForgotPasswordBegin{
IdentityInfo: node.IdentityInfo,
},
}, nil
default:
panic(fmt.Errorf("interaction: unexpected node: %T", node))
}
}
32 changes: 20 additions & 12 deletions pkg/lib/interaction/nodes/forgot_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,19 @@ func init() {
interaction.RegisterNode(&NodeForgotPasswordEnd{})
}

type EdgeForgotPasswordBegin struct {
IdentityInfo *identity.Info `json:"identity_info"`
}

func (e *EdgeForgotPasswordBegin) Instantiate(ctx *interaction.Context, graph *interaction.Graph, rawInput interface{}) (interaction.Node, error) {
return &NodeForgotPasswordBegin{
IdentityInfo: e.IdentityInfo,
}, nil
}

type NodeForgotPasswordBegin struct {
LoginIDKeys []config.LoginIDKeyConfig `json:"-"`
LoginIDKeys []config.LoginIDKeyConfig `json:"-"`
IdentityInfo *identity.Info `json:"identity_info"`
}

func (n *NodeForgotPasswordBegin) Prepare(ctx *interaction.Context, graph *interaction.Graph) error {
Expand All @@ -35,21 +46,19 @@ func (n *NodeForgotPasswordBegin) DeriveEdges(graph *interaction.Graph) ([]inter

func (n *NodeForgotPasswordBegin) edge() *EdgeForgotPasswordSelectLoginID {
return &EdgeForgotPasswordSelectLoginID{
Configs: n.LoginIDKeys,
Configs: n.LoginIDKeys,
IdentityInfo: n.IdentityInfo,
}
}

func (n *NodeForgotPasswordBegin) GetIdentityCandidates() []identity.Candidate {
return n.edge().GetIdentityCandidates()
}

type InputForgotPasswordSelectLoginID interface {
GetLoginID() string
}

type EdgeForgotPasswordSelectLoginID struct {
Configs []config.LoginIDKeyConfig
RedirectURI string
Configs []config.LoginIDKeyConfig `json:"-"`
RedirectURI string `json:"-"`
IdentityInfo *identity.Info `json:"identity_info"`
}

// GetIdentityCandidates implements IdentityCandidatesGetter.
Expand All @@ -63,12 +72,11 @@ func (e *EdgeForgotPasswordSelectLoginID) GetIdentityCandidates() []identity.Can
}

func (e *EdgeForgotPasswordSelectLoginID) Instantiate(ctx *interaction.Context, graph *interaction.Graph, rawInput interface{}) (interaction.Node, error) {
var input InputForgotPasswordSelectLoginID
if !interaction.Input(rawInput, &input) {
return nil, interaction.ErrIncompatibleInput
if e.IdentityInfo == nil {
return nil, forgotpasswordFillDetails(interaction.ErrUserNotFound)
}

loginID := input.GetLoginID()
loginID := e.IdentityInfo.LoginID.LoginID

err := ctx.ForgotPassword.SendCode(loginID)
if errors.Is(err, forgotpassword.ErrUserNotFound) {
Expand Down

0 comments on commit 2c4eed6

Please sign in to comment.