Skip to content

Commit

Permalink
fix: improve expired error responses
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Aug 25, 2020
1 parent f261c44 commit 124a92e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
19 changes: 12 additions & 7 deletions selfservice/flow/login/handler.go
Expand Up @@ -5,9 +5,8 @@ import (
"time"

"github.com/julienschmidt/httprouter"
"github.com/pkg/errors"

"github.com/ory/x/urlx"
"github.com/pkg/errors"

"github.com/ory/kratos/driver/configuration"
"github.com/ory/kratos/selfservice/errorx"
Expand Down Expand Up @@ -50,11 +49,11 @@ func NewHandler(d handlerDependencies, c configuration.Provider) *Handler {
func (h *Handler) RegisterPublicRoutes(public *x.RouterPublic) {
public.GET(RouteInitBrowserFlow, h.initBrowserFlow)
public.GET(RouteInitAPIFlow, h.initAPIFlow)
public.GET(RouteGetFlow, h.fetchLoginFlow)
public.GET(RouteGetFlow, h.fetchFlow)
}

func (h *Handler) RegisterAdminRoutes(admin *x.RouterAdmin) {
admin.GET(RouteGetFlow, h.fetchLoginFlow)
admin.GET(RouteGetFlow, h.fetchFlow)
}

func (h *Handler) NewLoginFlow(w http.ResponseWriter, r *http.Request, flow flow.Type) (*Flow, error) {
Expand Down Expand Up @@ -233,17 +232,23 @@ type getSelfServiceLoginFlow struct {
// 404: genericError
// 410: genericError
// 500: genericError
func (h *Handler) fetchLoginFlow(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
func (h *Handler) fetchFlow(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
ar, err := h.d.LoginFlowPersister().GetLoginFlow(r.Context(), x.ParseUUID(r.URL.Query().Get("id")))
if err != nil {
h.d.Writer().WriteError(w, r, err)
return
}

if ar.ExpiresAt.Before(time.Now()) {
if ar.Type == flow.TypeBrowser {
h.d.Writer().WriteError(w, r, errors.WithStack(x.ErrGone.
WithReason("The login flow has expired. Redirect the user to the login flow init endpoint to initialize a new login flow.").
WithDetail("redirect_to", urlx.AppendPaths(h.c.SelfPublicURL(), RouteInitBrowserFlow).String())))
return
}
h.d.Writer().WriteError(w, r, errors.WithStack(x.ErrGone.
WithReason("The login flow has expired. Redirect the user to the login flow init endpoint to initialize a new login flow.").
WithDetail("redirect_to", urlx.AppendPaths(h.c.SelfPublicURL(), RouteInitBrowserFlow).String())))
WithReason("The login flow has expired. Call the login flow init API endpoint to initialize a new login flow.").
WithDetail("api", urlx.AppendPaths(h.c.SelfPublicURL(), RouteInitAPIFlow).String())))
return
}

Expand Down
15 changes: 10 additions & 5 deletions selfservice/flow/registration/handler.go
Expand Up @@ -5,9 +5,8 @@ import (
"time"

"github.com/julienschmidt/httprouter"
"github.com/pkg/errors"

"github.com/ory/x/urlx"
"github.com/pkg/errors"

"github.com/ory/kratos/driver/configuration"
"github.com/ory/kratos/selfservice/errorx"
Expand Down Expand Up @@ -113,7 +112,7 @@ func (h *Handler) NewRegistrationFlow(w http.ResponseWriter, r *http.Request, ft
func (h *Handler) initApiFlow(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
a, err := h.NewRegistrationFlow(w, r, flow.TypeAPI)
if err != nil {
h.d.SelfServiceErrorManager().Forward(r.Context(), w, r, err)
h.d.Writer().WriteError(w, r, err)
return
}

Expand Down Expand Up @@ -199,9 +198,15 @@ func (h *Handler) fetchFlow(w http.ResponseWriter, r *http.Request, ps httproute
}

if ar.ExpiresAt.Before(time.Now()) {
if ar.Type == flow.TypeBrowser {
h.d.Writer().WriteError(w, r, errors.WithStack(x.ErrGone.
WithReason("The registration flow has expired. Redirect the user to the registration flow init endpoint to initialize a new registration flow.").
WithDetail("redirect_to", urlx.AppendPaths(h.c.SelfPublicURL(), RouteInitBrowserFlow).String())))
return
}
h.d.Writer().WriteError(w, r, errors.WithStack(x.ErrGone.
WithReason("The registration flow has expired. Redirect the user to the registration flow init endpoint to initialize a new registration flow.").
WithDetail("redirect_to", urlx.AppendPaths(h.c.SelfPublicURL(), RouteInitBrowserFlow).String())))
WithReason("The registration flow has expired. Call the registration flow init API endpoint to initialize a new registration flow.").
WithDetail("api", urlx.AppendPaths(h.c.SelfPublicURL(), RouteInitAPIFlow).String())))
return
}

Expand Down

0 comments on commit 124a92e

Please sign in to comment.