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.

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