powered by Jive Software

Facebook chat using java, smack and PLAIN authentication

Hi,

I am trying to develope a simple chat application in java for facebook chat.

I am trying to authenticate using the PLAIN mechanism but getting an error:

SASL authentication PLAIN failed: not-authorized:

Here’s the code I am using:

import java.io.IOException;
import java.util.HashMap;
import javax.security.auth.callback.CallbackHandler;
import javax.security.sasl.Sasl;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.sasl.SASLMechanism;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.Base64; public class MySASLDigestMD5Mechanism extends SASLMechanism
{     public MySASLDigestMD5Mechanism(SASLAuthentication saslAuthentication)
    {
        super(saslAuthentication);
    }     protected void authenticate()
        throws IOException, XMPPException
    {
        String mechanisms[] = {
            getName()
        };
        java.util.Map props = new HashMap();
        sc = Sasl.createSaslClient(mechanisms, null, "xmpp", hostname, props, this);
        super.authenticate();
    }     public void authenticate(String username, String host, String password)
        throws IOException, XMPPException
    {
        authenticationId = username;
        this.password = password;
        hostname = host;
        String mechanisms[] = {
            getName()
        };
        java.util.Map props = new HashMap();
        sc = Sasl.createSaslClient(mechanisms, null, "xmpp", host, props, this);
        super.authenticate();
    }     public void authenticate(String username, String host, CallbackHandler cbh)
        throws IOException, XMPPException
    {
        String mechanisms[] = {
            getName()
        };
        java.util.Map props = new HashMap();
        sc = Sasl.createSaslClient(mechanisms, null, "xmpp", host, props, cbh);
        super.authenticate();
    }     protected String getName()
    {
        return "DIGEST-MD5";
    }     public void challengeReceived(String challenge)
        throws IOException
    {
        //StringBuilder stanza = new StringBuilder();
        byte response[];
        if(challenge != null)
            response = sc.evaluateChallenge(Base64.decode(challenge));
        else
            //response = sc.evaluateChallenge(null);
            response = sc.evaluateChallenge(new byte[0]);
        //String authenticationText = "";
        Packet responseStanza;
        //if(response != null)
        //{
            //authenticationText = Base64.encodeBytes(response, 8);
            //if(authenticationText.equals(""))
                //authenticationText = "=";
                       if (response == null){
                responseStanza = new Response();
            } else {
                responseStanza = new Response(Base64.encodeBytes(response,Base64.DONT_BREAK_LINES));               }
        //}
        //stanza.append("<response xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
        //stanza.append(authenticationText);
        //stanza.append("</response>");
        //getSASLAuthentication().send(stanza.toString());
        getSASLAuthentication().send(responseStanza);
    }
}

main:

public class tryAgain
          {                     public static void main(String[] args)
                    {
                                       SASLAuthentication.registerSASLMechanism("DIGEST-MD5",MySASLDigestMD5Mechanism. class);
                            ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com",5222);
                                                 config.setSASLAuthenticationEnabled(true);
                            config.setRosterLoadedAtLogin (true);
                                                 XMPPConnection connection = new XMPPConnection(config);
                            try
                                             {
                                                       connection.connect();
                                             } catch (XMPPException e)
                                             {
                                                       // TODO Auto-generated catch block
                                                       e.printStackTrace();
                                             }
                            try
                                             {
                                                       connection.login("user@chat.facebook.com", passwd);
                                        } catch (XMPPException e)
                                        {
                                                  // TODO Auto-generated catch block
                                                  e.printStackTrace();
                                        }
                                            }
          }

Stack trace:

SASL authentication PLAIN failed: not-authorized:

at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java: 342)

at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:243)

at org.jivesoftware.smack.Connection.login(Connection.java:366)

at tryAgain.main(tryAgain.java:31)

//the error is at connection.login(username,password) line.

For google chat, the PLAIN authentication went smoothly and just the username and password was enough. But that does not suit facebook I guess.

Basically I am looking for a sample code upon which I can base my code. If that will be too time consuming, I request you to guide me with any help and suggestions possible. Thanks.

You are using DIGEST-MD5 in your code. Last time I checked, Facebook didn’t support it.

For PLAIN mechanim, you need a TLS connection. Otherwise FB will return a not-authorized.