public class CmsPersistentLoginTokenHandler extends java.lang.Object
When a token is created for a user, a special additional info item is stored on the user, such that the token uniquely identifies that info item. The value of the info item is the expiration date of the token. A token is validated by looking up the additional info item for the user and checking whether the token is still valid according to the stored expiration date.
Modifier and Type | Class and Description |
---|---|
static class |
CmsPersistentLoginTokenHandler.Token
Bean representing the data encoded in a login token (user name and key).
|
Modifier and Type | Field and Description |
---|---|
static long |
DEFAULT_LIFETIME
Default token lifetime.
|
static java.lang.String |
KEY_PREFIX
Prefix used for the keys for the additional infos this class creates.
|
Constructor and Description |
---|
CmsPersistentLoginTokenHandler()
Creates a new instance.
|
Modifier and Type | Method and Description |
---|---|
java.lang.String |
createToken(CmsObject cms)
Generates a new login token for a given user and registers the token in the user's additional info.
|
void |
invalidateToken(CmsUser user,
java.lang.String token)
Invalidates all tokens for the given user.
|
void |
removeExpiredTokens(CmsUser user,
long now)
Removes expired tokens from the user's additional infos.
|
static void |
setAdminCms(CmsObject adminCms)
Static method used to give this class access to an admin cms context.
|
void |
setTokenLifetime(long duration)
Sets the token lifetime.
|
CmsUser |
validateToken(java.lang.String tokenString)
Validates a token and returns the matching user for which the token is valid.
|
public static final long DEFAULT_LIFETIME
public static final java.lang.String KEY_PREFIX
public CmsPersistentLoginTokenHandler()
public static void setAdminCms(CmsObject adminCms)
adminCms
- the admin cms context to setpublic java.lang.String createToken(CmsObject cms) throws CmsException
cms
- the CMS context for which to create a new tokenCmsException
- if something goes wrongpublic void invalidateToken(CmsUser user, java.lang.String token) throws CmsException
user
- the usertoken
- the token stringCmsException
- if something goes wrongpublic void removeExpiredTokens(CmsUser user, long now)
This method does not write the user back to the database.
user
- the user for which to remove the additional infosnow
- the current timepublic void setTokenLifetime(long duration)
duration
- the number of milliseconds for which the token should be validpublic CmsUser validateToken(java.lang.String tokenString)
Returns null if no user matching the token is found, or if the token for the user is expired
tokenString
- the token for which to find the matching user