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.

178 lines
5.1 KiB

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public class Vault implements VaultInterface {
public boolean config;
public int PWLength;
public boolean haveCapitals;
public boolean hasSpecialChars;
public boolean hasNumbers;
public boolean credentialM;
public String userName;
public String password;
public boolean isInt = false;
InputStream inputS = System.in;
OutputStream outputS = System.out;
private CredentialRepository credentialRepository = new CredentialRepository();
public static void main (String args[]) {
Vault vault = new Vault();
vault.credentialMenu();
}
private void println(String output) {
try {
outputS.write((output + "\n").getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
e.printStackTrace();
}
}
public void configure(){
StringBuilder sbcm = new StringBuilder();
Scanner scan = new Scanner(inputS);
config = true;
sbcm.append("Configure:\n");
sbcm.append("- Password length: l\n");
sbcm.append("- Have capitals: h\n");
sbcm.append("- Have special characters: s\n");
sbcm.append("- Have Numbers: n\n");
sbcm.append("- exit: e\n");
println(sbcm.toString());
String input = scan.nextLine();
if (input.equals("e")) {
config = false;
} else if (input.equals("l")) {
println("Set PW length:");
String inputPw = scan.nextLine();
setPWLength(inputPw);
} else if (input.equals("h")) {
println("Should your PW have capitals? Type in yes or no.");
String inputC = scan.nextLine();
setCapital(inputC);
} else if (input.equals("s")){
println("Should you PW have special characters? Type in yes or no.");
String inputS = scan.nextLine();
setSpecialChar(inputS);
} else if (input.equals("n")) {
println("Should you PW have numbers? Type in yes or no.");
String inputN = scan.nextLine();
setNumbers(inputN);
}
}
public void setPWLength(String pwLength){
try {
PWLength = Integer.parseInt(pwLength);
} catch (NumberFormatException e) {
println("Please enter valid number.");
}
println("\nNew password length is now: ");
}
public void addCredential(String userName, String password){
credentialRepository.createNewCredential(userName, password);
}
public void showCredential(){
Scanner scan = new Scanner(inputS);
println("Type in ID or username");
String str = scan.nextLine();
if(str.matches("[0-9]+")) {
isInt = true;
credentialRepository.getCredentialsViaId(Integer.parseInt(str));
} else {
isInt = false;
credentialRepository.getCredentialsViaName(str);
}
}
public void getAsJson(){
}
public void loadFromJson(){
}
public void setCapital(String input) {
if(input.equals("yes")){
haveCapitals = true;
println("Your PWs contain now capitals.");
}else if(input.equals("no")){
haveCapitals = false;
println("Your PWs don´t have capitals anymore.");
}
}
public void setSpecialChar(String input) {
if(input.equals("yes")){
hasSpecialChars = true;
println("Your PWs contain now special characters.");
}else if(input.equals("no")){
hasSpecialChars = false;
println("Your PWs don´t have special characters anymore.");
}
}
public void setNumbers(String input) {
if(input.equals("yes")){
hasNumbers = true;
println("Your PWs contains now numbers.");
}else if(input.equals("no")){
hasNumbers = false;
println("Your PWs don´t have numbers anymore.");
}
}
public void credentialMenu() {
StringBuilder sbcm = new StringBuilder();
Scanner scan = new Scanner(inputS);
credentialM = true;
sbcm.append("Configure credentials:\n");
sbcm.append("- add credential: a\n");
sbcm.append("- show credential: c\n");
sbcm.append("- edit credential: l\n");
sbcm.append("- exit: e\n");
println(sbcm.toString());
String input = scan.nextLine();
if (input.equals("e")) {
credentialM = false;
} else if (input.equals("a")) {
println("Type in username");
userName = scan.nextLine();
println("Type in password");
password = scan.nextLine();
addCredential(userName, password);
} else if (input.equals("c")) {
//println("Type username or ID to show credential:");
showCredential();
} else if (input.equals("l")) {
println("Type username or ID to select credential:");
}
}
}