Package org.owasp.esapi.reference
Class DefaultUser
- java.lang.Object
-
- org.owasp.esapi.reference.DefaultUser
-
- All Implemented Interfaces:
java.io.Serializable,java.security.Principal,User
public class DefaultUser extends java.lang.Object implements User, java.io.Serializable
Reference implementation of the User interface. This implementation is serialized into a flat file in a simple format.- Since:
- June 1, 2007
- Author:
- Jeff Williams (jeff.williams .at. aspectsecurity.com) Aspect Security, Chris Schmidt (chrisisbeef .at. gmail.com) Digital Ritual Software
- See Also:
User, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description DefaultUser(java.lang.String accountName)Instantiates a new user.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddRole(java.lang.String role)Adds a role to this user's account.voidaddRoles(java.util.Set<java.lang.String> newRoles)Adds a set of roles to this user's account.voidaddSession(javax.servlet.http.HttpSession s)Adds a session for this User.voidchangePassword(java.lang.String oldPassword, java.lang.String newPassword1, java.lang.String newPassword2)Sets the user's password, performing a verification of the user's old password, the equality of the two new passwords, and the strength of the new password.java.lang.Objectclone()Override clone and make final to prevent duplicate user objects.voiddisable()Disable this user's account.voidenable()Enable this user's account.longgetAccountId()Gets this user's account id number.java.lang.StringgetAccountName()Gets this user's account name.java.lang.StringgetCSRFToken()Gets the CSRF token for this user's current sessions.java.util.HashMapgetEventMap()Returns the hashmap used to store security events for this user.java.util.DategetExpirationTime()Returns the date that this user's account will expire.intgetFailedLoginCount()Returns the number of failed login attempts since the last successful login for an account.java.util.DategetLastFailedLoginTime()Returns the date of the last failed login time for a user.java.lang.StringgetLastHostAddress()Returns the last host address used by the user.java.util.DategetLastLoginTime()Returns the date of the last successful login time for a user.java.util.DategetLastPasswordChangeTime()Gets the date of user's last password change.java.util.LocalegetLocale()java.lang.StringgetName()java.util.Set<java.lang.String>getRoles()Gets the roles assigned to a particular account.java.lang.StringgetScreenName()Gets the screen name (alias) for the current user.java.util.SetgetSessions()Returns a Set containing the sessions associated with this User.voidincrementFailedLoginCount()Increment failed login count.booleanisAnonymous()Checks if user is anonymous.booleanisEnabled()Checks if this user's account is currently enabled.booleanisExpired()Checks if this user's account is expired.booleanisInRole(java.lang.String role)Checks if this user's account is assigned a particular role.booleanisLocked()Checks if this user's account is locked.booleanisLoggedIn()Tests to see if the user is currently logged in.booleanisSessionAbsoluteTimeout()Tests to see if this user's session has exceeded the absolute time out based on ESAPI's configuration settings.booleanisSessionTimeout()Tests to see if the user's session has timed out from inactivity based on ESAPI's configuration settings.voidlock()Lock this user's account.voidloginWithPassword(java.lang.String password)Login with password.voidlogout()Logout this user.voidremoveRole(java.lang.String role)Removes a role from this user's account.voidremoveSession(javax.servlet.http.HttpSession s)Removes a session for this User.java.lang.StringresetCSRFToken()Returns a token to be used as a prevention against CSRF attacks.voidsetAccountName(java.lang.String accountName)Sets this user's account name.voidsetExpirationTime(java.util.Date expirationTime)Sets the date and time when this user's account will expire.voidsetLastFailedLoginTime(java.util.Date lastFailedLoginTime)Set the time of the last failed login for this user.voidsetLastHostAddress(java.lang.String remoteHost)Set the last remote host address used by this user.voidsetLastLoginTime(java.util.Date lastLoginTime)Set the time of the last successful login for this user.voidsetLastPasswordChangeTime(java.util.Date lastPasswordChangeTime)Set the time of the last password change for this user.voidsetLocale(java.util.Locale locale)voidsetRoles(java.util.Set<java.lang.String> roles)Sets the roles for this account.voidsetScreenName(java.lang.String screenName)Sets the screen name (username alias) for this user.java.lang.StringtoString()voidunlock()Unlock this user's account.booleanverifyPassword(java.lang.String password)Verify that the supplied password matches the password for this user.
-
-
-
Method Detail
-
addRole
public void addRole(java.lang.String role) throws AuthenticationExceptionAdds a role to this user's account.- Specified by:
addRolein interfaceUser- Parameters:
role- the role to add- Throws:
AuthenticationException- the authentication exception
-
addRoles
public void addRoles(java.util.Set<java.lang.String> newRoles) throws AuthenticationExceptionAdds a set of roles to this user's account.- Specified by:
addRolesin interfaceUser- Parameters:
newRoles- the new roles to add- Throws:
AuthenticationException- the authentication exception
-
changePassword
public void changePassword(java.lang.String oldPassword, java.lang.String newPassword1, java.lang.String newPassword2) throws AuthenticationException, EncryptionExceptionSets the user's password, performing a verification of the user's old password, the equality of the two new passwords, and the strength of the new password.- Specified by:
changePasswordin interfaceUser- Parameters:
oldPassword- the old passwordnewPassword1- the new passwordnewPassword2- the new password - used to verify that the new password was typed correctly- Throws:
AuthenticationException- if newPassword1 does not match newPassword2, if oldPassword does not match the stored old password, or if the new password does not meet complexity requirementsEncryptionException
-
getAccountId
public long getAccountId()
Gets this user's account id number.- Specified by:
getAccountIdin interfaceUser- Returns:
- the account id
-
getAccountName
public java.lang.String getAccountName()
Gets this user's account name.- Specified by:
getAccountNamein interfaceUser- Returns:
- the account name
-
getCSRFToken
public java.lang.String getCSRFToken()
Gets the CSRF token for this user's current sessions.- Specified by:
getCSRFTokenin interfaceUser- Returns:
- the CSRF token
-
getExpirationTime
public java.util.Date getExpirationTime()
Returns the date that this user's account will expire.- Specified by:
getExpirationTimein interfaceUser- Returns:
- Date representing the account expiration time.
-
getFailedLoginCount
public int getFailedLoginCount()
Returns the number of failed login attempts since the last successful login for an account. This method is intended to be used as a part of the account lockout feature, to help protect against brute force attacks. However, the implementor should be aware that lockouts can be used to prevent access to an application by a legitimate user, and should consider the risk of denial of service.- Specified by:
getFailedLoginCountin interfaceUser- Returns:
- the number of failed login attempts since the last successful login
-
getLastFailedLoginTime
public java.util.Date getLastFailedLoginTime()
Returns the date of the last failed login time for a user. This date should be used in a message to users after a successful login, to notify them of potential attack activity on their account.- Specified by:
getLastFailedLoginTimein interfaceUser- Returns:
- date of the last failed login
-
getLastHostAddress
public java.lang.String getLastHostAddress()
Returns the last host address used by the user. This will be used in any log messages generated by the processing of this request.- Specified by:
getLastHostAddressin interfaceUser- Returns:
- the last host address used by the user
-
getLastLoginTime
public java.util.Date getLastLoginTime()
Returns the date of the last successful login time for a user. This date should be used in a message to users after a successful login, to notify them of potential attack activity on their account.- Specified by:
getLastLoginTimein interfaceUser- Returns:
- date of the last successful login
-
getLastPasswordChangeTime
public java.util.Date getLastPasswordChangeTime()
Gets the date of user's last password change.- Specified by:
getLastPasswordChangeTimein interfaceUser- Returns:
- the date of last password change
-
getName
public java.lang.String getName()
- Specified by:
getNamein interfacejava.security.Principal
-
getRoles
public java.util.Set<java.lang.String> getRoles()
Gets the roles assigned to a particular account.
-
getScreenName
public java.lang.String getScreenName()
Gets the screen name (alias) for the current user.- Specified by:
getScreenNamein interfaceUser- Returns:
- the screen name
-
addSession
public void addSession(javax.servlet.http.HttpSession s)
Adds a session for this User.- Specified by:
addSessionin interfaceUser- Parameters:
s- The session to associate with this user.
-
removeSession
public void removeSession(javax.servlet.http.HttpSession s)
Removes a session for this User.- Specified by:
removeSessionin interfaceUser- Parameters:
s- The session to remove from being associated with this user.
-
getSessions
public java.util.Set getSessions()
Returns a Set containing the sessions associated with this User.- Specified by:
getSessionsin interfaceUser- Returns:
- The Set of sessions for this User.
-
incrementFailedLoginCount
public void incrementFailedLoginCount()
Increment failed login count.- Specified by:
incrementFailedLoginCountin interfaceUser
-
isAnonymous
public boolean isAnonymous()
Checks if user is anonymous.- Specified by:
isAnonymousin interfaceUser- Returns:
- true, if user is anonymous
-
isEnabled
public boolean isEnabled()
Checks if this user's account is currently enabled.
-
isExpired
public boolean isExpired()
Checks if this user's account is expired.
-
isInRole
public boolean isInRole(java.lang.String role)
Checks if this user's account is assigned a particular role.
-
isLocked
public boolean isLocked()
Checks if this user's account is locked.
-
isLoggedIn
public boolean isLoggedIn()
Tests to see if the user is currently logged in.- Specified by:
isLoggedInin interfaceUser- Returns:
- true, if the user is logged in
-
isSessionAbsoluteTimeout
public boolean isSessionAbsoluteTimeout()
Tests to see if this user's session has exceeded the absolute time out based on ESAPI's configuration settings.- Specified by:
isSessionAbsoluteTimeoutin interfaceUser- Returns:
- true, if user's session has exceeded the absolute time out
-
isSessionTimeout
public boolean isSessionTimeout()
Tests to see if the user's session has timed out from inactivity based on ESAPI's configuration settings. A session may timeout prior to ESAPI's configuration setting due to the servlet container setting for session-timeout in web.xml. The following is an example of a web.xml session-timeout set for one hour.60 - Specified by:
isSessionTimeoutin interfaceUser- Returns:
- true, if user's session has timed out from inactivity based on ESAPI configuration
-
loginWithPassword
public void loginWithPassword(java.lang.String password) throws AuthenticationExceptionLogin with password.- Specified by:
loginWithPasswordin interfaceUser- Parameters:
password- the password- Throws:
AuthenticationException- if login fails
-
removeRole
public void removeRole(java.lang.String role)
Removes a role from this user's account.- Specified by:
removeRolein interfaceUser- Parameters:
role- the role to remove
-
resetCSRFToken
public java.lang.String resetCSRFToken()
Returns a token to be used as a prevention against CSRF attacks. This token should be added to all links and forms. The application should verify that all requests contain the token, or they may have been generated by a CSRF attack. It is generally best to perform the check in a centralized location, either a filter or controller. See the verifyCSRFToken method. In this implementation, we have chosen to use a random token that is stored in the User object. Note that it is possible to avoid the use of server side state by using either the hash of the users's session id or an encrypted token that includes a timestamp and the user's IP address. user's IP address. A relatively short 8 character string has been chosen because this token will appear in all links and forms.- Specified by:
resetCSRFTokenin interfaceUser- Returns:
- the string
-
setAccountName
public void setAccountName(java.lang.String accountName)
Sets this user's account name.- Specified by:
setAccountNamein interfaceUser- Parameters:
accountName- the new account name
-
setExpirationTime
public void setExpirationTime(java.util.Date expirationTime)
Sets the date and time when this user's account will expire.- Specified by:
setExpirationTimein interfaceUser- Parameters:
expirationTime- the new expiration time
-
setLastFailedLoginTime
public void setLastFailedLoginTime(java.util.Date lastFailedLoginTime)
Set the time of the last failed login for this user.- Specified by:
setLastFailedLoginTimein interfaceUser- Parameters:
lastFailedLoginTime- the date and time when the user just failed to login correctly.
-
setLastHostAddress
public void setLastHostAddress(java.lang.String remoteHost) throws AuthenticationHostExceptionSet the last remote host address used by this user.- Specified by:
setLastHostAddressin interfaceUser- Parameters:
remoteHost- The address of the user's current source host.- Throws:
AuthenticationHostException
-
setLastLoginTime
public void setLastLoginTime(java.util.Date lastLoginTime)
Set the time of the last successful login for this user.- Specified by:
setLastLoginTimein interfaceUser- Parameters:
lastLoginTime- the date and time when the user just successfully logged in.
-
setLastPasswordChangeTime
public void setLastPasswordChangeTime(java.util.Date lastPasswordChangeTime)
Set the time of the last password change for this user.- Specified by:
setLastPasswordChangeTimein interfaceUser- Parameters:
lastPasswordChangeTime- the date and time when the user just successfully changed his/her password.
-
setRoles
public void setRoles(java.util.Set<java.lang.String> roles) throws AuthenticationExceptionSets the roles for this account.- Specified by:
setRolesin interfaceUser- Parameters:
roles- the new roles- Throws:
AuthenticationException- the authentication exception
-
setScreenName
public void setScreenName(java.lang.String screenName)
Sets the screen name (username alias) for this user.- Specified by:
setScreenNamein interfaceUser- Parameters:
screenName- the new screen name
-
toString
public java.lang.String toString()
- Specified by:
toStringin interfacejava.security.Principal- Overrides:
toStringin classjava.lang.Object
-
verifyPassword
public boolean verifyPassword(java.lang.String password)
Verify that the supplied password matches the password for this user. This method is typically used for "reauthentication" for the most sensitive functions, such as transactions, changing email address, and changing other account information.- Specified by:
verifyPasswordin interfaceUser- Parameters:
password- the password that the user entered- Returns:
- true, if the password passed in matches the account's password
-
clone
public final java.lang.Object clone() throws java.lang.CloneNotSupportedExceptionOverride clone and make final to prevent duplicate user objects.- Overrides:
clonein classjava.lang.Object- Returns:
- Nothing, as clone() is not supported for this class. A CloneNotSupportedException is always thrown for this class.
- Throws:
java.lang.CloneNotSupportedException
-
getLocale
public java.util.Locale getLocale()
-
setLocale
public void setLocale(java.util.Locale locale)
-
getEventMap
public java.util.HashMap getEventMap()
Description copied from interface:UserReturns the hashmap used to store security events for this user. Used by the IntrusionDetector.- Specified by:
getEventMapin interfaceUser
-
-