1 20 21 package org.jivesoftware.smack.sasl; 22 23 import org.jivesoftware.smack.SASLAuthentication; 24 import org.jivesoftware.smack.util.StringUtils; 25 26 import java.io.IOException ; 27 28 39 public abstract class SASLMechanism { 40 41 private SASLAuthentication saslAuthentication; 42 43 public SASLMechanism(SASLAuthentication saslAuthentication) { 44 super(); 45 this.saslAuthentication = saslAuthentication; 46 } 47 48 56 public void authenticate(String username, String host, String password) throws IOException { 57 StringBuffer stanza = new StringBuffer (); 59 stanza.append("<auth mechanism=\"").append(getName()); 60 stanza.append("\" xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">"); 61 String authenticationText = getAuthenticationText(username, host, password); 62 if (authenticationText != null) { 63 stanza.append(StringUtils.encodeBase64(authenticationText)); 64 } 65 stanza.append("</auth>"); 66 67 getSASLAuthentication().send(stanza.toString()); 69 } 70 71 77 public void challengeReceived(String challenge) throws IOException { 78 StringBuffer stanza = new StringBuffer (); 80 stanza.append("<response xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">"); 81 String authenticationText = getChallengeResponse(StringUtils.decodeBase64(challenge)); 82 stanza.append(StringUtils.encodeBase64(authenticationText)); 83 stanza.append("</response>"); 84 85 getSASLAuthentication().send(stanza.toString()); 87 } 88 89 96 protected abstract String getChallengeResponse(byte[] bytes); 97 98 103 protected abstract String getName(); 104 105 115 protected abstract String getAuthenticationText(String username, String host, String password); 116 117 protected SASLAuthentication getSASLAuthentication() { 118 return saslAuthentication; 119 } 120 } 121 | Popular Tags |