public class FileBasedAuthenticator extends AbstractAuthenticator
account id | account name | hashed password | roles | lockout | status | old password hashes | last hostname | last change | last login | last failed | expiration | failed --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 1203123710837 | mitch | 44k/NAzQUlrCq9musTGGkcMNmdzEGJ8w8qZTLzpxLuQ= | admin,user | unlocked | enabled | u10dW4vTo3ZkoM5xP+blayWCz7KdPKyKUojOn9GJobg= | 192.168.1.255 | 1187201000926 | 1187200991568 | 1187200605330 | 2187200605330 | 1
Authenticator| Modifier and Type | Field and Description |
|---|---|
private long |
checkInterval
How frequently to check the user db for external modifications
|
private long |
lastChecked
The last time we checked if the user db had been modified externally
|
private long |
lastModified
The last modified time we saw on the user db.
|
private Logger |
logger
The logger.
|
private static int |
MAX_ACCOUNT_NAME_LENGTH |
private java.util.Map<User,java.util.List<java.lang.String>> |
passwordMap |
private static Authenticator |
singletonInstance |
private java.io.File |
userDB
The file that contains the user db
|
private java.util.Map<java.lang.Long,User> |
userMap
The user map.
|
USER| Modifier | Constructor and Description |
|---|---|
private |
FileBasedAuthenticator() |
| Modifier and Type | Method and Description |
|---|---|
void |
changePassword(User user,
java.lang.String currentPassword,
java.lang.String newPassword,
java.lang.String newPassword2)
Changes the password for the specified user.
|
private DefaultUser |
createUser(java.lang.String line)
Create a new user with all attributes from a String.
|
User |
createUser(java.lang.String accountName,
java.lang.String password1,
java.lang.String password2)
Creates a new User with the information provided.
|
private java.lang.String |
dump(java.util.Collection<java.lang.String> c)
Dump a collection as a comma-separated list.
|
java.lang.String |
generateStrongPassword()
Generate a strong password.
|
private java.lang.String |
generateStrongPassword(java.lang.String oldPassword)
Generate a strong password that is not similar to the specified old password.
|
java.lang.String |
generateStrongPassword(User user,
java.lang.String oldPassword)
Generate strong password that takes into account the user's information and old password.
|
(package private) java.util.List<java.lang.String> |
getAllHashedPasswords(User user,
boolean create)
Returns all of the specified User's hashed passwords.
|
(package private) java.lang.String |
getHashedPassword(User user)
Return the specified User's current hashed password.
|
static Authenticator |
getInstance() |
(package private) java.util.List<java.lang.String> |
getOldPasswordHashes(User user)
Get a List of the specified User's old password hashes.
|
User |
getUser(long accountId)
Returns the User matching the provided accountId.
|
User |
getUser(java.lang.String accountName)
Returns the User matching the provided accountName.
|
java.util.Set |
getUserNames()
Gets a collection containing all the existing user names.
|
java.lang.String |
hashPassword(java.lang.String password,
java.lang.String accountName)
Returns a string representation of the hashed password, using the
accountName as the salt.
|
protected void |
loadUsersIfNecessary()
Load users if they haven't been loaded in a while.
|
protected void |
loadUsersImmediately() |
static void |
main(java.lang.String[] args)
Fail safe main program to add or update an account in an emergency.
|
void |
removeUser(java.lang.String accountName)
Removes the account of the specified accountName.
|
private java.lang.String |
save(DefaultUser user)
Save.
|
void |
saveUsers()
Saves the user database to the file system.
|
protected void |
saveUsers(java.io.PrintWriter writer)
Save users.
|
private void |
setHashedPassword(User user,
java.lang.String hash)
Add a hash to a User's hashed password list.
|
(package private) void |
setOldPasswordHashes(User user,
java.util.List<java.lang.String> oldHashes)
Set the specified User's old password hashes.
|
void |
verifyAccountNameStrength(java.lang.String newAccountName)
Ensures that the account name passes site-specific complexity requirements, like minimum length.
|
boolean |
verifyPassword(User user,
java.lang.String password)
Verify that the supplied password matches the password for this user.
|
void |
verifyPasswordStrength(java.lang.String oldPassword,
java.lang.String newPassword,
User user)
Ensures that the password meets site-specific complexity requirements, like length or number
of character sets.
|
clearCurrent, exists, getCurrentUser, getUserFromRememberToken, getUserFromSession, login, login, logout, setCurrentUserprivate static volatile Authenticator singletonInstance
private final Logger logger
private java.io.File userDB
private long checkInterval
private long lastModified
private long lastChecked
private static final int MAX_ACCOUNT_NAME_LENGTH
private java.util.Map<java.lang.Long,User> userMap
private java.util.Map<User,java.util.List<java.lang.String>> passwordMap
public static Authenticator getInstance()
public static void main(java.lang.String[] args)
throws java.lang.Exception
java -Dorg.owasp.esapi.resources="/path/resources" -classpath esapi.jar org.owasp.esapi.Authenticator alice password admin
args - the arguments (username, password, role)java.lang.Exception - the exceptionprivate void setHashedPassword(User user, java.lang.String hash)
user - the user to associate with the new hashhash - the hash to store in the user's password hash listjava.lang.String getHashedPassword(User user)
user - this User's current hashed password will be returnedvoid setOldPasswordHashes(User user, java.util.List<java.lang.String> oldHashes)
user - the User whose old password hashes will be setoldHashes - a list of the User's old password hashes *java.util.List<java.lang.String> getAllHashedPasswords(User user, boolean create)
user - the User whose old hashes should be returnedcreate - true - if no password list is associated with this user, create one
false - if no password list is associated with this user, do not create onejava.util.List<java.lang.String> getOldPasswordHashes(User user)
user - he user whose old password hashes should be returnedpublic User createUser(java.lang.String accountName, java.lang.String password1, java.lang.String password2) throws AuthenticationException
accountName - the account name of the new userpassword1 - the password of the new userpassword2 - the password of the new user. This field is to encourage user interface designers to include two password fields in their forms.AuthenticationException - if user creation fails due to any of the qualifications listed in this method's descriptionpublic java.lang.String generateStrongPassword()
private java.lang.String generateStrongPassword(java.lang.String oldPassword)
oldPassword - the password to be compared to the new password for similaritypublic void changePassword(User user, java.lang.String currentPassword, java.lang.String newPassword, java.lang.String newPassword2) throws AuthenticationException
user - the user to change the password forcurrentPassword - the current password for the specified usernewPassword - the new password to usenewPassword2 - a verification copy of the new passwordAuthenticationException - if any errors occurpublic boolean verifyPassword(User user, java.lang.String password)
user - the user who requires verificationpassword - the hashed user-supplied passwordpublic java.lang.String generateStrongPassword(User user, java.lang.String oldPassword)
user - the user whose information to use when generating passwordoldPassword - the old password to use when verifying strength of new password. The new password may be checked for fragments of oldPassword.public User getUser(long accountId)
accountId - the account idpublic User getUser(java.lang.String accountName)
accountName - the account namepublic java.util.Set getUserNames()
public java.lang.String hashPassword(java.lang.String password,
java.lang.String accountName)
throws EncryptionException
password - the password to hashaccountName - the account name to use as the saltEncryptionExceptionprotected void loadUsersIfNecessary()
protected void loadUsersImmediately()
private DefaultUser createUser(java.lang.String line) throws AuthenticationException
line - parameters to set as attributes for the new User.AuthenticationExceptionpublic void removeUser(java.lang.String accountName)
throws AuthenticationException
accountName - the account name to removeAuthenticationException - the authentication exception if user does not existpublic void saveUsers()
throws AuthenticationException
AuthenticationException - if the user file could not be writtenprotected void saveUsers(java.io.PrintWriter writer)
throws AuthenticationCredentialsException
writer - the print writer to use for savingAuthenticationCredentialsExceptionprivate java.lang.String save(DefaultUser user)
user - the User to saveprivate java.lang.String dump(java.util.Collection<java.lang.String> c)
c - the collection to convert to a comma separated listpublic void verifyAccountNameStrength(java.lang.String newAccountName)
throws AuthenticationException
newAccountName - AuthenticationException - if account name does not meet complexity requirementspublic void verifyPasswordStrength(java.lang.String oldPassword,
java.lang.String newPassword,
User user)
throws AuthenticationException
oldPassword - the old passwordnewPassword - the new passworduser - the userAuthenticationException - if newPassword is too similar to oldPassword or if newPassword does not meet complexity requirements