public class AuthenticationTokenManager extends AbstractLogEnabled implements Component, Serviceable, Initializable
Modifier and Type | Class and Description |
---|---|
static class |
AuthenticationTokenManager.Token
An Ametys authentication token
|
Modifier and Type | Field and Description |
---|---|
private CurrentUserProvider |
_currentUserProvider |
private String |
_datasourceId |
private ServiceManager |
_manager |
private SQLDatabaseTypeExtensionPoint |
_sqlDatabaseTypeExtensionPoint |
static String |
ROLE
The avalon role
|
static String |
TOKEN_SEPARATOR
The separator in token
|
private static String |
TOKEN_SQL_GET_FIELDS
all fields without login and population_id
|
private static String |
TOKEN_SQL_SET_FIELDS
all fields without id and last_update_date
|
static String |
USER_TOKEN_TYPE
The user token type
|
Constructor and Description |
---|
AuthenticationTokenManager() |
Modifier and Type | Method and Description |
---|---|
private void |
_deleteOldTokens(Connection connection)
Generates the sql statement that deletes the entries of the users token
database that are old
|
private void |
_deleteUserToken(Connection connection,
Integer id)
Deletes the database entry that has this token
|
private void |
_generateToken(UserIdentity user,
long duration,
boolean autoRenewDuration,
Integer nbUsesLeft,
String context,
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 table
|
private AuthenticationTokenManager.Token |
_getTokenFromResultSet(ResultSet resultSet,
Connection connection) |
private void |
_updateUserToken(Connection connection,
AuthenticationTokenManager.Token token)
Update the last update date in the database
|
private UserIdentity |
_validateToken(String encodedToken,
String context,
boolean forceRemove) |
void |
deleteAuthenticationToken(List<Integer> ids)
Delete one or multiples authentication token
|
void |
deleteTokenById(Integer tokenId)
Destroy the given token
|
void |
deleteTokenByValue(String token,
String context)
Destroy the given token
|
String |
generateAuthenticationToken(Map<String,Object> parameters)
Generate a new authentication token
|
String |
generateToken(long duration,
String type,
String comment)
Generates a new token for the current user
|
String |
generateToken(UserIdentity user,
long duration,
boolean autoRenewDuration,
Integer nbUsesLeft,
String context,
String type,
String comment)
Generates a new token
|
String |
generateToken(UserIdentity user,
long duration,
Integer nbUsesLeft,
String type,
String comment)
Generates a new token
|
String |
generateToken(UserIdentity user,
long duration,
String type,
String comment)
Generates a new token
|
List<AuthenticationTokenManager.Token> |
getTokens(String type)
Get the existing tokens for the connected user
|
List<AuthenticationTokenManager.Token> |
getTokens(UserIdentity user,
String type)
Get the existing tokens for this user
|
void |
initialize() |
void |
service(ServiceManager manager) |
UserIdentity |
validateToken(String token)
Check if a token is valid and return the user
|
UserIdentity |
validateToken(String token,
String context)
Check if a token is valid and return the user
|
getLogger, setLogger
public static final String TOKEN_SEPARATOR
public static final String USER_TOKEN_TYPE
private static final String TOKEN_SQL_GET_FIELDS
private static final String TOKEN_SQL_SET_FIELDS
private ServiceManager _manager
private CurrentUserProvider _currentUserProvider
private String _datasourceId
private SQLDatabaseTypeExtensionPoint _sqlDatabaseTypeExtensionPoint
public AuthenticationTokenManager()
public void service(ServiceManager manager) throws ServiceException
service
in interface Serviceable
ServiceException
public void initialize() throws Exception
initialize
in interface Initializable
Exception
private CurrentUserProvider _getCurrentUserProvider() throws RuntimeException
RuntimeException
public List<AuthenticationTokenManager.Token> getTokens(String type) throws RuntimeException
type
- The type of tokens to return. null to return all.RuntimeException
- If there is no user connected or if there is a
database errorpublic List<AuthenticationTokenManager.Token> getTokens(UserIdentity user, String type) throws RuntimeException
type
- The type of tokens to return. null to return all.user
- The user. Cannot be nullRuntimeException
- If the user is null or if there is a database
errorpublic String generateToken(long duration, String type, String comment) throws RuntimeException
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
creationRuntimeException
- If the user is not authenticated, or if there is
a database errorpublic String generateToken(UserIdentity user, long duration, String type, String comment) throws RuntimeException
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
creationRuntimeException
- If the user is null or if there is a database
error or if duration is negativepublic String generateToken(UserIdentity user, long duration, Integer nbUsesLeft, String type, String comment) throws RuntimeException
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
creationRuntimeException
- If the user is null or if there is a database
error or if duration is negativepublic String generateToken(UserIdentity user, long duration, boolean autoRenewDuration, Integer nbUsesLeft, String context, String type, String comment) throws RuntimeException
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)context
- context 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
creationRuntimeException
- If the user is null or if there is a database
error or if duration is negativeprivate void _generateToken(UserIdentity user, long duration, boolean autoRenewDuration, Integer nbUsesLeft, String context, String type, String comment, String hashedTokenAndSalt, String salt, Timestamp creationDateTime, Timestamp endTime) throws RuntimeException
RuntimeException
private UserIdentity _validateToken(String encodedToken, String context, boolean forceRemove)
public UserIdentity validateToken(String token)
token
- The token to validatepublic UserIdentity validateToken(String token, String context)
token
- The token to validatecontext
- context to validate the token withpublic void deleteTokenByValue(String token, String context)
token
- The token to removecontext
- context of the token (null for no context)public void deleteTokenById(Integer tokenId)
tokenId
- The token identifier to removeprivate void _deleteOldTokens(Connection connection) throws SQLException
connection
- the database's sessionSQLException
- if a sql exception occursprivate PreparedStatement _getSelectUserTokenStatement(Connection connection, String login, String populationId, String type) throws SQLException
connection
- the database's sessionlogin
- The login of the userpopulationId
- The populationId of the usertype
- The type to filter or null to get allSQLException
- if a sql exception occursprivate AuthenticationTokenManager.Token _getTokenFromResultSet(ResultSet resultSet, Connection connection) throws SQLException, IOException
SQLException
IOException
private void _deleteUserToken(Connection connection, Integer id) throws SQLException
connection
- the database's sessionid
- the token idSQLException
- if an error occurredprivate void _updateUserToken(Connection connection, AuthenticationTokenManager.Token token) throws SQLException
connection
- the database's sessiontoken
- the tokenSQLException
- if an error occurredpublic String generateAuthenticationToken(Map<String,Object> parameters)
parameters
- a map of the following parameters for the
authentication token : descriptionpublic void deleteAuthenticationToken(List<Integer> ids)
ids
- a list of authentication token ids