Skip to content

Commit

Permalink
refactor: Check an identity to update settings is present
Browse files Browse the repository at this point in the history
  • Loading branch information
sawadashota committed Feb 17, 2022
1 parent e325031 commit d0cd751
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
25 changes: 14 additions & 11 deletions selfservice/flow/settings/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@ import (
"net/http"
"time"

"github.com/ory/kratos/text"

"github.com/ory/kratos/ui/node"
"github.com/ory/x/sqlcon"

"github.com/julienschmidt/httprouter"
"github.com/ory/herodot"
"github.com/ory/nosurf"
"github.com/ory/x/sqlcon"
"github.com/ory/x/urlx"
"github.com/pkg/errors"

"github.com/ory/herodot"
"github.com/ory/kratos/continuity"
"github.com/ory/kratos/driver/config"
"github.com/ory/kratos/identity"
"github.com/ory/kratos/schema"
"github.com/ory/kratos/selfservice/errorx"
"github.com/ory/kratos/selfservice/flow"
"github.com/ory/kratos/session"
"github.com/ory/kratos/text"
"github.com/ory/kratos/ui/node"
"github.com/ory/kratos/x"
"github.com/ory/nosurf"
"github.com/ory/x/urlx"
)

const (
Expand Down Expand Up @@ -537,12 +535,17 @@ func (h *Handler) submitSettingsFlow(w http.ResponseWriter, r *http.Request, ps
}

if updateContext == nil {
c := &UpdateContext{Session: ss, Flow: f}
h.d.SettingsFlowErrorHandler().WriteFlowError(w, r, node.DefaultGroup, f, c.GetIdentityToUpdate(), errors.WithStack(schema.NewNoSettingsStrategyResponsible()))
h.d.SettingsFlowErrorHandler().WriteFlowError(w, r, node.DefaultGroup, f, ss.Identity, errors.WithStack(schema.NewNoSettingsStrategyResponsible()))
return
}
i, ok := updateContext.GetIdentityToUpdate()
if !ok {
// An identity to update must always be present.
h.d.SettingsFlowErrorHandler().WriteFlowError(w, r, node.DefaultGroup, f, ss.Identity, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("Could not find a identity to update.")))
return
}

if err := h.d.SettingsHookExecutor().PostSettingsHook(w, r, s, updateContext, updateContext.GetIdentityToUpdate()); err != nil {
if err := h.d.SettingsHookExecutor().PostSettingsHook(w, r, s, updateContext, i); err != nil {
h.d.SettingsFlowErrorHandler().WriteFlowError(w, r, node.DefaultGroup, f, ss.Identity, err)
return
}
Expand Down
13 changes: 11 additions & 2 deletions selfservice/flow/settings/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import (
"net/http"
"time"

"github.com/ory/herodot"

"github.com/ory/kratos/text"
"github.com/ory/kratos/ui/node"

"github.com/ory/kratos/schema"
"github.com/ory/x/sqlcon"

"github.com/ory/kratos/schema"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -223,6 +226,12 @@ func (e *HookExecutor) PostSettingsHook(w http.ResponseWriter, r *http.Request,
return nil
}

x.ContentNegotiationRedirection(w, r, ctxUpdate.GetIdentityToUpdate().CopyWithoutCredentials(), e.d.Writer(), returnTo.String())
i, ok := ctxUpdate.GetIdentityToUpdate()
if !ok {
// An identity to update must always be present.
return errors.WithStack(herodot.ErrInternalServerError.WithReasonf("Could not find a identity to update."))
}

x.ContentNegotiationRedirection(w, r, i.CopyWithoutCredentials(), e.d.Writer(), returnTo.String())
return nil
}
7 changes: 3 additions & 4 deletions selfservice/flow/settings/strategy_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ func (c *UpdateContext) UpdateIdentity(i *identity.Identity) {
c.toUpdate = i
}

func (c *UpdateContext) GetIdentityToUpdate() *identity.Identity {
func (c *UpdateContext) GetIdentityToUpdate() (*identity.Identity, bool) {
if c.toUpdate == nil {
return c.GetSessionIdentity()
return nil, false
}

return c.toUpdate
return c.toUpdate, true
}

func (c UpdateContext) GetSessionIdentity() *identity.Identity {
Expand Down

0 comments on commit d0cd751

Please sign in to comment.