Now the username and password can be given as program arguments with -u/--username and -p/--password

This commit is contained in:
Daniel Collin 2015-10-13 08:53:07 +00:00
parent e7f012dc5f
commit 45e0e3d8cb
3 changed files with 57 additions and 16 deletions

View file

@ -12,6 +12,7 @@ import com.coder.server.CoderServer;
import com.coder.server.message.CoderMessage;
import com.coder.server.message.ProjectListData;
import zutil.log.CompactLogFormatter;
import zutil.log.LogUtil;
public class CoderClient extends Thread{
@ -21,6 +22,8 @@ public class CoderClient extends Thread{
private int port;
private Session session;
private ProjectEditorWindow projectEditorWindow;
private String username = null;
private char[] password = null;
public CoderClient(String url, int port) {
this.url = url;
@ -28,12 +31,10 @@ public class CoderClient extends Thread{
this.projectEditorWindow = new ProjectEditorWindow("CoderClient");
super.start();
LogUtil.setGlobalFormatter(new CompactLogFormatter());
}
public void run(){
//save the previously typed user name
String username = "";
//keep track of the number of connection retries to the server
int connectionRetries = 0;
while(true){
@ -44,6 +45,7 @@ public class CoderClient extends Thread{
if(connectionRetries > 0){
logger.info("This is reconnection try number: " + connectionRetries);
}
logger.info("Setting up a connected session");
this.session = Session.setupConnection(url, port);
if(session == null){
logger.info("Could not setup a connection to " + url + ":" + port);
@ -64,18 +66,26 @@ public class CoderClient extends Thread{
}
}
//ask for username and password in a dialog window
LoginDialog loginDialog = new LoginDialog(username, null);
loginDialog.setVisible(true); //blocking
loginDialog.dispose();
if(loginDialog.getAction() == LoginDialogAction.CANCEL){
logger.info("Login dialog closed or canceled by the user. terminating.");
break;
//get user credentials
if(username != null && !username.isEmpty() && password != null && password.length > 0){
//do nothing, already have all credentials we need
logger.info("All login credentials have already been given. No need for a login dialog.");
}else{
//ask for username and password in a dialog window
logger.info("All or some login credentials have not been given. Using a ogin dialog for input.");
LoginDialog loginDialog = new LoginDialog(username, null);
loginDialog.setVisible(true); //blocking
loginDialog.dispose();
if(loginDialog.getAction() == LoginDialogAction.CANCEL){
logger.info("Login dialog closed or canceled by the user. terminating.");
break;
}
username = loginDialog.getUsername();
password = loginDialog.getPassword();
}
logger.info("The username: \"" + username + "\" will be used as the credential holder");
//authenticate the user
username = loginDialog.getUsername();
String password = new String(loginDialog.getPassword());
boolean authenticated = session.authenticate(username, password);
if(!authenticated){
JOptionPane.showMessageDialog(null, "Wrong username or password", "Authentication Failed", JOptionPane.INFORMATION_MESSAGE);
@ -133,9 +143,40 @@ public class CoderClient extends Thread{
}
}
private void setPassword(char[] password) {
this.password = password;
}
private void setUsername(String username) {
this.username = username;
}
public static void main(String[] args){
int port = CoderServer.SERVER_PORT;
new CoderClient("127.0.0.1", port);
CoderClient client = new CoderClient("127.0.0.1", port);
for(int i = 0; i < args.length; ++i){
String arg = args[i];
if(arg.equals("-u") || arg.equals("--username")){
i++;
if(i < args.length){
client.setUsername(args[i]);
}else{
System.err.println("missing username argument");
System.exit(1);
}
}else if(arg.equals("-p") || arg.equals("--password")){
i++;
if(i < args.length){
client.setPassword(args[i].toCharArray());
}else{
System.err.println("missing password argument");
System.exit(1);
}
}
}
client.start();
}
}

View file

@ -29,7 +29,7 @@ public class ProjectEditorWindow extends JFrame implements TreeSelectionListener
private JTextArea codingArea;
private JButton compileButton;
private JButton runButton;
private HashSet<GUIMessageSentListener> GUIMessageSentListeners;
private HashSet<GUIMessageSentListener> GUIMessageSentListeners = new HashSet<GUIMessageSentListener>();
public ProjectEditorWindow(String title){
super(title);

View file

@ -119,7 +119,7 @@ public class Session extends Thread {
return new String[]{};
}
public boolean authenticate(String username, String clearTextPassword) {
public boolean authenticate(String username, char[] clearTextPassword) {
logger.info("Authenticating session");
if(socket == null){
@ -163,7 +163,7 @@ public class Session extends Thread {
// Setting up encryption
/*
logger.info("Setting up encryption");
String hashedPassword = Hasher.PBKDF2(clearTextPassword, username, AUTH_HASH_ITERATIONS);
String hashedPassword = Hasher.PBKDF2(new String(clearTextPassword), username, AUTH_HASH_ITERATIONS);
String key = Hasher.PBKDF2(hashedPassword, msg.AuthenticationChallenge.salt, AUTH_HASH_ITERATIONS);
Encrypter crypto;
try {