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 |
static String |
ROLE
The avalon role
|
static String |
TOKEN_SEPARATOR
The separator in token
|
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,
String id)
Deletes the database entry that has this token
|
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 Users_Token table
|
private UserIdentity |
_validateToken(String token,
boolean forceRemove) |
void |
deleteTokenById(String tokenId)
Destroy the given token
|
void |
deleteTokenByValue(String token)
Destroy the given token
|
String |
generateToken(long duration,
String type,
String comment)
Generates a new token for the current user
|
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 s
|
getLogger, setLogger
public static final String TOKEN_SEPARATOR
private ServiceManager _manager
private CurrentUserProvider _currentUserProvider
private String _datasourceId
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 negativeprivate UserIdentity _validateToken(String token, boolean forceRemove)
public UserIdentity validateToken(String token)
token
- The token to validatepublic void deleteTokenByValue(String token)
token
- The token to removepublic void deleteTokenById(String 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 void _deleteUserToken(Connection connection, String id) throws SQLException
connection
- the database's sessionid
- the token idSQLException
- if an error occurred