122 lines
2.3 KiB
Java
122 lines
2.3 KiB
Java
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;
|
|
|
|
public class LocalCredentials {
|
|
|
|
/**
|
|
* Checks for already saved credentials, at least if its file exists
|
|
* @return <ul>
|
|
* <li> {@code true} a file exists </li>
|
|
* <li> {@code false} doesn't exist </li>
|
|
* </ul>
|
|
*/
|
|
|
|
private String path;
|
|
|
|
private File credentialsFile;
|
|
private File credentialsPath;
|
|
|
|
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;
|
|
}
|
|
|
|
public String[] getCredentialsFile()
|
|
{
|
|
String[] result = new String[2];
|
|
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"))
|
|
{
|
|
result[0] = currentValue[1];
|
|
}else if(currentValue[0].equals("pass"))
|
|
{
|
|
result[1] = currentValue[1];
|
|
}
|
|
}
|
|
|
|
reader.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
if(result[0] == null || result[1] == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public boolean saveCredentials(String[] 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("user=" + credentials[0]);
|
|
writer.append("\n");
|
|
writer.append("pass=" + credentials[1]);
|
|
|
|
writer.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|