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.

180 lines
5.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. InputStream inputS = System.in;
  16. OutputStream outputS = System.out;
  17. private CredentialRepository credentialRepository = new CredentialRepository();
  18. public static void main (String args[]) {
  19. Vault vault = new Vault();
  20. vault.credentialMenu();
  21. }
  22. private void println(String output) {
  23. try {
  24. outputS.write((output + "\n").getBytes(StandardCharsets.UTF_8));
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. public void configure(){
  30. StringBuilder sbcm = new StringBuilder();
  31. Scanner scan = new Scanner(inputS);
  32. config = true;
  33. sbcm.append("Configure:\n");
  34. sbcm.append("- Password length: l\n");
  35. sbcm.append("- Have capitals: h\n");
  36. sbcm.append("- Have special characters: s\n");
  37. sbcm.append("- Have Numbers: n\n");
  38. sbcm.append("- exit: e\n");
  39. println(sbcm.toString());
  40. String input = scan.nextLine();
  41. if (input.equals("e")) {
  42. config = false;
  43. } else if (input.equals("l")) {
  44. println("Set PW length:");
  45. String inputPw = scan.nextLine();
  46. setPWLength(inputPw);
  47. } else if (input.equals("h")) {
  48. println("Should your PW have capitals? Type in yes or no.");
  49. String inputC = scan.nextLine();
  50. setCapital(inputC);
  51. } else if (input.equals("s")){
  52. println("Should you PW have special characters? Type in yes or no.");
  53. String inputS = scan.nextLine();
  54. setSpecialChar(inputS);
  55. } else if (input.equals("n")) {
  56. println("Should you PW have numbers? Type in yes or no.");
  57. String inputN = scan.nextLine();
  58. setNumbers(inputN);
  59. }
  60. }
  61. public void setPWLength(String pwLength){
  62. try {
  63. PWLength = Integer.parseInt(pwLength);
  64. } catch (NumberFormatException e) {
  65. println("Please enter valid number.");
  66. }
  67. println("\nNew password length is now: ");
  68. }
  69. public void addCredential(String userName, String password){
  70. credentialRepository.createNewCredential(userName, password);
  71. }
  72. public void showCredential(String str){
  73. if(str.matches("[0-9]+")) {
  74. println("Getting credential via ID");
  75. credentialRepository.getCredentialsViaId(Integer.parseInt(str));
  76. } else {
  77. println("Getting credential via name");
  78. credentialRepository.getCredentialsViaName(str);
  79. }
  80. }
  81. public void getAsJson(){
  82. }
  83. public void loadFromJson(){
  84. }
  85. public void setCapital(String input) {
  86. if(input.equals("yes")){
  87. haveCapitals = true;
  88. println("Your PWs contain now capitals.");
  89. }else if(input.equals("no")){
  90. haveCapitals = false;
  91. println("Your PWs don´t have capitals anymore.");
  92. }
  93. }
  94. public void setSpecialChar(String input) {
  95. if(input.equals("yes")){
  96. hasSpecialChars = true;
  97. println("Your PWs contain now special characters.");
  98. }else if(input.equals("no")){
  99. hasSpecialChars = false;
  100. println("Your PWs don´t have special characters anymore.");
  101. }
  102. }
  103. public void setNumbers(String input) {
  104. if(input.equals("yes")){
  105. hasNumbers = true;
  106. println("Your PWs contains now numbers.");
  107. }else if(input.equals("no")){
  108. hasNumbers = false;
  109. println("Your PWs don´t have numbers anymore.");
  110. }
  111. }
  112. public void credentialMenu() {
  113. StringBuilder sbcm = new StringBuilder();
  114. Scanner scan = new Scanner(inputS);
  115. credentialM = true;
  116. sbcm.append("Configure credentials:\n");
  117. sbcm.append("- add credential: a\n");
  118. sbcm.append("- show credential: c\n");
  119. sbcm.append("- edit credential: l\n");
  120. sbcm.append("- exit: e\n");
  121. println(sbcm.toString());
  122. String input = scan.nextLine();
  123. if (input.equals("e")) {
  124. credentialM = false;
  125. }
  126. if (input.equals("a")) {
  127. println("Type in username");
  128. userName = scan.nextLine();
  129. println("Type in password");
  130. password = scan.nextLine();
  131. addCredential(userName, password);
  132. }
  133. if (input.equals("c")) {
  134. println("Type in ID or username");
  135. String str = scan.nextLine();
  136. showCredential(str);
  137. } else if (input.equals("l")) {
  138. println("Type username or ID to select credential:");
  139. }
  140. }
  141. }