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.

196 lines
5.5 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. if (input.equals("e")) {
  45. config = false;
  46. } else if (input.equals("l")) {
  47. println("Set PW length:");
  48. String inputPw = scan.nextLine();
  49. setPWLength(inputPw);
  50. } else if (input.equals("h")) {
  51. println("Should your PW have capitals? Type in yes or no.");
  52. String inputC = scan.nextLine();
  53. setCapital(inputC);
  54. } else if (input.equals("s")){
  55. println("Should you PW have special characters? Type in yes or no.");
  56. String inputS = scan.nextLine();
  57. setSpecialChar(inputS);
  58. } else if (input.equals("n")) {
  59. println("Should you PW have numbers? Type in yes or no.");
  60. String inputN = scan.nextLine();
  61. setNumbers(inputN);
  62. }
  63. }
  64. public void setPWLength(String pwLength) {
  65. try {
  66. PWLength = Integer.parseInt(pwLength);
  67. } catch (NumberFormatException e) {
  68. println("Please enter valid number.");
  69. }
  70. println("\nNew password length is now: ");
  71. }
  72. public void addCredential(String userName, String password){
  73. credentialRepository.createNewCredential(userName, password);
  74. }
  75. public void showCredential(String str) {
  76. if(str.matches("[0-9]+")) {
  77. println("Getting credential via ID");
  78. credentialRepository.getCredentialsViaId(Integer.parseInt(str));
  79. } else {
  80. println("Getting credential via name");
  81. credentialRepository.getCredentialsViaName(str);
  82. }
  83. }
  84. public void getAsJson(){
  85. }
  86. public void loadFromJson(){
  87. }
  88. public void setCapital(String input) {
  89. if(input.equals("yes")){
  90. haveCapitals = true;
  91. println("Your PWs contain now capitals.");
  92. }else if(input.equals("no")){
  93. haveCapitals = false;
  94. println("Your PWs don´t have capitals anymore.");
  95. }
  96. }
  97. public void setSpecialChar(String input) {
  98. if(input.equals("yes")){
  99. hasSpecialChars = true;
  100. println("Your PWs contain now special characters.");
  101. }else if(input.equals("no")){
  102. hasSpecialChars = false;
  103. println("Your PWs don´t have special characters anymore.");
  104. }
  105. }
  106. public void setNumbers(String input) {
  107. if(input.equals("yes")){
  108. hasNumbers = true;
  109. println("Your PWs contains now numbers.");
  110. }else if(input.equals("no")){
  111. hasNumbers = false;
  112. println("Your PWs don´t have numbers anymore.");
  113. }
  114. }
  115. public void credentialMenu() {
  116. StringBuilder sbcm = new StringBuilder();
  117. Scanner scan = new Scanner(inputS);
  118. credentialM = true;
  119. sbcm.append("Configure credentials:\n");
  120. sbcm.append("- add credential: a\n");
  121. sbcm.append("- show credential: c\n");
  122. sbcm.append("- edit credential: l\n");
  123. sbcm.append("- exit: e\n");
  124. println(sbcm.toString());
  125. String input = scan.nextLine();
  126. if (input.equals("e")) {
  127. credentialM = false;
  128. }
  129. if (input.equals("a")) {
  130. println("Type in username");
  131. userName = scan.nextLine();
  132. println("Type in password");
  133. password = scan.nextLine();
  134. addCredential(userName, password);
  135. }
  136. if (input.equals("c")) {
  137. println("Type in ID or username");
  138. String str = scan.nextLine();
  139. showCredential(str);
  140. } else if (input.equals("l")) {
  141. println("Type ID to select credential:");
  142. newID = Integer.parseInt(scan.nextLine());
  143. println("-change username: u\n-change password: p");
  144. input = scan.nextLine();
  145. if (input.equals("u")) {
  146. decision = true;
  147. }else{
  148. decision = false;
  149. }
  150. println("Type new:");
  151. newString = scan.nextLine();
  152. //editCredential(newID, newString, decision);
  153. }
  154. }
  155. }