Skip to content

Commit

Permalink
Differentiate between missing sessions and internal server errors
Browse files Browse the repository at this point in the history
  • Loading branch information
osbornk committed Jun 28, 2024
1 parent 7df3d56 commit 8cd8d17
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions session/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,20 @@ func (h *Handler) whoami(w http.ResponseWriter, r *http.Request, _ httprouter.Pa
s, err := h.r.SessionManager().FetchFromRequest(ctx, r)
c := h.r.Config()
if err != nil {
h.r.Audit().WithRequest(r).WithError(err).Info("No valid session found.")

// We cache errors (and set cache header only when configured) where no session was found.
if noSess := new(ErrNoActiveSessionFound); c.SessionWhoAmICaching(ctx) && errors.As(err, &noSess) && noSess.credentialsMissing {
w.Header().Set("Ory-Session-Cache-For", fmt.Sprintf("%d", int64(time.Minute.Seconds())))
if noSess := new(ErrNoActiveSessionFound); errors.As(err, &noSess) {
if c.SessionWhoAmICaching(ctx) && noSess.credentialsMissing {
w.Header().Set("Ory-Session-Cache-For", fmt.Sprintf("%d", int64(time.Minute.Seconds())))
}

h.r.Writer().WriteError(w, r, ErrNoSessionFound.WithWrap(err))
return

}

h.r.Audit().WithRequest(r).WithError(err).Info("No valid session found.")
h.r.Writer().WriteError(w, r, ErrNoSessionFound.WithWrap(err))
h.r.Writer().WriteError(w, r, herodot.ErrInternalServerError.WithReasonf("Unable to validate session.").WithWrap(err))
return
}

Expand Down

0 comments on commit 8cd8d17

Please sign in to comment.