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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
AuthenticationTokenManager.Token
An Ametys authentication token
-
Field Summary
Fields Modifier and Type Field Description private CurrentUserProvider
_currentUserProvider
private String
_datasourceId
private JSONUtils
_jsonUtils
private ServiceManager
_manager
private SQLDatabaseTypeExtensionPoint
_sqlDatabaseTypeExtensionPoint
static String
ROLE
The avalon rolestatic String
TOKEN_SEPARATOR
The separator in tokenprivate static String
TOKEN_SQL_GET_FIELDS
all fields without login and population_idprivate static String
TOKEN_SQL_SET_FIELDS
all fields without id and last_update_datestatic String
USER_TOKEN_TYPE
The user token type
-
Constructor Summary
Constructors Constructor Description AuthenticationTokenManager()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private String
_contextsToString(Set<String> contexts)
private Set<String>
_convertStringToContexts(String contexts)
private void
_deleteOldTokens(Connection connection)
Generates the sql statement that deletes the entries of the users token database that are oldprivate void
_deleteUserToken(Connection connection, Integer id)
Deletes the database entry that has this tokenprivate void
_generateToken(UserIdentity user, long duration, boolean autoRenewDuration, Integer nbUsesLeft, Set<String> contexts, String type, String comment, String hashedTokenAndSalt, String salt, Timestamp creationDateTime, Timestamp endTime)
private CurrentUserProvider
_getCurrentUserProvider()
private PreparedStatement
_getSelectUserTokenStatement(Connection connection, String login, String populationId, String type)
Generates the statement that selects the users having the specified login in the Authentication_Token tableprivate AuthenticationTokenManager.Token
_getTokenFromResultSet(ResultSet resultSet, Connection connection)
private void
_updateUserToken(Connection connection, AuthenticationTokenManager.Token token)
Update the last update date in the databaseprivate UserIdentity
_validateToken(String encodedToken, String context, boolean forceRemove)
void
deleteAuthenticationToken(List<Integer> ids)
Delete one or multiples authentication tokenvoid
deleteTokenById(Integer tokenId)
Destroy the given tokenvoid
deleteTokenByValue(String token, String context)
Destroy the given tokenString
generateAuthenticationToken(Map<String,Object> parameters)
Generate a new authentication tokenString
generateToken(long duration, String type, String comment)
Generates a new token for the current userString
generateToken(UserIdentity user, long duration, boolean autoRenewDuration, Integer nbUsesLeft, Set<String> contexts, String type, String comment)
Generates a new tokenString
generateToken(UserIdentity user, long duration, Integer nbUsesLeft, String type, String comment)
Generates a new tokenString
generateToken(UserIdentity user, long duration, String type, String comment)
Generates a new tokenList<AuthenticationTokenManager.Token>
getTokens(String type)
Get the existing tokens for the connected userList<AuthenticationTokenManager.Token>
getTokens(UserIdentity user, String type)
Get the existing tokens for this uservoid
initialize()
void
service(ServiceManager manager)
UserIdentity
validateToken(String token)
Check if a token is valid and return the userUserIdentity
validateToken(String token, String context)
Check if a token is valid and return the user-
Methods inherited from class org.ametys.runtime.plugin.component.AbstractLogEnabled
getLogger, setLogger
-
-
-
-
Field Detail
-
TOKEN_SEPARATOR
public static final String TOKEN_SEPARATOR
The separator in token- See Also:
- Constant Field Values
-
USER_TOKEN_TYPE
public static final String USER_TOKEN_TYPE
The user token type- See Also:
- Constant Field Values
-
TOKEN_SQL_GET_FIELDS
private static final String TOKEN_SQL_GET_FIELDS
all fields without login and population_id- See Also:
- Constant Field Values
-
TOKEN_SQL_SET_FIELDS
private static final String TOKEN_SQL_SET_FIELDS
all fields without id and last_update_date- See Also:
- Constant Field Values
-
_manager
private ServiceManager _manager
-
_currentUserProvider
private CurrentUserProvider _currentUserProvider
-
_datasourceId
private String _datasourceId
-
_sqlDatabaseTypeExtensionPoint
private SQLDatabaseTypeExtensionPoint _sqlDatabaseTypeExtensionPoint
-
_jsonUtils
private JSONUtils _jsonUtils
-
-
Constructor Detail
-
AuthenticationTokenManager
public AuthenticationTokenManager()
-
-
Method Detail
-
service
public void service(ServiceManager manager) throws ServiceException
- Specified by:
service
in interfaceServiceable
- Throws:
ServiceException
-
initialize
public void initialize() throws Exception
- Specified by:
initialize
in interfaceInitializable
- Throws:
Exception
-
_getCurrentUserProvider
private CurrentUserProvider _getCurrentUserProvider() throws RuntimeException
- Throws:
RuntimeException
-
getTokens
public List<AuthenticationTokenManager.Token> getTokens(String type) throws RuntimeException
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:
type
- The type of tokens to return. null to return all.user
- The user. Cannot be null- Returns:
- The tokens identifier and associated comment
- Throws:
RuntimeException
- If the user is null or if there is a database error
-
generateToken
public String generateToken(long duration, String type, String comment) throws RuntimeException
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
-
_generateToken
private void _generateToken(UserIdentity user, long duration, boolean autoRenewDuration, Integer nbUsesLeft, Set<String> contexts, String type, String comment, String hashedTokenAndSalt, String salt, Timestamp creationDateTime, Timestamp endTime) throws RuntimeException
- Throws:
RuntimeException
-
_contextsToString
private String _contextsToString(Set<String> contexts)
-
_convertStringToContexts
private Set<String> _convertStringToContexts(String contexts)
-
_validateToken
private UserIdentity _validateToken(String encodedToken, String context, boolean forceRemove)
-
validateToken
public UserIdentity validateToken(String token)
Check if a token is valid and return the user- Parameters:
token
- The token to validate- Returns:
- The user associated to the valid token, null otherwise
-
validateToken
public UserIdentity validateToken(String token, String context)
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
-
deleteTokenByValue
public void deleteTokenByValue(String token, String context)
Destroy the given token- Parameters:
token
- The token to removecontext
- context of the token (null for no context)
-
deleteTokenById
public void deleteTokenById(Integer tokenId)
Destroy the given token- Parameters:
tokenId
- The token identifier to remove
-
_deleteOldTokens
private void _deleteOldTokens(Connection connection) throws SQLException
Generates the sql statement that deletes the entries of the users token database that are old- Parameters:
connection
- the database's session- Throws:
SQLException
- if a sql exception occurs
-
_getSelectUserTokenStatement
private PreparedStatement _getSelectUserTokenStatement(Connection connection, String login, String populationId, String type) throws SQLException
Generates the statement that selects the users having the specified login in the Authentication_Token table- Parameters:
connection
- the database's sessionlogin
- The login of the userpopulationId
- The populationId of the usertype
- The type to filter or null to get all- Returns:
- the retrieve statement
- Throws:
SQLException
- if a sql exception occurs
-
_getTokenFromResultSet
private AuthenticationTokenManager.Token _getTokenFromResultSet(ResultSet resultSet, Connection connection) throws SQLException, IOException
- Throws:
SQLException
IOException
-
_deleteUserToken
private void _deleteUserToken(Connection connection, Integer id) throws SQLException
Deletes the database entry that has this token- Parameters:
connection
- the database's sessionid
- the token id- Throws:
SQLException
- if an error occurred
-
_updateUserToken
private void _updateUserToken(Connection connection, AuthenticationTokenManager.Token token) throws SQLException
Update the last update date in the database- Parameters:
connection
- the database's sessiontoken
- the token- Throws:
SQLException
- if an error occurred
-
generateAuthenticationToken
public String generateAuthenticationToken(Map<String,Object> parameters)
Generate a new authentication token- Parameters:
parameters
- a map of the following parameters for the authentication token : description- Returns:
- The generated token
-
deleteAuthenticationToken
public void deleteAuthenticationToken(List<Integer> ids)
Delete one or multiples authentication token- Parameters:
ids
- a list of authentication token ids
-
-