/src/fitnesse/authentication/Password.java

http://github.com/unclebob/fitnesse · Java · 113 lines · 92 code · 19 blank · 2 comment · 15 complexity · cc6211dc0413d0b0a0f9217ccfbaf0c2 MD5 · raw file

  1. // Copyright (C) 2003-2009 by Object Mentor, Inc. All rights reserved.
  2. // Released under the terms of the CPL Common Public License version 1.0.
  3. package fitnesse.authentication;
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6. public class Password {
  7. public static final String defaultFile = "passwords.txt";
  8. public static final String defaultCipher = "fitnesse.authentication.HashingCipher";
  9. private static BufferedReader input;
  10. private PasswordFile passwords;
  11. private String username;
  12. private String password;
  13. private PasswordCipher cipher;
  14. public static void main(String[] args) throws Exception {
  15. Password password = new Password();
  16. if (!password.args(args))
  17. printUsage();
  18. input = new BufferedReader(new InputStreamReader(System.in));
  19. password.interactForPassword();
  20. password.savePassword();
  21. System.out.println("password saved in " + password.passwords.getName());
  22. }
  23. public static void printUsage() {
  24. System.err.println("Usage: java fitnesse.authentication.Password [-f <password file>] [-c <password cipher>] <user>");
  25. System.err.println("\t-f <password file> {" + defaultFile + "}");
  26. System.err.println("\t-c <password cipher> {" + defaultCipher + "}");
  27. System.exit(-1);
  28. }
  29. public Password(String filename) throws Exception {
  30. cipher = new HashingCipher();
  31. passwords = new PasswordFile(filename, cipher);
  32. }
  33. public Password() throws Exception {
  34. this(defaultFile);
  35. }
  36. public void savePassword() throws Exception {
  37. passwords.savePassword(username, password);
  38. }
  39. public boolean args(String[] args) {
  40. if (args.length < 1 || args.length > 5)
  41. return false;
  42. try {
  43. boolean done = false;
  44. int argIndex = 0;
  45. while (!done) {
  46. if (args[argIndex].startsWith("-")) {
  47. if ("-f".equals(args[argIndex])) {
  48. passwords = new PasswordFile(args[argIndex + 1], cipher);
  49. argIndex += 2;
  50. } else if ("-c".equals(args[argIndex])) {
  51. cipher = passwords.instantiateCipher(args[argIndex + 1]);
  52. argIndex += 2;
  53. } else
  54. return false;
  55. } else {
  56. username = args[argIndex];
  57. done = true;
  58. }
  59. }
  60. return true;
  61. }
  62. catch (Exception e) {
  63. e.printStackTrace();
  64. return false;
  65. }
  66. }
  67. public String getUsername() {
  68. return username;
  69. }
  70. public String getFilename() {
  71. return passwords.getName();
  72. }
  73. public PasswordCipher getCipher() {
  74. return cipher;
  75. }
  76. private void interactForPassword() throws Exception {
  77. while (password == null) {
  78. System.out.println("Be advised, the password will be visible as it is typed.");
  79. System.out.print("enter password for " + username + ": ");
  80. String password1 = getUserEntry();
  81. System.out.print("confirm password: ");
  82. String password2 = getUserEntry();
  83. if (password1 != null && password1.equals(password2))
  84. password = password1;
  85. else {
  86. System.out.println("");
  87. System.out.println("passwords did not match");
  88. }
  89. }
  90. }
  91. private String getUserEntry() throws Exception {
  92. return input.readLine();
  93. }
  94. }