Class AuthenticationTokenManager
java.lang.Object
org.ametys.runtime.plugin.component.AbstractLogEnabled
org.ametys.core.authentication.token.AuthenticationTokenManager
- All Implemented Interfaces:
LogEnabled,Initializable,Component,Serviceable
public class AuthenticationTokenManager
extends AbstractLogEnabled
implements Component, Serviceable, Initializable
The component to handle temporary authentication token.
Token can only be used once and are available for a short time only.
Token can only be used once and are available for a short time only.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAn Ametys authentication token -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidDelete one or multiples authentication tokenvoiddeleteTokenById(Integer tokenId) Destroy the given tokenvoiddeleteTokenByValue(String token, String context) Destroy the given tokenvoiddeleteTokenByValue(String token, String context, boolean acceptTokenWithUnrestrictedContext) Destroy the given tokengenerateAuthenticationToken(Map<String, Object> parameters) Generate a new authentication tokengenerateToken(long duration, String type, String comment) Generates a new token for the current usergenerateToken(UserIdentity user, long duration, boolean autoRenewDuration, Integer nbUsesLeft, Set<String> contexts, String type, String comment) Generates a new tokengenerateToken(UserIdentity user, long duration, Integer nbUsesLeft, String type, String comment) Generates a new tokengenerateToken(UserIdentity user, long duration, String type, String comment) Generates a new tokenGet the existing tokens for the connected usergetTokens(UserIdentity user, String type) Get the existing tokens for this userGet the existing tokens for the connected uservoidvoidservice(ServiceManager manager) validateToken(String token, String context) Check if a token is valid and return the uservalidateToken(String token, String context, boolean acceptTokenWithUnrestrictedContext) Check if a token is valid and return the userMethods inherited from class org.ametys.runtime.plugin.component.AbstractLogEnabled
getLogger, setLogger
-
Field Details
-
ROLE
The avalon role -
TOKEN_SEPARATOR
The separator in token- See Also:
-
USER_TOKEN_TYPE
The user token type- See Also:
-
-
Constructor Details
-
AuthenticationTokenManager
public AuthenticationTokenManager()
-
-
Method Details
-
service
- Specified by:
servicein interfaceServiceable- Throws:
ServiceException
-
initialize
- Specified by:
initializein interfaceInitializable- Throws:
Exception
-
getUserTokens
Get the existing tokens for the connected user- Returns:
- The tokens as JSON
- Throws:
RuntimeException- If there is no user connected or if there is a database error
-
getTokens
Get the existing tokens for the connected user- Parameters:
type- The type of tokens to return. null to return all.- Returns:
- The tokens
- Throws:
RuntimeException- If there is no user connected or if there is a database error
-
getTokens
public List<AuthenticationTokenManager.Token> getTokens(UserIdentity user, String type) throws RuntimeException Get the existing tokens for this user- Parameters:
user- The user. Cannot be nulltype- The type of tokens to return. null to return all.- Returns:
- The tokens identifier and associated comment
- Throws:
RuntimeException- If the user is null or if there is a database error
-
generateToken
Generates a new token for the current user- Parameters:
duration- The time the token is valid in seconds. 0 means for ever and moreover the ticket will be reusable.type- The type of token. Mandatory but can be anything you want between 1 to 32 characters. Such as "Cookie".comment- An optional token comment to remember the reason of its creation- Returns:
- The token
- Throws:
RuntimeException- If the user is not authenticated, or if there is a database error
-
generateToken
public String generateToken(UserIdentity user, long duration, String type, String comment) throws RuntimeException Generates a new token- Parameters:
user- The user that will be authenticated with the tokenduration- The time the token is valid in seconds. 0 means for ever and moreover the ticket will be reusabletype- The type of token. Mandatory but can be anything you want between 1 to 32 characters. Such as "Cookie".comment- An optional token comment to remember the reason of its creation- Returns:
- The token
- Throws:
RuntimeException- If the user is null or if there is a database error or if duration is negative
-
generateToken
public String generateToken(UserIdentity user, long duration, Integer nbUsesLeft, String type, String comment) throws RuntimeException Generates a new token- Parameters:
user- The user that will be authenticated with the tokenduration- The time the token is valid in seconds. 0 means for ever and moreover the ticket will be reusablenbUsesLeft- number of available uses (null for no limit)type- The type of token. Mandatory but can be anything you want between 1 to 32 characters. Such as "Cookie".comment- An optional token comment to remember the reason of its creation- Returns:
- The token
- Throws:
RuntimeException- If the user is null or if there is a database error or if duration is negative
-
generateToken
public String generateToken(UserIdentity user, long duration, boolean autoRenewDuration, Integer nbUsesLeft, Set<String> contexts, String type, String comment) throws RuntimeException Generates a new token- Parameters:
user- The user that will be authenticated with the tokenduration- The time the token is valid in seconds. 0 means for ever and moreover the ticket will be reusableautoRenewDuration- true to automatically renew token if used before it's expirationnbUsesLeft- number of available uses (null for no limit)contexts- contexts where the token can be usedtype- The type of token. Mandatory but can be anything you want between 1 to 32 characters. Such as "Cookie".comment- An optional token comment to remember the reason of its creation- Returns:
- The token
- Throws:
RuntimeException- If the user is null or if there is a database error or if duration is negative
-
validateToken
Check if a token is valid and return the user- Parameters:
token- The token to validatecontext- context to validate the token with- Returns:
- The user associated to the valid token, null otherwise
-
validateToken
public UserIdentity validateToken(String token, String context, boolean acceptTokenWithUnrestrictedContext) Check if a token is valid and return the user- Parameters:
token- The token to validatecontext- The non null nor empty context to validate the token withacceptTokenWithUnrestrictedContext- true if a token with no specific context should be considered valid, false otherwise- Returns:
- The user associated to the valid token, null otherwise
-
deleteTokenByValue
Destroy the given token- Parameters:
token- The token to removecontext- context of the token (null for no context)
-
deleteTokenByValue
public void deleteTokenByValue(String token, String context, boolean acceptTokenWithUnrestrictedContext) Destroy the given token- Parameters:
token- The token to removecontext- context of the token (null for no context)acceptTokenWithUnrestrictedContext- true if a token with no specific context should be considered valid, false otherwise
-
deleteTokenById
Destroy the given token- Parameters:
tokenId- The token identifier to remove
-
generateAuthenticationToken
Generate a new authentication token- Parameters:
parameters- a map of the following parameters for the authentication token : description- Returns:
- The generated token
-
deleteAuthenticationToken
Delete one or multiples authentication token- Parameters:
ids- a list of authentication token ids
-