<titledata-rh="true">Session Storage | OAuth2 Proxy</title><metadata-rh="true"name="viewport"content="width=device-width,initial-scale=1"><metadata-rh="true"name="twitter:card"content="summary_large_image"><metadata-rh="true"property="og:url"content="https://oauth2-proxy.github.io/oauth2-proxy/docs/configuration/session_storage"><metadata-rh="true"name="docusaurus_locale"content="en"><metadata-rh="true"name="docsearch:language"content="en"><metadata-rh="true"name="docusaurus_version"content="7.5.x"><metadata-rh="true"name="docusaurus_tag"content="docs-default-7.5.x"><metadata-rh="true"name="docsearch:version"content="7.5.x"><metadata-rh="true"name="docsearch:docusaurus_tag"content="docs-default-7.5.x"><metadata-rh="true"property="og:title"content="Session Storage | OAuth2 Proxy"><metadata-rh="true"name="description"content="Sessions allow a user's authentication to be tracked between multiple HTTP"><metadata-rh="true"property="og:description"content="Sessions allow a user's authentication to be tracked between multiple HTTP"><linkdata-rh="true"rel="icon"href="/oauth2-proxy/img/logos/OAuth2_Proxy_icon.svg"><linkdata-rh="true"rel="canonical"href="https://oauth2-proxy.github.io/oauth2-proxy/docs/configuration/session_storage"><linkdata-rh="true"rel="alternate"href="https://oauth2-proxy.github.io/oauth2-proxy/docs/configuration/session_storage"hreflang="en"><linkdata-rh="true"rel="alternate"href="https://oauth2-proxy.github.io/oauth2-proxy/docs/configuration/session_storage"hreflang="x-default"><linkrel="stylesheet"href="/oauth2-proxy/assets/css/styles.5c990d8a.css">
data in one of the available session storage backends.</p><p>At present the available backends are (as passed to <code>--session-store-type</code>):</p><ul><li><ahref="#cookie-storage">cookie</a> (default)</li><li><ahref="#redis-storage">redis</a></li></ul><h3class="anchor anchorWithStickyNavbar_LWe7"id="cookie-storage">Cookie Storage<ahref="#cookie-storage"class="hash-link"aria-label="Direct link to Cookie Storage"title="Direct link to Cookie Storage"></a></h3><p>The Cookie storage backend is the default backend implementation and has
been used in the OAuth2 Proxy historically.</p><p>With the Cookie storage backend, all session information is stored in client
side cookies and transferred with each and every request.</p><p>The following should be known when using this implementation:</p><ul><li>Since all state is stored client side, this storage backend means that the OAuth2 Proxy is completely stateless</li><li>Cookies are signed server side to prevent modification client-side</li><li>It is mandatory to set a <code>cookie-secret</code> which will ensure data is encrypted within the cookie data.</li><li>Since multiple requests can be made concurrently to the OAuth2 Proxy, this session implementation
cannot lock sessions and while updating and refreshing sessions, there can be conflicts which force
users to re-authenticate</li></ul><h3class="anchor anchorWithStickyNavbar_LWe7"id="redis-storage">Redis Storage<ahref="#redis-storage"class="hash-link"aria-label="Direct link to Redis Storage"title="Direct link to Redis Storage"></a></h3><p>The Redis Storage backend stores sessions, encrypted, in redis. Instead sending all the information
to the user as the cookie value instead.</p><p>A ticket is composed as the following:</p><p><code>{CookieName}-{ticketID}.{secret}</code></p><p>Where:</p><ul><li>The <code>CookieName</code> is the OAuth2 cookie name (_oauth2_proxy by default)</li><li>The <code>ticketID</code> is a 128 bit random number, hex-encoded</li><li>The <code>secret</code> is a 128 bit random number, base64url encoded (no padding). The secret is unique for every session.</li><li>The pair of <code>{CookieName}-{ticketID}</code> comprises a ticket handle, and thus, the redis key
to which the session is stored. The encoded session is encrypted with the secret and stored
in redis via the <code>SETEX</code> command.</li></ul><p>Encrypting every session uniquely protects the refresh/access/id tokens stored in the session from
disclosure.</p><p>Additionally the browser only has to send a short Cookie with every request and not the whole JWT, which can get quite big.</p><p>Two settings are used to configure the OAuth2 Proxy cookie lifetime:</p><divclass="codeBlockContainer_Ckt0 theme-code-block"style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><divclass="codeBlockContent_biex"><pretabindex="0"class="prism-code language-text codeBlock_bY9V thin-scrollbar"><codeclass="codeBlockLines_e6Vv"><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">--cookie-refresh duration refresh the cookie after this duration; 0 to disable</span><br></span><spanclass="token-line"style="color:#bfc7d5"><spanclass="token plain">--cookie-expire duration expire timeframe for cookie 168h0m0s</span><br></span></code></pre><divclass="buttonGroup__atx"><buttontype="button"aria-label="Copy code to clipboard"title="Copy"class="clean-btn"><spanclass="copyButtonIcons_eSgA"aria-hidden="true"><svgviewBox="0 0 24 24"class="copyButtonIcon_y97N"><pathfill="currentColor"d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svgviewBox="0 0 24 24"class="copyButtonSuccessIcon_LjdS"><pathfill="currentColor"d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p>The "cookie-expire" value should be equal to the lifetime of the Refresh-Token that is issued by the OAuth2 authorization server.
If it expires earlier and is deleted by the browser, OAuth2 Proxy cannot find the stored Refresh-Tokens in Redis and thus cannot start
the refresh flow to get new Access-Tokens. If it is longer, it might be that the old Refresh-Token will be found in Redis but has already
expired.</p><p>The "cookie-refresh" value controls when OAuth2 Proxy tries to refresh an Access-Token. If it is set to "0", the
Access-Token will never be refreshed, even it is already expired and there would be a valid Refresh-Token in the
available. If set, OAuth2 Proxy will refresh the Access-Token after this many seconds even if it is still valid.
Of course, it will also be refreshed after it has expired, as long as a Refresh Token is available.</p><p>Caveat: It can happen that the Access-Token is valid for e.g. "1m" and a request happens after exactly "59s".
It would pass OAuth2 Proxy and be forwarded to the backend but is just expired when the backend tries to validate
it. This is especially relevant if the backend uses the JWT to make requests to other backends.
For this reason, it's advised to set the cookie-refresh a couple of seconds less than the Access-Token lifespan.</p><p>Recommended settings:</p><ul><li>cookie<!---->_<!---->refresh := Access-Token lifespan - 1m</li><li>cookie<!---->_<!---->expire := Refresh-Token lifespan (i.e. Keycloak's client<!---->_<!---->session<!---->_<!---->idle)</li></ul><h4class="anchor anchorWithStickyNavbar_LWe7"id="usage">Usage<ahref="#usage"class="hash-link"aria-label="Direct link to Usage"title="Direct link to Usage"></a></h4><p>When using the redis store, specify <code>--session-store-type=redis</code> as well as the Redis connection URL, via
<code>--redis-connection-url=redis://host[:port][/db-number]</code>.</p><p>You may also configure the store for Redis Sentinel. In this case, you will want to use the
<code>--redis-use-sentinel=true</code> flag, as well as configure the flags <code>--redis-sentinel-master-name</code>
and <code>--redis-sentinel-connection-urls</code> appropriately.</p><p>Redis Cluster is available to be the backend store as well. To leverage it, you will need to set the
<code>--redis-use-cluster=true</code> flag, and configure the flags <code>--redis-cluster-connection-urls</code> appropriately.</p><p>Note that flags <code>--redis-use-sentinel=true</code> and <code>--redis-use-cluster=true</code> are mutually exclusive.</p><p>Note, if Redis timeout option is set to non-zero, the <code>--redis-connection-idle-timeout</code>
must be less than <ahref="https://redis.io/docs/reference/clients/#client-timeouts"target="_blank"rel="noopener noreferrer">Redis timeout option</a>. For example: if either redis.conf includes