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.

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