String mode_values = "";
for (PasswordMode pm : PasswordMode.values())
mode_values += "'" + pm + "', ";
mode_values += "or leave it unspecified.";
throw new AuthenticationException("The requested password check mode '" + pmode_plain
+ "' is not a valid mode. Possible values are " + mode_values);
}
}
String pfilename = System.getProperty(PASSWD_FILENAME_PROPERTY);
String username = null;
CharSequence user = credentials.get(USERNAME_KEY);
if (null == user)
throw new AuthenticationException("Authentication request was missing the required key '" + USERNAME_KEY
+ "'");
else
username = user.toString();
String password = null;
CharSequence pass = credentials.get(PASSWORD_KEY);
if (null == pass)
throw new AuthenticationException("Authentication request was missing the required key '" + PASSWORD_KEY
+ "'");
else
password = pass.toString();
boolean authenticated = false;
InputStream in = null;
try
{
in = Thread.currentThread().getContextClassLoader().getResourceAsStream("passwd.properties");
// in = new BufferedInputStream(new FileInputStream(pfilename));
Properties props = new Properties();
props.load(in);
// note we keep the message here and for the wrong password exactly
// the same to prevent attackers from guessing what users are valid
if (null == props.getProperty(username))
throw new AuthenticationException(authenticationErrorMessage(mode, username));
switch (mode)
{
case PLAIN:
authenticated = password.equals(props.getProperty(username));
break;
case MD5:
authenticated = MessageDigest.isEqual(FBUtilities.threadLocalMD5Digest().digest(password.getBytes()),
Hex.hexToBytes(props.getProperty(username)));
break;
default:
throw new RuntimeException("Unknown PasswordMode " + mode);
}
}
catch (IOException e)
{
throw new RuntimeException("Authentication table file given by property " + PASSWD_FILENAME_PROPERTY
+ " could not be opened: " + e.getMessage());
}
catch (Exception e)
{
throw new RuntimeException("Unexpected authentication problem", e);
}
finally
{
FileUtils.closeQuietly(in);
}
if (!authenticated)
throw new AuthenticationException(authenticationErrorMessage(mode, username));
return new AuthenticatedUser(username);
}