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.

208 lines
5.7 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(String userName, String password){
  79. credentialRepository.createNewCredential(userName, password);
  80. }
  81. public void showCredential(String str) {
  82. if(str.matches("[0-9]+")) {
  83. println("Getting credential via ID");
  84. credentialRepository.getCredentialsViaId(Integer.parseInt(str));
  85. } else {
  86. println("Getting credential via name");
  87. credentialRepository.getCredentialsViaName(str);
  88. }
  89. }
  90. public void getAsJson(){
  91. }
  92. public void loadFromJson(){
  93. }
  94. public void setCapital(String input) {
  95. if(input.equals("yes")){
  96. haveCapitals = true;
  97. println("Your PWs contain now capitals.");
  98. }else if(input.equals("no")){
  99. haveCapitals = false;
  100. println("Your PWs don´t have capitals anymore.");
  101. }
  102. }
  103. public void setSpecialChar(String input) {
  104. if(input.equals("yes")){
  105. hasSpecialChars = true;
  106. println("Your PWs contain now special characters.");
  107. }else if(input.equals("no")){
  108. hasSpecialChars = false;
  109. println("Your PWs don´t have special characters anymore.");
  110. }
  111. }
  112. public void setNumbers(String input) {
  113. if(input.equals("yes")){
  114. hasNumbers = true;
  115. println("Your PWs contains now numbers.");
  116. }else if(input.equals("no")){
  117. hasNumbers = false;
  118. println("Your PWs don´t have numbers anymore.");
  119. }
  120. }
  121. public void credentialMenu() {
  122. StringBuilder sbcm = new StringBuilder();
  123. Scanner scan = new Scanner(inputS);
  124. credentialM = true;
  125. sbcm.append("Configure credentials:\n");
  126. sbcm.append("- add credential: a\n");
  127. sbcm.append("- show credential: c\n");
  128. sbcm.append("- edit credential: l\n");
  129. sbcm.append("- exit: e\n");
  130. println(sbcm.toString());
  131. String input = scan.nextLine();
  132. switch(input) {
  133. case "e":
  134. credentialM = false;
  135. break;
  136. case "a":
  137. println("Type in username");
  138. userName = scan.nextLine();
  139. println("Type in password");
  140. password = scan.nextLine();
  141. addCredential(userName, password);
  142. break;
  143. case "c":
  144. println("Type in ID or username");
  145. String str = scan.nextLine();
  146. showCredential(str);
  147. break;
  148. case "l":
  149. println("Type ID to select credential:");
  150. newID = Integer.parseInt(scan.nextLine());
  151. println("-change username: u\n-change password: p");
  152. input = scan.nextLine();
  153. if (!input.equals("u")) {
  154. decision = false;
  155. }
  156. println("Type new:");
  157. newString = scan.nextLine();
  158. //editCredential(newID, newString, decision);
  159. break;
  160. }
  161. }
  162. }