@Transactional public class TokenAuthenticationManagerImpl extends Object implements TokenAuthenticationManager
Modifier and Type | Field and Description |
---|---|
protected static String |
RECENT_TOKENS_CACHE_SIZE_SYSTEM_PROPERTY_NAME |
protected static String |
RECENT_TOKENS_CACHE_TTL_SYSTEM_PROPERTY_NAME |
Constructor and Description |
---|
TokenAuthenticationManagerImpl(SessionTokenStorage tokenManager,
ApplicationDAO applicationDao,
TokenFactory tokenFactory,
com.atlassian.cache.Cache<String,Boolean> cache,
com.atlassian.event.api.EventPublisher eventPublisher,
PropertyManager propertyManager,
DirectoryManager directoryManager,
ApplicationManager applicationManager,
ApplicationService applicationService,
Clock clock) |
Modifier and Type | Method and Description |
---|---|
protected List<ValidationFactor> |
activeValidationFactors(ValidationFactor[] factors) |
Token |
authenticateApplication(Application application,
ApplicationAuthenticationContext authenticationContext,
TokenLifetime tokenLifetime)
Authenticates an application and generates an authentication token.
|
Token |
authenticateApplicationWithoutValidatingPassword(Application application,
ApplicationAuthenticationContext authenticationContext,
TokenLifetime tokenLifetime)
Authenticates an application and generates an authentication token, ignoring the credentials.
|
Token |
authenticateUser(Application application,
UserAuthenticationContext authenticationContext,
boolean validatePassword,
TokenLifetime tokenLifetime) |
Token |
authenticateUser(Application application,
UserAuthenticationContext authenticateContext,
TokenLifetime tokenLifetime)
Authenticates a user and and generates an authentication token.
|
Token |
authenticateUserWithoutValidatingPassword(Application application,
UserAuthenticationContext authenticateContext)
Feigns the authentication process for a user and creates a token for the authentication without validating the password.
|
List<Application> |
findAuthorisedApplications(User user,
String applicationName)
Returns a list of applications a user
is authorised to authenticate with.
|
User |
findUserByToken(Token token,
Application application)
Will find a user via the passed in token.
|
Token |
findUserTokenByKey(String tokenKey,
Application application)
Returns the token matching a given key
|
protected org.apache.commons.lang3.tuple.Pair<Token,Boolean> |
generateUserToken(long directoryID,
AuthenticationContext authenticationContext,
TokenLifetime tokenLifetime)
This method will return a
Token based on the passed in parameters. |
protected org.apache.commons.lang3.tuple.Pair<Token,Boolean> |
genericValidateToken(String token,
ValidationFactor[] validationFactors)
Will validate a token key with the given
ValidationFactor 's
against one (if it exists) in the datastore. |
Date |
getTokenExpiryTime(Token token)
Returns the expiry time of a token.
|
void |
invalidateAllTokens()
Invalidates all user and application tokens.
|
Optional<Token> |
invalidateToken(String tokenKey)
Attempts to invalidate a Token based on the passed in Token key (random hash).
|
void |
invalidateTokensForUser(String username,
String exclusionToken,
String applicationName)
Invalidates all sessions for a user, possibly excluding a specific one.
|
protected boolean |
isAllowedToAuthenticate(String username,
long directoryId,
Application application)
Determines if a user is permitted to attempt authentication with a given application.
|
protected boolean |
isExpired(Token token) |
protected org.apache.commons.lang3.tuple.Pair<Token,Boolean> |
maybeUpdateLastAccessedTime(Token token) |
void |
removeExpiredTokens()
Removes all tokens that have exceeded their expiry time.
|
Token |
validateApplicationToken(String tokenKey,
ValidationFactor[] clientValidationFactors)
Validates an application token key given validation factors.
|
Token |
validateUserToken(Application application,
String userTokenKey,
ValidationFactor[] validationFactors)
Validates a user token key given validation factors and checks that the user is allowed to authenticate
with the specified application
|
protected static final String RECENT_TOKENS_CACHE_SIZE_SYSTEM_PROPERTY_NAME
protected static final String RECENT_TOKENS_CACHE_TTL_SYSTEM_PROPERTY_NAME
public TokenAuthenticationManagerImpl(SessionTokenStorage tokenManager, ApplicationDAO applicationDao, TokenFactory tokenFactory, com.atlassian.cache.Cache<String,Boolean> cache, com.atlassian.event.api.EventPublisher eventPublisher, PropertyManager propertyManager, DirectoryManager directoryManager, ApplicationManager applicationManager, ApplicationService applicationService, Clock clock)
public Optional<Token> invalidateToken(String tokenKey)
TokenAuthenticationManager
If the token does not exist (ie. already invalidated) this method returns Optional.empty()
. If an existing token is successfully invalidated, a
TokenInvalidatedEvent is fired, and the invalidated token is returned
invalidateToken
in interface TokenAuthenticationManager
tokenKey
- the token key (random hash) to invalidate.public void invalidateAllTokens()
TokenAuthenticationManager
invalidateAllTokens
in interface TokenAuthenticationManager
public void removeExpiredTokens()
TokenAuthenticationManager
NOTE: Do not call this method from the web layer, as this is wrapped in a Spring managed transaction.
removeExpiredTokens
in interface TokenAuthenticationManager
public User findUserByToken(Token token, Application application) throws InvalidTokenException, OperationFailedException
TokenAuthenticationManager
findUserByToken
in interface TokenAuthenticationManager
token
- the tokenapplication
- the application to do the lookup forInvalidTokenException
- if the User or Directory cannot be found that relates to the given token,
or the token is associated to an Application and not a UserOperationFailedException
- if there was an issue accessing the user from the underlying directorypublic Token findUserTokenByKey(String tokenKey, Application application) throws InvalidTokenException, ApplicationAccessDeniedException, OperationFailedException
TokenAuthenticationManager
findUserTokenByKey
in interface TokenAuthenticationManager
tokenKey
- the token keyapplication
- the application to do the lookup forInvalidTokenException
- if the token cannot be found by the give key,
or the token is associated to an Application and not a UserApplicationAccessDeniedException
- the user is not allowed to authenticate with the application.OperationFailedException
- if there was an issue accessing the user from the underlying directorypublic List<Application> findAuthorisedApplications(User user, String applicationName) throws OperationFailedException, DirectoryNotFoundException
TokenAuthenticationManager
NOTE: this is a potentially expensive call, iterating all applications and all group mappings for each application and determining group membership, ie. expense = number of applications * number of group mappings per application.
findAuthorisedApplications
in interface TokenAuthenticationManager
user
- user to search for.applicationName
- name of the current applicationOperationFailedException
- if there was an error querying directory.DirectoryNotFoundException
- if the directory could not be found.public Token authenticateApplication(Application application, ApplicationAuthenticationContext authenticationContext, TokenLifetime tokenLifetime) throws InvalidAuthenticationException
TokenAuthenticationManager
authenticateApplication
in interface TokenAuthenticationManager
application
- the application being authenticatedauthenticationContext
- application authentication credentials.tokenLifetime
- Requested lifetime of the tokenInvalidAuthenticationException
- authentication was not successful because either the application does not exist, the password is incorrect, the application is inactive or there was a problem generating the authentication token.public Token authenticateApplicationWithoutValidatingPassword(Application application, ApplicationAuthenticationContext authenticationContext, TokenLifetime tokenLifetime) throws InvalidAuthenticationException
TokenAuthenticationManager
This method should only be used to generate a token for an application that has already authenticated via some other means (eg. TLS client certificates) as this method bypasses any password checks.
authenticateApplicationWithoutValidatingPassword
in interface TokenAuthenticationManager
application
- the application being authenticatedauthenticationContext
- application authentication credentials.tokenLifetime
- Requested lifetime of the tokenInvalidAuthenticationException
- authentication was not successful because either the application does not exist, the application is inactive or there was a problem generating the authentication token.public Token authenticateUser(Application application, UserAuthenticationContext authenticationContext, boolean validatePassword, TokenLifetime tokenLifetime) throws InvalidAuthenticationException, OperationFailedException, InactiveAccountException, ApplicationAccessDeniedException, ExpiredCredentialException
public Token authenticateUser(Application application, UserAuthenticationContext authenticateContext, TokenLifetime tokenLifetime) throws InvalidAuthenticationException, OperationFailedException, InactiveAccountException, ApplicationAccessDeniedException, ExpiredCredentialException
TokenAuthenticationManager
The RemoteDirectory.authenticate(String, com.atlassian.crowd.embedded.api.PasswordCredential)
method is
iteratively called for each assigned directory. If the user does not exist in one directory, the directory is skipped and the next one is examined. If the user does
not exist in any of the assigned directories then an InvalidAuthenticationException
is thrown.
authenticateUser
in interface TokenAuthenticationManager
authenticateContext
- The authentication details for the user.tokenLifetime
- Requested lifetime of the tokenInvalidAuthenticationException
- The authentication was not successful.OperationFailedException
- error thrown by directory implementation when attempting to find or authenticate the user.InactiveAccountException
- user account is inactive.ApplicationAccessDeniedException
- user does not have access to authenticate with application.ExpiredCredentialException
- the user's credentials have expired. The user must change their credentials in order to successfully authenticate.public Token authenticateUserWithoutValidatingPassword(Application application, UserAuthenticationContext authenticateContext) throws InvalidAuthenticationException, OperationFailedException, InactiveAccountException, ApplicationAccessDeniedException
TokenAuthenticationManager
This method should only be used to generate a token for a user that has already authenticated credentials via some other means (eg. SharePoint NTLM connector) as this method bypasses any password checks.
If you want actual password authentication, use the TokenAuthenticationManager.authenticateUser(Application, UserAuthenticationContext, TokenLifetime)
method.
authenticateUserWithoutValidatingPassword
in interface TokenAuthenticationManager
authenticateContext
- The authentication details for the user.InvalidAuthenticationException
- if the authentication was not successful.OperationFailedException
- if the error thrown by directory implementation when attempting to find or authenticate the user.InactiveAccountException
- if the user account is inactive.ApplicationAccessDeniedException
- if the user does not have access to authenticate with application.public Token validateApplicationToken(String tokenKey, ValidationFactor[] clientValidationFactors) throws InvalidTokenException
TokenAuthenticationManager
validateApplicationToken
in interface TokenAuthenticationManager
tokenKey
- returns a valid token corresponding to the tokenKey.clientValidationFactors
- validation factors for generating the token hash.InvalidTokenException
- if the tokenKey or corresponding client validation factors do not represent a valid application token.public Token validateUserToken(Application application, String userTokenKey, ValidationFactor[] validationFactors) throws InvalidTokenException, ApplicationAccessDeniedException, OperationFailedException
TokenAuthenticationManager
validateUserToken
in interface TokenAuthenticationManager
application
- the application performing the authenticationuserTokenKey
- returns a valid token corresponding to the tokenKey.validationFactors
- validation factors for generating the token hash.InvalidTokenException
- if the userTokenKey or corresponding validationFactors do not represent a valid SSO token.ApplicationAccessDeniedException
- the user is not allowed to authenticate with the application.OperationFailedException
- there was an error communicating with an underlying directory when determining if a user is allowed to authenticate with the application (eg. if a user has the appropriate group memberships).protected List<ValidationFactor> activeValidationFactors(ValidationFactor[] factors)
protected org.apache.commons.lang3.tuple.Pair<Token,Boolean> generateUserToken(long directoryID, AuthenticationContext authenticationContext, TokenLifetime tokenLifetime) throws InvalidTokenException, OperationFailedException
Token
based on the passed in parameters.
If a token already exists in the datastore, this token will be returned with an updated lastAccessed time.
If a token is not found based on the passed in parameters a new Token
will be generated and stored in the datastore.directoryID
- the directoryID you wish to generate a Token forauthenticationContext
- holder for the required attributes to authenticate against the Crowd servertokenLifetime
- requested lifetime of the tokenToken
and boolean indicating whether lastAccessedTime
was updatedInvalidTokenException
- if there was an issue generating the key for a token.OperationFailedException
- if adding the new token failedprotected org.apache.commons.lang3.tuple.Pair<Token,Boolean> genericValidateToken(String token, ValidationFactor[] validationFactors) throws InvalidTokenException
ValidationFactor
's
against one (if it exists) in the datastore.token
- the key of a Token
validationFactors
- the ValidationFactor
's that are being used for authenticationInvalidTokenException
- thrown if the token keys are not equal, or the token has expired, or the token does not existprotected org.apache.commons.lang3.tuple.Pair<Token,Boolean> maybeUpdateLastAccessedTime(Token token) throws ObjectNotFoundException
ObjectNotFoundException
protected boolean isExpired(Token token)
protected boolean isAllowedToAuthenticate(String username, long directoryId, Application application) throws OperationFailedException, DirectoryNotFoundException
For a a user to have access to an application:
And either:
Note that this call is not cached and does not affect the cache.
application
- application the user wants to authenticate with.username
- the username of the user that wants to authenticate with the application.directoryId
- the directoryId of the user that wants to authenticate with the application.true
iff the user is permitted to attempt authentication with the application.OperationFailedException
- if the directory implementation could not be loaded when performing a membership check.DirectoryNotFoundException
public void invalidateTokensForUser(String username, @Nullable String exclusionToken, String applicationName) throws UserNotFoundException, ApplicationNotFoundException
TokenAuthenticationManager
invalidateTokensForUser
in interface TokenAuthenticationManager
exclusionToken
- the random hash of a token to leave validapplicationName
- name of the current applicationUserNotFoundException
ApplicationNotFoundException
public Date getTokenExpiryTime(Token token)
TokenAuthenticationManager
getTokenExpiryTime
in interface TokenAuthenticationManager
token
- a tokenCopyright © 2021 Atlassian. All rights reserved.