Package net.shibboleth.idp.profile.support

# Class StorageAwareCookieManager

java.lang.Object
net.shibboleth.shared.component.AbstractInitializableComponent
net.shibboleth.shared.net.CookieManager
net.shibboleth.idp.profile.support.StorageAwareCookieManager

All Implemented Interfaces:
  `Component`, `DestructableComponent`, `InitializableComponent`

public class StorageAwareCookieManager extends CookieManager
An extended `CookieManager` that allows use of a `StorageService`.

Reads are backed up by a read into the storage service, while writes are passed through to it.

The cookie path and domain, and the username (as obtained by a lookup function) are used to construct the storage context and key to maintain the expected isolation. Notably, the function operates by obtaining the `ProfileRequestContext` from the bound request attribute to address the fact that the API does not directly include it.

This is NOT suitable for use cases in which consistency of data is critical, as there are few if any storage options (other than the client itself) that will provide sufficient reliability and locking to avoid problems. It's best used for advisory cookies whose absence does not create issues with security or expected behavior.

Since:
  5.1.0

## Nested Class Summary

## Nested classes/interfaces inherited from class net.shibboleth.shared.net.CookieManager

`CookieManager.SameSiteValue`

## Field Summary

Fields
Modifier and Type
Field
Description
`private org.slf4j.Logger`
`log`
Class logger.
`private String`
`storageContext`
Storage context based on fixed value and cookie attributes.
`private StorageService`
`storageService`
Optional storage service to backstop the cookie.
`private Function<ProfileRequestContext,String>`
`usernameLookupStrategy`
Lookup strategy for username.

## Constructor Summary

Constructors
Constructor
Description
`StorageAwareCookieManager()`

## Method Summary

Modifier and Type
Method
Description
`void`
`addCookie(String name, String value, String overridePath, int overrideMaxAge)`
`protected void`
`doInitialize()`
`String`
`getCookieValue(String name, String defValue)`
`protected String`
`getPartitionedCookieName(String cookieName)`
Get cookie name as partitioned in storage by username.
`String`
`getStorageContext()`
Get the storage context used to hold the cookies.
`void`
`setStorageService(StorageService ss)`
Sets the `StorageService` to read/write.
`void`
`setUsernameLookupStrategy(Function<ProfileRequestContext,String> strategy)`
Sets the lookup strategy to obtain the username for cookie partitioning.
`void`
`unsetCookie(String name, String overridePath)`

### Methods inherited from class net.shibboleth.shared.net.CookieManager

`addCookie, addCookie, cookieHasValue, getCookieAttributes, getCookieDomain, getCookieLimit, getCookiePath, getHttpServletRequest, getHttpServletResponse, getMaxAge, getSameSite, getSameSiteCondition, isHttpOnly, isSecure, purgeStaleCookies, purgeStaleCookies, setCookieAttributes, setCookieDomain, setCookieLimit, setCookiePath, setGuardSetAttribute, setHttpOnly, setHttpServletRequestSupplier, setHttpServletResponseSupplier, setMaxAge, setMaxAgeDuration, setSameSite, setSameSiteCondition, setSameSiteCookies, setSecure, unsetCookie`

### Methods inherited from class net.shibboleth.shared.component.AbstractInitializableComponent

`checkComponentActive, checkSetterPreconditions, destroy, doDestroy, ifDestroyedThrowDestroyedComponentException, ifInitializedThrowUnmodifiabledComponentException, ifNotInitializedThrowUninitializedComponentException, initialize, isDestroyed, isInitialized`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### log

@Nonnull private org.slf4j.Logger log
Class logger.

### storageService

@Nullable private StorageService storageService
Optional storage service to backstop the cookie.

### usernameLookupStrategy

@Nullable private Function<ProfileRequestContext,String> usernameLookupStrategy
Lookup strategy for username.

### storageContext

@NonnullAfterInit private String storageContext
Storage context based on fixed value and cookie attributes.

## Constructor Details

### StorageAwareCookieManager

public StorageAwareCookieManager()

## Method Details

### setStorageService

public void setStorageService(@Nullable StorageService ss)
Sets the `StorageService` to read/write.

Parameters:
  `ss` - storage service

### getStorageContext

@NonnullAfterInit public String getStorageContext()
Get the storage context used to hold the cookies.

Returns:
  storage context

### setUsernameLookupStrategy

public void setUsernameLookupStrategy(@Nullable Function<ProfileRequestContext,String> strategy)
Sets the lookup strategy to obtain the username for cookie partitioning.

Parameters:
  `strategy` - lookup strategy

### doInitialize

protected void doInitialize() throws ComponentInitializationException

Overrides:
  `doInitialize` in class `CookieManager`
Throws:
  `ComponentInitializationException`

### addCookie

public void addCookie(@Nonnull String name, @Nonnull String value, @Nullable @NotEmpty String overridePath, int overrideMaxAge)

Overrides:
  `addCookie` in class `CookieManager`

### unsetCookie

public void unsetCookie(@Nonnull String name, @Nullable @NotEmpty String overridePath)

Overrides:
  `unsetCookie` in class `CookieManager`

### getCookieValue

@Nullable public String getCookieValue(@Nonnull String name, @Nullable String defValue)

Overrides:
  `getCookieValue` in class `CookieManager`

### getPartitionedCookieName

@Nonnull protected String getPartitionedCookieName(@Nonnull String cookieName) throws IOException
Get cookie name as partitioned in storage by username.

Parameters:
  `cookieName` - base cookie name
Returns:
  the storage key for the cookie
Throws:
  `IOException` - if unable to locate username for key