1 package base.jdbs.network.util;
2
3 import net.jxta.credential.AuthenticationCredential;
4 import net.jxta.exception.PeerGroupException;
5 import net.jxta.exception.ProtocolNotSupportedException;
6 import net.jxta.impl.membership.pse.StringAuthenticator;
7 import net.jxta.membership.MembershipService;
8 import net.jxta.peergroup.PeerGroup;
9
10 import org.apache.log4j.Logger;
11
12 import base.InternetCafe;
13
14 public class JxtaAuthenticationUtil {
15
16 private static final transient Logger logger = Logger.getLogger(JxtaAuthenticationUtil.class.getName());
17
18 private static final String STRING_AUTHENTICATOR = "StringAuthentication";
19 private static final String AUTHENTICATION = STRING_AUTHENTICATOR;
20
21 public static boolean authenticate(PeerGroup peerGroup, String keyStorePassword, String identityPassword, boolean join) {
22 InternetCafe.setStatusBarMessage("Authenticating "+peerGroup.getPeerName()+" in group "+peerGroup.getPeerGroupName()+"...");
23 boolean isAuthenticated = isAuthenticated(peerGroup);
24
25
26
27 String pwd = keyStorePassword;
28
29 if (pwd != null) {
30 if (keyStorePassword == null ||
31 keyStorePassword.trim().length() == 0) {
32 keyStorePassword = pwd;
33 }
34
35 if (identityPassword == null ||
36 identityPassword.trim().length() == 0) {
37 identityPassword = pwd;
38 }
39 }
40
41 if (! isAuthenticated) {
42 if (keyStorePassword != null && identityPassword != null) {
43 MembershipService ms = peerGroup.getMembershipService();
44 AuthenticationCredential ac = new AuthenticationCredential(peerGroup, AUTHENTICATION, null);
45 StringAuthenticator sa = null;
46
47 try {
48 sa = (StringAuthenticator)ms.apply(ac);
49 } catch (ProtocolNotSupportedException pnse) {
50 logger.fatal(pnse.getMessage());
51 pnse.printStackTrace();
52 System.exit(-1);
53
54 } catch (PeerGroupException pge) {
55 logger.fatal(pge.getMessage());
56 pge.printStackTrace();
57 System.exit(-1);
58 }
59
60 sa.setAuth1_KeyStorePassword(keyStorePassword);
61 sa.setAuth2Identity(peerGroup.getPeerID());
62 sa.setAuth3_IdentityPassword(identityPassword);
63
64 isAuthenticated = sa.isReadyForJoin();
65 if(isAuthenticated){
66 InternetCafe.setStatusBarMessage("Successfully Authenticated...");
67 }
68
69 if (isAuthenticated && join) {
70 try {
71 ms.join(sa);
72 } catch (PeerGroupException pge) {
73
74 logger.fatal("can't join "+peerGroup.getPeerGroupName());
75 pge.printStackTrace();
76 System.exit(-1);
77 }
78 }
79 }
80 }else logger.info("Peer "+peerGroup.getPeerName()+ " was already authenticated in group "+peerGroup.getPeerGroupName()+"...");
81
82 return isAuthenticated;
83 }
84
85 /***
86 * This method is helpful to check if we are authenticated in a PeerGroup.
87 * @param peerGroup The PeerGroup where our authentication must be ckecked.
88 * @return true if we are authenticated in the PeerGroup passed as formal
89 * parameter, false otherwise.
90 */
91 private static boolean isAuthenticated(PeerGroup peerGroup) {
92 boolean result = false;
93 logger.info("Checking authentication in : "+peerGroup.getPeerGroupName());
94 try {
95 result = (peerGroup.getMembershipService().getDefaultCredential() != null);
96 } catch (PeerGroupException pge) {
97 pge.printStackTrace();
98 logger.info("no default credential");
99 }
100 logger.info("is authenticated: " + peerGroup.getPeerGroupName() +" " + result);
101 return result;
102 }
103 }