Skip to content

Commit 6e8579b

Browse files
committed
feat: improve cache handling
1 parent 71e2449 commit 6e8579b

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

session/handler.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"net/http"
66
"strconv"
7+
"time"
78

89
"github.com/ory/x/pointerx"
910

@@ -181,7 +182,7 @@ func (h *Handler) whoami(w http.ResponseWriter, r *http.Request, ps httprouter.P
181182
if err != nil {
182183
// We cache errors where no session was found.
183184
if noSess := new(ErrNoActiveSessionFound); errors.As(err, &noSess) && noSess.credentialsMissing {
184-
w.Header().Set("X-Ory-Cache-Until", "180")
185+
w.Header().Set("Ory-Session-Cache-For", fmt.Sprintf("%d", int64(time.Minute.Seconds())))
185186
}
186187

187188
h.r.Audit().WithRequest(r).WithError(err).Info("No valid session cookie found.")
@@ -206,7 +207,7 @@ func (h *Handler) whoami(w http.ResponseWriter, r *http.Request, ps httprouter.P
206207

207208
// Set userId as the X-Kratos-Authenticated-Identity-Id header.
208209
w.Header().Set("X-Kratos-Authenticated-Identity-Id", s.Identity.ID.String())
209-
w.Header().Set("X-Ory-Cache-Until", fmt.Sprintf("%d", s.ExpiresAt.Unix()))
210+
w.Header().Set("Ory-Session-Cache-For", fmt.Sprintf("%d", int64(s.ExpiresAt.Sub(time.Now()).Seconds())))
210211

211212
if err := h.r.SessionManager().RefreshCookie(r.Context(), w, r, s); err != nil {
212213
h.r.Audit().WithRequest(r).WithError(err).Info("Could not re-issue cookie.")

session/handler_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func TestSessionWhoAmI(t *testing.T) {
150150
res, err := client.Get(ts.URL + RouteWhoami)
151151
require.NoError(t, err)
152152
assertNoCSRFCookieInResponse(t, ts, client, res) // Test that no CSRF cookie is ever set here.
153-
assert.NotEmpty(t, res.Header.Get("X-Ory-Session-Expires-At"))
153+
assert.NotEmpty(t, res.Header.Get("Ory-Session-Cache-For"))
154154

155155
// Set cookie
156156
reg.CSRFHandler().IgnorePath("/set")
@@ -175,7 +175,7 @@ func TestSessionWhoAmI(t *testing.T) {
175175

176176
assert.EqualValues(t, http.StatusOK, res.StatusCode)
177177
assert.NotEmpty(t, res.Header.Get("X-Kratos-Authenticated-Identity-Id"))
178-
assert.NotEmpty(t, res.Header.Get("X-Ory-Session-Expires-At"))
178+
assert.NotEmpty(t, res.Header.Get("Ory-Session-Cache-For"))
179179

180180
assert.Empty(t, gjson.GetBytes(body, "identity.credentials"))
181181
assert.Equal(t, "mp", gjson.GetBytes(body, "identity.metadata_public.public").String(), "%s", body)

0 commit comments

Comments
 (0)