ilex.util.PasswordFileEntry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opendcs Show documentation
Show all versions of opendcs Show documentation
A collection of software for aggregatting and processing environmental data such as from NOAA GOES satellites.
The newest version!
/*
* $Id$
*/
package ilex.util;
import java.security.*;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.NoSuchElementException;
import java.io.*;
/**
Represent a single entry in a PasswordFile.
*/
public class PasswordFileEntry
implements HasProperties, Cloneable, Serializable
{
private static final long serialVersionUID = 1L;
/** the user name */
private String username;
/** the roles assigned to this user */
private String[] roles;
/** SHA hash of the password */
private byte[] ShaPassword;
/** Additional properties of this entry */
private Properties properties;
private PasswordFile owner = null;
private boolean changed = false;
private boolean local = false;
private Date lastModified = null;
public static final String digestAlgo = "SHA";
//=================================================================
// Constructors & Parsers
//=================================================================
/**
* Constructs a PasswordFileEntry given complete arguments.
*
* @param username the user name
* @param roles the role names
* @param ShaPassword the SHA password
* @throws AuthException if username is null or zero length.
*/
public PasswordFileEntry( String username, String[] roles,
byte[] ShaPassword )
throws AuthException
{
this(username);
this.roles = roles == null ? null : (String[])roles.clone();
this.ShaPassword = (byte[])ShaPassword.clone();
}
/**
* Constructs a password file entry with just the username assigned.
*
* @param username the user name
* @throws AuthException if username is null or zero length.
*/
public PasswordFileEntry( String username ) throws AuthException
{
this();
if (username == null || username.length() == 0)
throw new AuthException("Username may not be blank.");
this.username = username;
}
/**
* Constructs a password file entry with username and entered password.
*
* @param username the user name
* @param password the password, the SHA hash will be stored.
* @throws AuthException if username is null or zero length.
*/
public PasswordFileEntry( String username, String password )
throws AuthException
{
this(username);
setPassword(password);
}
/**
* Construct an empty password file entry.
*/
public PasswordFileEntry( )
{
this.username = null;
roles = null;
ShaPassword = null;
properties = new Properties();
}
/**
* Parses a line from a password file into this entry.
* Lines are of the following format:
*
* username:role1,role2,...:sha-password:prop=value,prop=value,...
*
* Username, and password must be non-blank. At least one role must
* be listed. The final field is a list of optional properties.
*
* @param file_line the line from the file
* @throws AuthException if the line is improperly formatted.
*/
public void parseLine( String file_line )
throws AuthException
{
StringTokenizer tokenizer = new StringTokenizer(file_line,":");
if (tokenizer.countTokens() < 3)
throw new AuthException("Only " + tokenizer.countTokens()
+ " tokens, need at least 3 for username, roles, password");
try
{
// Extract the tokens from the line.
username = tokenizer.nextToken();
String roles_str = tokenizer.nextToken();
String passwd_str = tokenizer.nextToken();
// prop str starts after the colon after password
String prop_str = null;
if (passwd_str != null && passwd_str.length() > 0)
{
int idx = file_line.indexOf(passwd_str);
String x = file_line.substring(idx);
idx = x.indexOf(':');
if (idx > 0 && x.length() > idx+1)
{
idx++;
prop_str = x.substring(idx);
}
}
// Special string 'none' for roles:
if (roles_str.compareToIgnoreCase("none") == 0)
roles = null;
else
{
// Parse the individual roles.
tokenizer = new StringTokenizer(roles_str, ",");
int n = tokenizer.countTokens();
roles = new String[n];
for(int i = 0; i < n; i++)
roles[i] = tokenizer.nextToken();
}
// Convert the hex password string to a byte array.
ShaPassword = ByteUtil.fromHexString(passwd_str);
if (prop_str != null)
properties = PropertiesUtil.string2props(prop_str);
}
catch(NoSuchElementException e)
{
throw new AuthException(e.toString());
}
}
/**
* @return String formatted for storage in a password file
* @see PasswordFileEntry.parseLine(String)
*/
public String toString( )
{
StringBuffer sb = new StringBuffer(username);
sb.append(':');
if (roles != null)
for(int i = 0; i 0)
// sb.append(',');
// sb.append(pname + "=" + properties.getProperty(pname));
// }
// return sb.toString();
}
/**
* @return a deep-copy clone of this entry.
* @throws CloneNotSupportedException
*/
public Object clone( ) throws CloneNotSupportedException
{
try
{
PasswordFileEntry pfe = new PasswordFileEntry(username);
pfe.roles = roles == null ? null : (String[])roles.clone();
pfe.ShaPassword = ShaPassword == null ? null :
(byte[])ShaPassword.clone();
return pfe;
}
catch(AuthException e)
{
throw new CloneNotSupportedException(e.toString());
}
}
//=================================================================
// Accessor methods:
//=================================================================
/**
* @return the username component of this entry.
*/
public String getUsername( )
{
return username;
}
/**
* @return the array of strings containing the roles assigned to this
* entry (may be null).
*/
public String[] getRoles( )
{
return roles;
}
/**
* Determines if a specific role is assigned to this entry.
* @param role the role name
* @return true if this entry has that role
*/
public boolean isRoleAssigned( String role )
{
if (roles == null)
return false;
for(int i=0; i