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.

253 lines
6.9 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 listCredentials() {
  130. for (Credential credential : credentialRepository.getCredentialList()) {
  131. println(String.format("ID: %d - Username: %s", credential.getId(), credential.getName()));
  132. }
  133. }
  134. public void getAsJson(){
  135. }
  136. public void loadFromJson(){
  137. }
  138. public void setCapital(String input) {
  139. if(input.equals("yes")){
  140. haveCapitals = true;
  141. println("Your PWs contain now capitals.");
  142. }else if(input.equals("no")){
  143. haveCapitals = false;
  144. println("Your PWs don´t have capitals anymore.");
  145. }
  146. }
  147. public void setSpecialChar(String input) {
  148. if(input.equals("yes")){
  149. hasSpecialChars = true;
  150. println("Your PWs contain now special characters.");
  151. }else if(input.equals("no")){
  152. hasSpecialChars = false;
  153. println("Your PWs don´t have special characters anymore.");
  154. }
  155. }
  156. public void setNumbers(String input) {
  157. if(input.equals("yes")){
  158. hasNumbers = true;
  159. println("Your PWs contains now numbers.");
  160. }else if(input.equals("no")){
  161. hasNumbers = false;
  162. println("Your PWs don´t have numbers anymore.");
  163. }
  164. }
  165. public void credentialMenu() {
  166. StringBuilder sbcm = new StringBuilder();
  167. Scanner scan = new Scanner(inputS);
  168. credentialM = true;
  169. sbcm.append("Configure credentials:\n");
  170. sbcm.append("- add credential: a\n");
  171. sbcm.append("- show credential: c\n");
  172. sbcm.append("- edit credential: l\n");
  173. sbcm.append("- list credentials: s\n");
  174. sbcm.append("- exit: e\n");
  175. println(sbcm.toString());
  176. String input = scan.nextLine();
  177. switch(input) {
  178. case "e":
  179. credentialM = false;
  180. break;
  181. case "a":
  182. println("Add credential:");
  183. addCredential();
  184. break;
  185. case "c":
  186. showCredential();
  187. break;
  188. case "l":
  189. editCredential();
  190. break;
  191. case "s":
  192. listCredentials();
  193. break;
  194. }
  195. }
  196. }