Interface TokenAuthenticationModel
-
- All Known Implementing Classes:
SecureTokenAuthenticationModelImpl
,TokenAuthenticationModelImpl
public interface TokenAuthenticationModel
Encapsulates managing of access tokens for users- Since:
- 2019.1
- Author:
- Dmitrii Bogdanov
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static class
TokenAuthenticationModel.CheckCredentialsResult
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description TokenAuthenticationModel.CheckCredentialsResult
checkCredentials(SimpleCredentials credentials)
Checks whether the password provided in credentials matches existing token for user with provided usernameint
countUserTokens(long userId)
Count user tokensAuthenticationToken
createToken(long userId, String tokenName, Date expirationTime)
Generate token withtokenName
for userId.AuthenticationToken
createToken(long userId, String tokenName, Date expirationTime, AuthenticationToken.PermissionsRestriction permissionsRestriction)
Generate token withtokenName
for userId.void
deleteAllTokens()
Delete all tokensvoid
deleteAllUserTokens(long userId)
Delete all user tokensvoid
deleteToken(long userId, String tokenName)
Delete token by user id and token nameAuthenticationToken
find(String id)
List<AuthenticationToken>
getUserTokens(long userId)
List all user tokensboolean
isMyToken(String tokenValue)
Check whether the token was generated by this moduleBearerTokenCredentials
parseToken(String token)
Parse token from raw stringvoid
updateLastAccessInfo(SimpleCredentials credentials, String remoteAddress)
Update information about last usage of a token.boolean
userHasReachedMaxAllowedNumberOfTokens(long userId)
Check whether user has already reached max number of allowed tokensvoid
validateName(String name)
Validate the token name
-
-
-
Method Detail
-
createToken
@NotNull AuthenticationToken createToken(long userId, @NotNull String tokenName, @NotNull Date expirationTime) throws AuthenticationTokenStorage.CreationException
Generate token withtokenName
for userId. Subsequent invocation ofparseToken(java.lang.String)
on the string returned by this method should result in correct instance ofBearerTokenCredentials
- Parameters:
userId
-tokenName
-expirationTime
-- Returns:
- generated token
- Throws:
AuthenticationTokenStorage.CreationException
- if the user already has the token with specified nameAuthenticationTokenStorage.CreationException
- if the user has already reached max allowed number of tokens
-
createToken
@NotNull AuthenticationToken createToken(long userId, @NotNull String tokenName, @NotNull Date expirationTime, @NotNull AuthenticationToken.PermissionsRestriction permissionsRestriction) throws AuthenticationTokenStorage.CreationException
Generate token withtokenName
for userId. Subsequent invocation ofparseToken(java.lang.String)
on the string returned by this method should result in correct instance ofBearerTokenCredentials
- Parameters:
userId
-tokenName
-expirationTime
-permissionsRestriction
-- Returns:
- generated token
- Throws:
AuthenticationTokenStorage.CreationException
- if the user already has the token with specified nameAuthenticationTokenStorage.CreationException
- if the user has already reached max allowed number of tokens
-
getUserTokens
@NotNull List<AuthenticationToken> getUserTokens(long userId)
List all user tokens- Parameters:
userId
-- Returns:
- empty collection if user doesn't have tokens or list of tokens without values
-
countUserTokens
int countUserTokens(long userId)
Count user tokens- Parameters:
userId
-- Returns:
- number of user tokens
- Since:
- 2019.1.1
-
updateLastAccessInfo
void updateLastAccessInfo(@NotNull SimpleCredentials credentials, @Nullable String remoteAddress)
Update information about last usage of a token.- Parameters:
credentials
-remoteAddress
-
-
deleteToken
void deleteToken(long userId, @NotNull String tokenName) throws AuthenticationTokenStorage.DeletionException
Delete token by user id and token name- Parameters:
userId
-tokenName
-- Throws:
AuthenticationTokenStorage.DeletionException
- if token does not exist
-
deleteAllTokens
void deleteAllTokens()
Delete all tokens
-
deleteAllUserTokens
void deleteAllUserTokens(long userId)
Delete all user tokens- Parameters:
userId
-
-
parseToken
@NotNull BearerTokenCredentials parseToken(@NotNull String token) throws InvalidBearerTokenException
Parse token from raw string- Parameters:
token
-- Returns:
- BearerTokenCredentials containing name and token value
- Throws:
InvalidBearerTokenException
- if token has invalid format
-
userHasReachedMaxAllowedNumberOfTokens
boolean userHasReachedMaxAllowedNumberOfTokens(long userId)
Check whether user has already reached max number of allowed tokens- Parameters:
userId
-- Returns:
- true if user already has max number of tokens
-
checkCredentials
TokenAuthenticationModel.CheckCredentialsResult checkCredentials(@NotNull SimpleCredentials credentials)
Checks whether the password provided in credentials matches existing token for user with provided username- Parameters:
credentials
-- Returns:
- Throws:
InvalidBearerTokenException
- if password in credentials is not a valid tokenNotAuthenticatedBearerTokenException
- if token is valid but does not match username or if the user does not exist
-
isMyToken
boolean isMyToken(@NotNull String tokenValue)
Check whether the token was generated by this module- Parameters:
tokenValue
- token- Returns:
- true if token created by this module
-
validateName
void validateName(@NotNull String name) throws AuthenticationTokenStorage.InvalidTokenName
Validate the token name- Parameters:
name
- token name- Throws:
AuthenticationTokenStorage.InvalidTokenName
- when the name is not valid
-
find
AuthenticationToken find(@NotNull String id)
-
-