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.

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