package include.auth;
import javax.naming.*;
import village.webservice.VillageServiceInterfaceProxy;
public class Authentication {
/**
* Function checks if login and password are correct and creates session.
*
* @param login user's login
* @param password user's password
* @return session identifier or null
*/
public String logon (String login, String password){
database.Account acc;
database.Authentication auth = null;
database.Session ses = null;
String name;
String server;
String result = null;
Integer int_result;
include.net.Server serv = new include.net.Server();
System.out.println("[Authentication] Executing function 'logon'. Parameters:");
System.out.println("[Authentication] login: '" + login + "'");
System.out.println("[Authentication] password: '" + password + "'");
if (login.indexOf('@') == -1) {
System.out.println("[Authentication] Local execution.");
try {
InitialContext ctx = new InitialContext();
auth = (database.Authentication) ctx.lookup("ear3/CMPAuthentication/local");
ses = (database.Session) ctx.lookup("ear3/CMPSession/local");
} catch (NamingException e) {
e.printStackTrace();
}
acc = new database.Account(login, password);
if(auth != null && auth.check(acc)) {
if (ses.getId(login) > -1)
ses.remove(ses.getId(login));
int_result = ((Integer) ses.add(login));
if (int_result != -1)
result = (int_result.toString()+"@"+serv.giveName());
}
} else {
name = login.substring(0, login.indexOf('@'));
server = login.substring(login.indexOf('@') + 1);
if (serv.isMyName(server)) {
result = this.logon(name, password);
} else {
System.out.println("[Authentication] Remote execution.");
VillageServiceInterfaceProxy prox = new VillageServiceInterfaceProxy();
village.webservice.VillageServiceInterface iface =
prox.setEndpoint("http://"+server+prox.villagePort+prox.villageSuffix);
try {
result = iface.login(login, password);
} catch(Exception cex){
cex.printStackTrace();
}
}
}
System.out.println("[Authentication] Function 'logon' returned value: '" + result + "'");
return result;
}
}