Skip to content

Commit

Permalink
refactor: move expired error into top-level flow module
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Oct 19, 2021
1 parent dc2adbf commit 01a2602
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 56 deletions.
30 changes: 2 additions & 28 deletions selfservice/flow/recovery/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package recovery
import (
"net/http"
"net/url"
"time"

"github.com/ory/x/sqlxx"

Expand All @@ -26,22 +25,6 @@ var (
ErrAlreadyLoggedIn = herodot.ErrBadRequest.WithReason("A valid session was detected and thus recovery is not possible.")
)

type FlowExpiredError struct {
*herodot.DefaultError
ago time.Duration
}

func NewFlowExpiredError(at time.Time) *FlowExpiredError {
ago := time.Since(at)
return &FlowExpiredError{
ago: ago,
DefaultError: herodot.ErrBadRequest.
WithError("recovery flow expired").
WithReasonf(`The recovery flow has expired. Please restart the flow.`).
WithReasonf("The recovery flow expired %.2f minutes ago, please try again.", ago.Minutes()),
}
}

type (
errorHandlerDependencies interface {
errorx.ManagementProvider
Expand All @@ -67,15 +50,6 @@ func NewErrorHandler(d errorHandlerDependencies) *ErrorHandler {
return &ErrorHandler{d: d}
}

func MethodToNodeGroup(method string) node.Group {
switch method {
case StrategyRecoveryLinkName:
return node.RecoveryLinkGroup
default:
return node.DefaultGroup
}
}

func (s *ErrorHandler) WriteFlowError(
w http.ResponseWriter,
r *http.Request,
Expand All @@ -94,7 +68,7 @@ func (s *ErrorHandler) WriteFlowError(
return
}

if e := new(FlowExpiredError); errors.As(err, &e) {
if e := new(flow.ExpiredError); errors.As(err, &e) {
// create new flow because the old one is not valid
a, err := FromOldFlow(s.d.Config(r.Context()), s.d.Config(r.Context()).SelfServiceFlowRecoveryRequestLifespan(), s.d.GenerateCSRFToken(r), r, s.d.RecoveryStrategies(r.Context()), *f)
if err != nil {
Expand All @@ -103,7 +77,7 @@ func (s *ErrorHandler) WriteFlowError(
return
}

a.UI.Messages.Add(text.NewErrorValidationRecoveryFlowExpired(e.ago))
a.UI.Messages.Add(text.NewErrorValidationRecoveryFlowExpired(e.Ago))
if err := s.d.RecoveryFlowPersister().CreateRecoveryFlow(r.Context(), a); err != nil {
s.forward(w, r, a, err)
return
Expand Down
4 changes: 2 additions & 2 deletions selfservice/flow/recovery/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func TestHandleError(t *testing.T) {
t.Cleanup(reset)

recoveryFlow = newFlow(t, time.Minute, flow.TypeAPI)
flowError = recovery.NewFlowExpiredError(anHourAgo)
flowError = flow.NewFlowExpiredError(anHourAgo)
methodName = recovery.StrategyRecoveryLinkName

res, err := ts.Client().Do(testhelpers.NewHTTPGetJSONRequest(t, ts.URL+"/error"))
Expand Down Expand Up @@ -194,7 +194,7 @@ func TestHandleError(t *testing.T) {
t.Cleanup(reset)

recoveryFlow = &recovery.Flow{Type: flow.TypeBrowser}
flowError = recovery.NewFlowExpiredError(anHourAgo)
flowError = flow.NewFlowExpiredError(anHourAgo)
methodName = node.RecoveryLinkGroup

lf, _ := expectRecoveryUI(t)
Expand Down
2 changes: 1 addition & 1 deletion selfservice/flow/recovery/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (f Flow) GetNID() uuid.UUID {

func (f *Flow) Valid() error {
if f.ExpiresAt.Before(time.Now().UTC()) {
return errors.WithStack(NewFlowExpiredError(f.ExpiresAt))
return errors.WithStack(flow.NewFlowExpiredError(f.ExpiresAt))
}
return nil
}
Expand Down
22 changes: 2 additions & 20 deletions selfservice/flow/verification/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package verification
import (
"net/http"
"net/url"
"time"

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

"github.com/pkg/errors"

"github.com/ory/x/sqlxx"

"github.com/ory/herodot"
"github.com/ory/x/urlx"

"github.com/ory/kratos/driver/config"
Expand Down Expand Up @@ -43,24 +41,8 @@ type (
ErrorHandler struct {
d errorHandlerDependencies
}

FlowExpiredError struct {
*herodot.DefaultError
ago time.Duration
}
)

func NewFlowExpiredError(at time.Time) *FlowExpiredError {
ago := time.Since(at)
return &FlowExpiredError{
ago: ago,
DefaultError: herodot.ErrBadRequest.
WithError("verification flow expired").
WithReasonf(`The verification flow has expired. Please restart the flow.`).
WithReasonf("The verification flow expired %.2f minutes ago, please try again.", ago.Minutes()),
}
}

func NewErrorHandler(d errorHandlerDependencies) *ErrorHandler {
return &ErrorHandler{d: d}
}
Expand All @@ -83,7 +65,7 @@ func (s *ErrorHandler) WriteFlowError(
return
}

if e := new(FlowExpiredError); errors.As(err, &e) {
if e := new(flow.ExpiredError); errors.As(err, &e) {
// create new flow because the old one is not valid
a, err := FromOldFlow(s.d.Config(r.Context()), s.d.Config(r.Context()).SelfServiceFlowVerificationRequestLifespan(),
s.d.GenerateCSRFToken(r), r, s.d.VerificationStrategies(r.Context()), f)
Expand All @@ -93,7 +75,7 @@ func (s *ErrorHandler) WriteFlowError(
return
}

a.UI.Messages.Add(text.NewErrorValidationVerificationFlowExpired(e.ago))
a.UI.Messages.Add(text.NewErrorValidationVerificationFlowExpired(e.Ago))
if err := s.d.VerificationFlowPersister().CreateVerificationFlow(r.Context(), a); err != nil {
s.forward(w, r, a, err)
return
Expand Down
4 changes: 2 additions & 2 deletions selfservice/flow/verification/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func TestHandleError(t *testing.T) {
t.Cleanup(reset)

verificationFlow = newFlow(t, time.Minute, flow.TypeAPI)
flowError = verification.NewFlowExpiredError(anHourAgo)
flowError = flow.NewFlowExpiredError(anHourAgo)
methodName = verification.StrategyVerificationLinkName

res, err := ts.Client().Do(testhelpers.NewHTTPGetJSONRequest(t, ts.URL+"/error"))
Expand Down Expand Up @@ -194,7 +194,7 @@ func TestHandleError(t *testing.T) {
t.Cleanup(reset)

verificationFlow = &verification.Flow{Type: flow.TypeBrowser}
flowError = verification.NewFlowExpiredError(anHourAgo)
flowError = flow.NewFlowExpiredError(anHourAgo)
methodName = node.VerificationLinkGroup

lf, _ := expectVerificationUI(t)
Expand Down
2 changes: 1 addition & 1 deletion selfservice/flow/verification/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func NewPostHookFlow(conf *config.Config, exp time.Duration, csrf string, r *htt

func (f *Flow) Valid() error {
if f.ExpiresAt.Before(time.Now()) {
return errors.WithStack(NewFlowExpiredError(f.ExpiresAt))
return errors.WithStack(flow.NewFlowExpiredError(f.ExpiresAt))
}
return nil
}
Expand Down
4 changes: 3 additions & 1 deletion selfservice/strategy/link/token_recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"time"

"github.com/ory/kratos/selfservice/flow"

"github.com/ory/kratos/corp"

"github.com/gofrs/uuid"
Expand Down Expand Up @@ -78,7 +80,7 @@ func NewRecoveryToken(address *identity.RecoveryAddress, expiresIn time.Duration

func (f *RecoveryToken) Valid() error {
if f.ExpiresAt.Before(time.Now()) {
return errors.WithStack(recovery.NewFlowExpiredError(f.ExpiresAt))
return errors.WithStack(flow.NewFlowExpiredError(f.ExpiresAt))
}
return nil
}
4 changes: 3 additions & 1 deletion selfservice/strategy/link/token_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"time"

"github.com/ory/kratos/selfservice/flow"

"github.com/ory/kratos/corp"

"github.com/gofrs/uuid"
Expand Down Expand Up @@ -67,7 +69,7 @@ func NewSelfServiceVerificationToken(address *identity.VerifiableAddress, f *ver

func (f *VerificationToken) Valid() error {
if f.ExpiresAt.Before(time.Now().UTC()) {
return errors.WithStack(verification.NewFlowExpiredError(f.ExpiresAt))
return errors.WithStack(flow.NewFlowExpiredError(f.ExpiresAt))
}
return nil
}

0 comments on commit 01a2602

Please sign in to comment.