001: package org.esupportail.cas.server.handlers.file;
002:
003: import java.io.FileNotFoundException;
004:
005: import java.io.BufferedReader;
006: import java.io.FileReader;
007:
008: import org.dom4j.Element;
009: import org.esupportail.cas.server.util.BasicHandler;
010: import org.esupportail.cas.server.util.MisconfiguredHandlerException;
011: import org.esupportail.cas.server.util.crypt.Crypt;
012: import org.esupportail.cas.server.util.log.Log;
013:
014: /**
015: * The specific Handler for file.
016: *
017: * @author Pascal Aubry <pascal.aubry at univ-rennes1.fr>
018: * @author Jean-Baptiste Daniel <danielj at sourceforge.net>
019: */
020: public final class FileHandler extends BasicHandler {
021:
022: /**
023: * The filename to read.
024: */
025: private String filename;
026: /**
027: * The character used to sperate fields in the file.
028: */
029: private String separator;
030: /**
031: * the encryption used to store passwords.
032: */
033: private String encryption;
034:
035: /**
036: * Constructor.
037: *
038: * @param handlerElement the XML element that declares the handler
039: * in the configuration file
040: * @param configDebug debugging mode of the global configuration
041: * @throws Exception Exception
042: */
043: public FileHandler(final Element handlerElement,
044: final Boolean configDebug) throws Exception {
045: super (handlerElement, configDebug);
046: traceBegin();
047:
048: checkConfigElement(true);
049:
050: filename = getConfigSubElementContent("filename", true/*needed*/);
051: trace("filename = " + filename);
052:
053: encryption = getConfigSubElementContent("encryption", false/*not needed*/);
054: if (encryption.equals("")) {
055: encryption = "md5";
056: }
057: if (!Crypt.isEncryptionSupported(encryption)) {
058: traceThrow(new MisconfiguredHandlerException(
059: "Encryption \"" + encryption
060: + "\" is not supported."));
061: }
062: trace("encryption = " + encryption);
063: if (encryption.equals("plain")) {
064: Log.warn("Passwords should be encrypted. Be sure to keep "
065: + filename + " out of danger!");
066: }
067:
068: separator = getConfigSubElementContent("separator", false/*not needed*/);
069: trace("separator = " + separator);
070:
071: traceEnd();
072: }
073:
074: /**
075: * Try to authenticate a user (by searching into a file).
076: *
077: * @param username the user's name
078: * @param password the user's password
079: *
080: * @return BasicHandler.SUCCEDED on success, or
081: * BasicHandler.FAILED_CONTINUE otherwise.
082: */
083: public int authenticate(final String username, final String password) {
084: traceBegin();
085:
086: try {
087: trace("Opening file...");
088: FileReader fileReader = new FileReader(filename);
089: trace("Creating a buffer...");
090: BufferedReader buf = new BufferedReader(fileReader);
091:
092: String line;
093:
094: trace("Reading file...");
095: while ((line = buf.readLine()) != null) {
096: String[] lineFields = line.split(separator);
097:
098: // compare the username
099: if (lineFields[0].equals(username)) {
100:
101: trace("Username found, checking password ("
102: + encryption + ")...");
103: String encryptedPassword = lineFields[1];
104: boolean match = Crypt.match(encryption, password,
105: encryptedPassword);
106:
107: // function will return now (even if passwords do not match
108: buf.close();
109:
110: // compare the password
111: if (match) {
112: trace("Password matches.");
113: traceEnd("SUCCEEDED");
114: return SUCCEEDED;
115: } else {
116: trace("Password does not match.");
117: traceEnd("FAILED_CONTINUE");
118: return FAILED_CONTINUE;
119: }
120: }
121: }
122: buf.close();
123: trace("Username not found.");
124: } catch (FileNotFoundException e) {
125: Log.warn("File \"" + filename + "\" could not be loaded.");
126: trace("Failure: " + e.toString());
127: } catch (Exception e) {
128: trace("Failure: " + e.toString());
129: }
130: traceEnd("FAILED_CONTINUE");
131: return FAILED_CONTINUE;
132: }
133: }
|