You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
6.6 KiB

2 years ago
2 years ago
2 years ago
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.nio.charset.StandardCharsets;
  5. import java.util.Scanner;
  6. public class Vault implements VaultInterface {
  7. public boolean config;
  8. public int PWLength;
  9. public boolean haveCapitals;
  10. public boolean hasSpecialChars;
  11. public boolean hasNumbers;
  12. public boolean credentialM;
  13. public String userName;
  14. public String password;
  15. public int newID;
  16. public String newString;
  17. public boolean decision;
  18. InputStream inputS = System.in;
  19. OutputStream outputS = System.out;
  20. private CredentialRepository credentialRepository = new CredentialRepository();
  21. private PasswordGenerator passwordGenerator = new PasswordGenerator();
  22. /*public static void main (String args[]) {
  23. Vault vault = new Vault();
  24. vault.credentialMenu();
  25. }*/
  26. private void println(String output) {
  27. try {
  28. outputS.write((output + "\n").getBytes(StandardCharsets.UTF_8));
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. public void configure(){
  34. StringBuilder sbcm = new StringBuilder();
  35. Scanner scan = new Scanner(inputS);
  36. config = true;
  37. sbcm.append("Configure:\n");
  38. sbcm.append("- Password length: l\n");
  39. sbcm.append("- Have capitals: h\n");
  40. sbcm.append("- Have special characters: s\n");
  41. sbcm.append("- Have Numbers: n\n");
  42. sbcm.append("- exit: e\n");
  43. println(sbcm.toString());
  44. String input = scan.nextLine();
  45. switch(input) {
  46. case "e":
  47. config = false;
  48. break;
  49. case "l":
  50. println("Set PW length:");
  51. String inputPw = scan.nextLine();
  52. setPWLength(inputPw);
  53. break;
  54. case "h":
  55. println("Should your PW have capitals? Type in yes or no.");
  56. String inputC = scan.nextLine();
  57. setCapital(inputC);
  58. break;
  59. case "s":
  60. println("Should you PW have special characters? Type in yes or no.");
  61. String inputS = scan.nextLine();
  62. setSpecialChar(inputS);
  63. break;
  64. case "n":
  65. println("Should you PW have numbers? Type in yes or no.");
  66. String inputN = scan.nextLine();
  67. setNumbers(inputN);
  68. break;
  69. }
  70. }
  71. public void setPWLength(String pwLength) {
  72. try {
  73. PWLength = Integer.parseInt(pwLength);
  74. } catch (NumberFormatException e) {
  75. println("Please enter valid number.");
  76. }
  77. println("\nNew password length is now: ");
  78. }
  79. public void addCredential(){
  80. Scanner scan = new Scanner(inputS);
  81. println("Type in username");
  82. userName = scan.nextLine();
  83. println("Do you want to set your password manually?\nyes or no");
  84. if(scan.nextLine().equals("yes")) {
  85. println("Type in password");
  86. password = scan.nextLine();
  87. credentialRepository.createNewCredential(userName, password);
  88. } else {
  89. println("Generating password.");
  90. password = passwordGenerator.generateRandomPassword();
  91. credentialRepository.createNewCredential(userName, password);
  92. }
  93. }
  94. public void showCredential() {
  95. Scanner scan = new Scanner(inputS);
  96. println("Type in ID or username");
  97. String str = scan.nextLine();
  98. Credential credential = null;
  99. if(str.matches("[0-9]+")) {
  100. println("Getting credential via ID");
  101. credential = credentialRepository.getCredentialsViaId(Integer.parseInt(str));
  102. } else {
  103. println("Getting credential via name");
  104. credential = credentialRepository.getCredentialsViaName(str);
  105. }
  106. if (credential != null) {
  107. println(credential.getName() + ": " + credential.getPassword());
  108. }
  109. }
  110. public void editCredential() {
  111. Scanner scan = new Scanner(inputS);
  112. println("Type ID to select credential:");
  113. newID = Integer.parseInt(scan.nextLine());
  114. println("-change username: u\n-change password: p");
  115. String input = scan.nextLine();
  116. if (input.equals("u")) {
  117. decision = true;
  118. }
  119. if(decision){
  120. println("Type new username:");
  121. newString = scan.nextLine();
  122. credentialRepository.updateUsername(newID, newString);
  123. } else {
  124. println("Type new password:");
  125. newString = scan.nextLine();
  126. credentialRepository.updatePassword(newID, newString);
  127. }
  128. }
  129. public void getAsJson(){
  130. }
  131. public void loadFromJson(){
  132. }
  133. public void setCapital(String input) {
  134. if(input.equals("yes")){
  135. haveCapitals = true;
  136. println("Your PWs contain now capitals.");
  137. }else if(input.equals("no")){
  138. haveCapitals = false;
  139. println("Your PWs don´t have capitals anymore.");
  140. }
  141. }
  142. public void setSpecialChar(String input) {
  143. if(input.equals("yes")){
  144. hasSpecialChars = true;
  145. println("Your PWs contain now special characters.");
  146. }else if(input.equals("no")){
  147. hasSpecialChars = false;
  148. println("Your PWs don´t have special characters anymore.");
  149. }
  150. }
  151. public void setNumbers(String input) {
  152. if(input.equals("yes")){
  153. hasNumbers = true;
  154. println("Your PWs contains now numbers.");
  155. }else if(input.equals("no")){
  156. hasNumbers = false;
  157. println("Your PWs don´t have numbers anymore.");
  158. }
  159. }
  160. public void credentialMenu() {
  161. StringBuilder sbcm = new StringBuilder();
  162. Scanner scan = new Scanner(inputS);
  163. credentialM = true;
  164. sbcm.append("Configure credentials:\n");
  165. sbcm.append("- add credential: a\n");
  166. sbcm.append("- show credential: c\n");
  167. sbcm.append("- edit credential: l\n");
  168. sbcm.append("- exit: e\n");
  169. println(sbcm.toString());
  170. String input = scan.nextLine();
  171. switch(input) {
  172. case "e":
  173. credentialM = false;
  174. break;
  175. case "a":
  176. println("Add credential:");
  177. addCredential();
  178. break;
  179. case "c":
  180. showCredential();
  181. break;
  182. case "l":
  183. editCredential();
  184. break;
  185. }
  186. }
  187. }