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.*;
006:
007: public class Password {
008: public static String defaultFile = "passwords.txt";
009:
010: public static String defaultCipher = "fitnesse.authentication.HashingCipher";
011:
012: private static BufferedReader input;
013:
014: private PasswordFile passwords;
015:
016: private String username;
017:
018: private String password;
019:
020: private PasswordCipher cipher;
021:
022: public static void main(String[] args) throws Exception {
023: Password password = new Password();
024: if (!password.args(args))
025: printUsage();
026:
027: input = new BufferedReader(new InputStreamReader(System.in));
028: password.interactForPassword();
029:
030: password.savePassword();
031: System.out.println("password saved in "
032: + password.passwords.getName());
033: }
034:
035: public static void printUsage() {
036: System.err
037: .println("Usage: java fitnesse.authentication.Password [-f <password file>] [-c <password cipher>] <user>");
038: System.err
039: .println("\t-f <password file> {" + defaultFile + "}");
040: System.err.println("\t-c <password cipher> {" + defaultCipher
041: + "}");
042: System.exit(-1);
043: }
044:
045: public Password(String filename) throws Exception {
046: cipher = new HashingCipher();
047: passwords = new PasswordFile(filename, cipher);
048: }
049:
050: public Password() throws Exception {
051: this (defaultFile);
052: }
053:
054: public void savePassword() throws Exception {
055: passwords.savePassword(username, password);
056: }
057:
058: public boolean args(String[] args) {
059: if (args.length < 1 || args.length > 5)
060: return false;
061:
062: try {
063: boolean done = false;
064: int argIndex = 0;
065: while (!done) {
066: if (args[argIndex].startsWith("-")) {
067: if ("-f".equals(args[argIndex])) {
068: passwords = new PasswordFile(
069: args[argIndex + 1], cipher);
070: argIndex += 2;
071: } else if ("-c".equals(args[argIndex])) {
072: cipher = passwords
073: .instantiateCipher(args[argIndex + 1]);
074: argIndex += 2;
075: } else
076: return false;
077: } else {
078: username = args[argIndex];
079: done = true;
080: }
081: }
082: return true;
083: } catch (Exception e) {
084: e.printStackTrace();
085: return false;
086: }
087: }
088:
089: public String getUsername() {
090: return username;
091: }
092:
093: public String getFilename() {
094: return passwords.getName();
095: }
096:
097: public PasswordCipher getCipher() {
098: return cipher;
099: }
100:
101: private void interactForPassword() throws Exception {
102: while (password == null) {
103: System.out
104: .println("Be advised, the password will be visible as it is typed.");
105: System.out.print("enter password for " + username + ": ");
106: String password1 = getUserEntry();
107: System.out.print("confirm password: ");
108: String password2 = getUserEntry();
109:
110: if (password1 != null && password1.equals(password2))
111: password = password1;
112: else {
113: System.out.println("");
114: System.out.println("passwords did not match");
115: }
116: }
117: }
118:
119: private String getUserEntry() throws Exception {
120: return input.readLine();
121: }
122: }
|