1 20 21 package org.jivesoftware.smack; 22 23 import org.jivesoftware.smack.filter.PacketIDFilter; 24 import org.jivesoftware.smack.packet.Authentication; 25 import org.jivesoftware.smack.packet.IQ; 26 27 34 class NonSASLAuthentication implements UserAuthentication { 35 36 private XMPPConnection connection; 37 38 public NonSASLAuthentication(XMPPConnection connection) { 39 super(); 40 this.connection = connection; 41 } 42 43 public String authenticate(String username, String password, String resource) throws 44 XMPPException { 45 Authentication discoveryAuth = new Authentication(); 48 discoveryAuth.setType(IQ.Type.GET); 49 discoveryAuth.setUsername(username); 50 51 PacketCollector collector = 52 connection.createPacketCollector(new PacketIDFilter(discoveryAuth.getPacketID())); 53 connection.sendPacket(discoveryAuth); 55 IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); 57 if (response == null) { 58 throw new XMPPException("No response from the server."); 59 } 60 else if (response.getType() == IQ.Type.ERROR) { 62 throw new XMPPException(response.getError()); 63 } 64 Authentication authTypes = (Authentication) response; 66 collector.cancel(); 67 68 Authentication auth = new Authentication(); 70 auth.setUsername(username); 71 72 if (authTypes.getDigest() != null) { 74 auth.setDigest(connection.getConnectionID(), password); 75 } 76 else if (authTypes.getPassword() != null) { 77 auth.setPassword(password); 78 } 79 else { 80 throw new XMPPException("Server does not support compatible authentication mechanism."); 81 } 82 83 auth.setResource(resource); 84 85 collector = connection.createPacketCollector(new PacketIDFilter(auth.getPacketID())); 86 connection.sendPacket(auth); 88 response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); 90 if (response == null) { 91 throw new XMPPException("Authentication failed."); 92 } 93 else if (response.getType() == IQ.Type.ERROR) { 94 throw new XMPPException(response.getError()); 95 } 96 collector.cancel(); 98 99 return response.getTo(); 100 } 101 } 102 | Popular Tags |