| Line | Hits | Source |
|---|---|---|
| 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 | 0 | public class JxtaAuthenticationUtil { |
| 15 | ||
| 16 | 0 | 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 | 0 | InternetCafe.setStatusBarMessage("Authenticating "+peerGroup.getPeerName()+" in group "+peerGroup.getPeerGroupName()+"..."); |
| 23 | 0 | boolean isAuthenticated = isAuthenticated(peerGroup); |
| 24 | // xxx: assumption that ks pwd == id pwd | |
| 25 | //String pwd = v.getConfig() != null ? v.getConfig().getPassword() : null; | |
| 26 | ||
| 27 | 0 | String pwd = keyStorePassword; |
| 28 | ||
| 29 | 0 | if (pwd != null) { |
| 30 | 0 | if (keyStorePassword == null || |
| 31 | 0 | keyStorePassword.trim().length() == 0) { |
| 32 | 0 | keyStorePassword = pwd; |
| 33 | } | |
| 34 | ||
| 35 | 0 | if (identityPassword == null || |
| 36 | 0 | identityPassword.trim().length() == 0) { |
| 37 | 0 | identityPassword = pwd; |
| 38 | } | |
| 39 | } | |
| 40 | ||
| 41 | 0 | if (! isAuthenticated) { |
| 42 | 0 | if (keyStorePassword != null && identityPassword != null) { |
| 43 | 0 | MembershipService ms = peerGroup.getMembershipService(); |
| 44 | 0 | AuthenticationCredential ac = new AuthenticationCredential(peerGroup, AUTHENTICATION, null); |
| 45 | 0 | StringAuthenticator sa = null; |
| 46 | ||
| 47 | try { | |
| 48 | 0 | sa = (StringAuthenticator)ms.apply(ac); |
| 49 | 0 | } catch (ProtocolNotSupportedException pnse) { |
| 50 | 0 | logger.fatal(pnse.getMessage()); |
| 51 | 0 | pnse.printStackTrace(); |
| 52 | 0 | System.exit(-1); |
| 53 | ||
| 54 | 0 | } catch (PeerGroupException pge) { |
| 55 | 0 | logger.fatal(pge.getMessage()); |
| 56 | 0 | pge.printStackTrace(); |
| 57 | 0 | System.exit(-1); |
| 58 | 0 | } |
| 59 | ||
| 60 | 0 | sa.setAuth1_KeyStorePassword(keyStorePassword); |
| 61 | 0 | sa.setAuth2Identity(peerGroup.getPeerID()); |
| 62 | 0 | sa.setAuth3_IdentityPassword(identityPassword); |
| 63 | ||
| 64 | 0 | isAuthenticated = sa.isReadyForJoin(); |
| 65 | 0 | if(isAuthenticated){ |
| 66 | 0 | InternetCafe.setStatusBarMessage("Successfully Authenticated..."); |
| 67 | } | |
| 68 | ||
| 69 | 0 | if (isAuthenticated && join) { |
| 70 | try { | |
| 71 | 0 | ms.join(sa); |
| 72 | 0 | } catch (PeerGroupException pge) { |
| 73 | ||
| 74 | 0 | logger.fatal("can't join "+peerGroup.getPeerGroupName()); |
| 75 | 0 | pge.printStackTrace(); |
| 76 | 0 | System.exit(-1); |
| 77 | 0 | } |
| 78 | } | |
| 79 | 0 | } |
| 80 | 0 | }else logger.info("Peer "+peerGroup.getPeerName()+ " was already authenticated in group "+peerGroup.getPeerGroupName()+"..."); |
| 81 | ||
| 82 | 0 | 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 | 0 | boolean result = false; |
| 93 | 0 | logger.info("Checking authentication in : "+peerGroup.getPeerGroupName()); |
| 94 | try { | |
| 95 | 0 | result = (peerGroup.getMembershipService().getDefaultCredential() != null); |
| 96 | 0 | } catch (PeerGroupException pge) { |
| 97 | 0 | pge.printStackTrace(); |
| 98 | 0 | logger.info("no default credential"); |
| 99 | 0 | } |
| 100 | 0 | logger.info("is authenticated: " + peerGroup.getPeerGroupName() +" " + result); |
| 101 | 0 | return result; |
| 102 | } | |
| 103 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |