Clean up auth provider code, avoid NPE with property. Use getRequiredProperty() for required property.

This commit is contained in:
Michael Jumper
2013-08-22 16:31:36 -07:00
parent ab87ba5614
commit 5af0052430

View File

@@ -44,6 +44,7 @@ import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import net.sourceforge.guacamole.GuacamoleException; import net.sourceforge.guacamole.GuacamoleException;
import net.sourceforge.guacamole.GuacamoleServerException;
import net.sourceforge.guacamole.net.auth.simple.SimpleAuthenticationProvider; import net.sourceforge.guacamole.net.auth.simple.SimpleAuthenticationProvider;
import net.sourceforge.guacamole.net.auth.Credentials; import net.sourceforge.guacamole.net.auth.Credentials;
import net.sourceforge.guacamole.properties.FileGuacamoleProperty; import net.sourceforge.guacamole.properties.FileGuacamoleProperty;
@@ -81,41 +82,58 @@ import org.xml.sax.helpers.XMLReaderFactory;
* </config> * </config>
* </configs> * </configs>
* *
* @author Laurent Meunier
*/ */
public class NoAuthenticationProvider extends SimpleAuthenticationProvider { public class NoAuthenticationProvider extends SimpleAuthenticationProvider {
/**
* Logger for this class.
*/
private Logger logger = LoggerFactory.getLogger(NoAuthenticationProvider.class); private Logger logger = LoggerFactory.getLogger(NoAuthenticationProvider.class);
/**
* Map of all known configurations, indexed by identifier.
*/
private Map<String, GuacamoleConfiguration> configs; private Map<String, GuacamoleConfiguration> configs;
/**
* The last time the configuration XML was modified, as milliseconds since
* UNIX epoch.
*/
private long configTime; private long configTime;
/** /**
* The filename of the XML file to read the user mapping from. * The filename of the XML file to read the user mapping from.
*/ */
public static final FileGuacamoleProperty NOAUTH_CONFIG = new FileGuacamoleProperty() { public static final FileGuacamoleProperty NOAUTH_CONFIG = new FileGuacamoleProperty() {
@Override @Override
public String getName() { public String getName() {
return "noauth-config"; return "noauth-config";
} }
}; };
/**
* Retrieves the configuration file, as defined within guacamole.properties.
*
* @return The configuration file, as defined within guacamole.properties.
* @throws GuacamoleException If an error occurs while reading the
* property.
*/
private File getConfigurationFile() throws GuacamoleException { private File getConfigurationFile() throws GuacamoleException {
// Get configuration file return GuacamoleProperties.getRequiredProperty(NOAUTH_CONFIG);
return GuacamoleProperties.getProperty(NOAUTH_CONFIG);
} }
public synchronized void init() throws GuacamoleException { public synchronized void init() throws GuacamoleException {
// Get configuration file // Get configuration file
File configFile = getConfigurationFile(); File configFile = getConfigurationFile();
if(configFile == null) {
throw new GuacamoleException(
"Missing \"noauth-config\" parameter required for NoAuthenticationProvider."
);
}
logger.info("Reading configuration file: {}", configFile); logger.info("Reading configuration file: {}", configFile);
// Parse document // Parse document
try { try {
// Set up parser // Set up parser
NoAuthConfigContentHandler contentHandler = new NoAuthConfigContentHandler(); NoAuthConfigContentHandler contentHandler = new NoAuthConfigContentHandler();
@@ -130,21 +148,24 @@ public class NoAuthenticationProvider extends SimpleAuthenticationProvider {
// Init configs // Init configs
configTime = configFile.lastModified(); configTime = configFile.lastModified();
configs = contentHandler.getConfigs(); configs = contentHandler.getConfigs();
} }
catch (IOException e) { catch (IOException e) {
throw new GuacamoleException("Error reading configuration file: " + e.getMessage(), e); throw new GuacamoleServerException("Error reading configuration file: " + e.getMessage(), e);
} }
catch (SAXException e) { catch (SAXException e) {
throw new GuacamoleException("Error parsing XML file: " + e.getMessage(), e); throw new GuacamoleServerException("Error parsing XML file: " + e.getMessage(), e);
} }
} }
@Override @Override
public Map<String, GuacamoleConfiguration> getAuthorizedConfigurations(Credentials credentials) throws GuacamoleException { public Map<String, GuacamoleConfiguration> getAuthorizedConfigurations(Credentials credentials) throws GuacamoleException {
// Check mapping file mod time // Check mapping file mod time
File configFile = getConfigurationFile(); File configFile = getConfigurationFile();
if (configFile.exists() && configTime < configFile.lastModified()) { if (configFile.exists() && configTime < configFile.lastModified()) {
// If modified recently, gain exclusive access and recheck // If modified recently, gain exclusive access and recheck
synchronized (this) { synchronized (this) {
if (configFile.exists() && configTime < configFile.lastModified()) { if (configFile.exists() && configTime < configFile.lastModified()) {
@@ -152,17 +173,14 @@ public class NoAuthenticationProvider extends SimpleAuthenticationProvider {
init(); // If still not up to date, re-init init(); // If still not up to date, re-init
} }
} }
} }
// If no mapping available, report as such // If no mapping available, report as such
if (configs == null) { if (configs == null)
throw new GuacamoleException("Configuration could not be read."); throw new GuacamoleServerException("Configuration could not be read.");
}
// Guacamole 0.8 wants a username to be set, otherwise the
// authentication process will fail.
credentials.setUsername("Anonymous");
return configs; return configs;
} }
} }