001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.authentication;
004:
005: import java.io.File;
006: import java.lang.reflect.Constructor;
007: import java.util.HashMap;
008: import java.util.Iterator;
009: import java.util.LinkedList;
010: import java.util.List;
011: import java.util.Map;
012:
013: import fitnesse.util.FileUtil;
014:
015: public class PasswordFile {
016: private File passwordFile;
017:
018: private Map passwordMap = new HashMap();
019:
020: private PasswordCipher cipher = new TransparentCipher();
021:
022: public PasswordFile(String filename) throws Exception {
023: passwordFile = new File(filename);
024: loadFile();
025: }
026:
027: public PasswordFile(String filename, PasswordCipher cipher)
028: throws Exception {
029: this (filename);
030: this .cipher = cipher;
031: }
032:
033: public Map getPasswordMap() {
034: return passwordMap;
035: }
036:
037: public String getName() {
038: return passwordFile.getName();
039: }
040:
041: public PasswordCipher getCipher() {
042: return cipher;
043: }
044:
045: public void savePassword(String user, String password)
046: throws Exception {
047: passwordMap.put(user, cipher.encrypt(password));
048: savePasswords();
049: }
050:
051: private void loadFile() throws Exception {
052: LinkedList lines = getPasswordFileLines();
053: loadCipher(lines);
054: loadPasswords(lines);
055: }
056:
057: private void loadPasswords(LinkedList lines) {
058: for (Iterator iterator = lines.iterator(); iterator.hasNext();) {
059: String line = (String) iterator.next();
060: if (!"".equals(line)) {
061: String[] tokens = line.split(":");
062: passwordMap.put(tokens[0], tokens[1]);
063: }
064: }
065: }
066:
067: private void loadCipher(LinkedList lines) throws Exception {
068: if (lines.size() > 0) {
069: String firstLine = lines.getFirst().toString();
070: if (firstLine.startsWith("!")) {
071: String cipherClassName = firstLine.substring(1);
072: instantiateCipher(cipherClassName);
073: lines.removeFirst();
074: }
075: }
076: }
077:
078: public PasswordCipher instantiateCipher(String cipherClassName)
079: throws Exception {
080: Class cipherClass = Class.forName(cipherClassName);
081: Constructor constructor = cipherClass
082: .getConstructor(new Class[] {});
083: cipher = (PasswordCipher) constructor
084: .newInstance(new Object[] {});
085: return cipher;
086: }
087:
088: private void savePasswords() throws Exception {
089: List lines = new LinkedList();
090: lines.add("!" + cipher.getClass().getName());
091: for (Iterator iterator = passwordMap.keySet().iterator(); iterator
092: .hasNext();) {
093: Object user = iterator.next();
094: Object password = passwordMap.get(user);
095: lines.add(user + ":" + password);
096: }
097: FileUtil.writeLinesToFile(passwordFile, lines);
098: }
099:
100: private LinkedList getPasswordFileLines() throws Exception {
101: LinkedList lines = new LinkedList();
102: if (passwordFile.exists())
103: lines = FileUtil.getFileLines(passwordFile);
104: return lines;
105: }
106: }
|