Class StoreBackedRememberMeService
java.lang.Object
com.svenruppert.jsentinel.authentication.StoreBackedRememberMeService
"Remember me" / persistent-login service built on top of a
RememberMeTokenStore and a PasswordHasher.
The plain token is generated server-side at issue(SubjectId, Duration) time,
returned to the caller exactly once (intended to ship in an
HttpOnly; Secure cookie or equivalent), and immediately
dropped — only the hash reaches the store. validate(String)
hashes the candidate the same way and queries the store by hash;
an attacker who exfiltrates the store cannot impersonate the
subject without the cookie value.
Bound to one TenantId at construction. Multi-tenant
deployments instantiate one service per tenant.
Token entropy: 256 bits from a SecureRandom, encoded
as URL-safe base64 (no padding). Override via the
tokenSource constructor argument if a different format
is required by the carrier (cookie, header, query parameter).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordTuple returned fromissue(SubjectId, Duration): the plain token (caller passes it to the client carrier and drops it) and the persisted record (handy for tests / audit hooks). -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault token entropy in bytes (256 bits). -
Constructor Summary
ConstructorsConstructorDescriptionStoreBackedRememberMeService(RememberMeTokenStore store, PasswordHasher hasher) Convenience constructor: binds toTenantId.DEFAULT, uses a systemClockand the default 256-bit token source.StoreBackedRememberMeService(RememberMeTokenStore store, PasswordHasher hasher, TenantId tenant, Clock clock, Supplier<String> tokenSource) Full constructor. -
Method Summary
Modifier and TypeMethodDescriptionIssues a new token forsubjectIdwith the given TTL.intPurges every expired token in the backing store.booleanRevokes the supplied plain token.intRevokes every token issued tosubjectIdin this service's tenant.Validates the supplied plain token.
-
Field Details
-
DEFAULT_TOKEN_BYTES
public static final int DEFAULT_TOKEN_BYTESDefault token entropy in bytes (256 bits).- See Also:
-
-
Constructor Details
-
StoreBackedRememberMeService
Convenience constructor: binds toTenantId.DEFAULT, uses a systemClockand the default 256-bit token source.- Parameters:
store- backing token store; non-nullhasher- password hasher used to hash tokens before persisting them; non-null
-
StoreBackedRememberMeService
public StoreBackedRememberMeService(RememberMeTokenStore store, PasswordHasher hasher, TenantId tenant, Clock clock, Supplier<String> tokenSource) Full constructor.- Parameters:
store- backing token store; non-nullhasher- password hasher used to hash tokens before persisting them; non-nulltenant- tenant scope;nullbecomesTenantId.DEFAULTclock- time source; non-nulltokenSource- supplier producing the plain token strings issued to clients; non-null and must return non-blank values
-
-
Method Details
-
issue
Issues a new token forsubjectIdwith the given TTL. The plain token is returned exactly once — callers ship it to the client (cookie / header) and discard it.- Parameters:
subjectId- subject the token authenticates; non-nullttl- lifetime; must be strictly positive- Returns:
- issued token (plain value + record metadata)
-
validate
Validates the supplied plain token. Returns the matching record only when the hash is known, the record is in the configured tenant, and the token has not expired. Expired matches are removed from the store as a side effect.- Parameters:
plainToken- plain token from the carrier; null/blank yieldsOptional.empty()- Returns:
- matching record, if valid
-
revoke
Revokes the supplied plain token. Idempotent — unknown tokens returnfalserather than throwing.- Parameters:
plainToken- plain token; null/blank returnsfalse- Returns:
truewhen a record was removed
-
revokeAll
Revokes every token issued tosubjectIdin this service's tenant.- Parameters:
subjectId- subject; non-null- Returns:
- number of tokens removed
-
purgeExpired
public int purgeExpired()Purges every expired token in the backing store. Spans all tenants — call once globally rather than per-tenant.- Returns:
- number of tokens purged
-