package xyz.thastertyn.Login; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import xyz.thastertyn.Types.Credentials; public class LocalCredentials { private static LocalCredentials localCredentials = new LocalCredentials(); private String path; private File credentialsFile; private File credentialsPath; /** * Checks for already saved credentials, at least if its file exists
* for Windows it checks {@code AppData\Roaming\jecnak\credemtials.txt}
* for Linux it checks {@code ~/.local/share/jecnak/credentials.txt}
* @return * */ public boolean checkForExistingCredentials() { if(System.getProperty("os.name").equals("Linux")) { // /home/user/.local/share/jecnak/... path = System.getProperty("user.home") + "/.local/share/jecnak/"; }else if(System.getProperty("os.name").contains("Windows")) { // C:\Users\\user\AppData\Roaming\... path = System.getenv("APPDATA") + "\\jecnak\\"; }else{ return false; } credentialsPath = new File(path); credentialsFile = new File(credentialsPath, "credentials.txt"); if(!credentialsFile.exists()) { return false; } return true; } /** * Try reading the credentails from a local file * @return String[] with username on index 0, and password on index 1,
* can also return null if credentials are corrupted, inaccessible, etc. */ public Credentials getCredentialsFile() { // TODO Use hashmap instead of array String user = null; String pass = null; if(credentialsFile == null) { return null; } try { BufferedReader reader = new BufferedReader(new FileReader(credentialsFile)); String line = ""; while((line = reader.readLine()) != null) { String[] currentValue = line.split("="); if(currentValue[0].equals("user")) { user = currentValue[1]; }else if(currentValue[0].equals("pass")) { pass = currentValue[1]; } } reader.close(); } catch (IOException e) { e.printStackTrace(); return null; } if(user == null || pass == null) { return null; } return new Credentials(user, pass); } /** * appends the current credentials to the file * @param credentials the array containing username and password. Username is on index 0, password on index 1 * @return */ public boolean saveCredentials(Credentials credentials) { if(credentialsFile == null) { return false; } if(!credentialsFile.exists()) { try{ credentialsPath.mkdirs(); credentialsFile.createNewFile(); }catch(IOException e) { return false; } } try { BufferedWriter writer = new BufferedWriter(new FileWriter(credentialsFile)); writer.append(String.format( "user=%s\npass=%s\n", credentials.getUsername(), credentials.getPassword())); writer.close(); } catch (IOException e) { e.printStackTrace(); return false; } return true; } /** * Deletes the credentials file * @return */ public boolean deleteCredentials() { credentialsFile.delete(); return true; } /** * Returns a single instance to prevent creating and loading the files all over again when not much has changed * @return {@link LocalCredentials} instance */ public static LocalCredentials getInstance() { return localCredentials; } }