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

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 int newID;
public String newString;
public boolean decision;
InputStream inputS = System.in;
OutputStream outputS = System.out;
private CredentialRepository credentialRepository = new CredentialRepository();
private PasswordGenerator passwordGenerator = new PasswordGenerator();
/*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();
switch(input) {
case "e":
config = false;
break;
case "l":
println("Set PW length:");
String inputPw = scan.nextLine();
setPWLength(inputPw);
break;
case "h":
println("Should your PW have capitals? Type in yes or no.");
String inputC = scan.nextLine();
setCapital(inputC);
break;
case "s":
println("Should you PW have special characters? Type in yes or no.");
String inputS = scan.nextLine();
setSpecialChar(inputS);
break;
case "n":
println("Should you PW have numbers? Type in yes or no.");
String inputN = scan.nextLine();
setNumbers(inputN);
break;
}
}
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(){
Scanner scan = new Scanner(inputS);
println("Type in username");
userName = scan.nextLine();
println("Do you want to set your password manually?\nyes or no");
if(scan.nextLine().equals("yes")) {
println("Type in password");
password = scan.nextLine();
credentialRepository.createNewCredential(userName, password);
} else {
println("Generating password.");
password = passwordGenerator.generateRandomPassword();
credentialRepository.createNewCredential(userName, password);
}
}
public void showCredential() {
Scanner scan = new Scanner(inputS);
println("Type in ID or username");
String str = scan.nextLine();
Credential credential = null;
if(str.matches("[0-9]+")) {
println("Getting credential via ID");
credential = credentialRepository.getCredentialsViaId(Integer.parseInt(str));
} else {
println("Getting credential via name");
credential = credentialRepository.getCredentialsViaName(str);
}
if (credential != null) {
println(credential.getName() + ": " + credential.getPassword());
}
}
public void editCredential() {
Scanner scan = new Scanner(inputS);
println("Type ID to select credential:");
newID = Integer.parseInt(scan.nextLine());
println("-change username: u\n-change password: p");
String input = scan.nextLine();
if (input.equals("u")) {
decision = true;
}
if(decision){
println("Type new username:");
newString = scan.nextLine();
credentialRepository.updateUsername(newID, newString);
} else {
println("Type new password:");
newString = scan.nextLine();
credentialRepository.updatePassword(newID, newString);
}
}
public void listCredentials() {
for (Credential credential : credentialRepository.getCredentialList()) {
println(String.format("ID: %d - Username: %s", credential.getId(), credential.getName()));
}
}
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("- list credentials: s\n");
sbcm.append("- exit: e\n");
println(sbcm.toString());
String input = scan.nextLine();
switch(input) {
case "e":
credentialM = false;
break;
case "a":
println("Add credential:");
addCredential();
break;
case "c":
showCredential();
break;
case "l":
editCredential();
break;
case "s":
listCredentials();
break;
}
}
}