Tuesday 19 June 2012

XMPP Plain Authentication

All of the communication between clientand server are done by passing XML. So it is important to getknowledge about XML first. Following are the links to get hands onXML.

For authenticating user to the xmppserver. Client needs to open a stream. XMPP works on stream base.

A stream is a sequence of data ofundetermined length. It's called a stream because it's like a streamof water that continues to flow. There's no definite end to it.

A better analogy might be a queue ofpeople waiting to get on a ride at an amusement park. As people areprocessed at the front (i.e. get on the roller coaster) more areadded at the back of the line. If it's a slow day the roller coastermay catch up with the end of the line and have to wait for people toboard. Other days there may always be people in line until the parkcloses. Each person is a discrete individual and must be put on theroller coaster or not put on the roller coaster. There are nohalf-people. There's always a definite number of people in linethough this number may change from moment to moment as people enterat the back of the line and exit from the front of the line. Althoughall the people are discrete, you'll sometimes have a family that mustbe put together in the same car. Thus although the individuals arediscrete, they aren't necessarily unrelated.

In Java a stream is composed ofdiscrete bytes. The bytes may represent chars or other kinds of data.They may come faster than you can handle them, or your thread mayblock while waiting for the next one to arrive. It often doesn'tmatter.

To open a stream in XMPP user needs tosend following XML to the server. Here because of you are using itwith client application it is necessary to ass jabber:client inxmlns.
<?xml version='1.0'?>

<stream:stream to='{server name}' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>
Server would open a stream for thatclient. Server must notify client with opened stream. If the streamhas been opened successfully. Server will also send starttls."starttls" is basically being used for securing thecommunication client-server.
      <xml encoding='UTF-8'version='1.0'></xml>

<stream:stream id='95360e39'from='{server name}' version='1.0' xml:lang='en' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>

</stream:stream>

<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'>
</starttls>
Server will also send details aboutmethods of communication which is called stream-features. All of thestream features are explained in details athttp://xmpp.org/registrar/stream-features.html. Following are someexamples of the stream-features.
      <mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>
<mechanism>DIGEST-MD5</mechanism>
<mechanism>PLAIN</mechanism>
<mechanism>ANONYMOUS</mechanism>
<mechanism>CRAM-MD5</mechanism>
</mechanisms>

<compression xmlns='http://jabber.org/features/compress'>
<method>zlib</method>
</compression>

<auth xmlns='http://jabber.org/features/iq-auth'></auth>

<register xmlns='http://jabber.org/features/iq-register'></register>

<stream:features>

<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'></starttls>
<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>
<mechanism>DIGEST-MD5</mechanism>
<mechanism>PLAIN</mechanism>
<mechanism>ANONYMOUS</mechanism>
<mechanism>CRAM-MD5</mechanism>
</mechanisms>

<compression xmlns='http://jabber.org/features/compress'>
<method>zlib</method>
</compression>

<auth xmlns='http://jabber.org/features/iq-auth'></auth>

<register xmlns='http://jabber.org/features/iq-register'></register>

</stream:features>
In my case i have been using plainauthentication. For that you need to make sure that server is enabledto use plain authentication. This detail would have been given inmechanism XML in stream-features. Auth XML needs to have base64 valueof (username + password). As given below.
      <auth id='sasl2' xmlns="urn:ietf:params:xml:ns:xmpp-sasl"mechanism="PLAIN">"\0" + username + "\0"+ password</auth>
if user gets logged in successfully.Client will get a success XML in return. If authentication will befailed on server side. It will send back a failure XML. Examples aregiven below
 <success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'></success>  
<not-authorized></not-authorized>

<failure xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>
<not-authorized></not-authorized>
</failure>

1 comments:

  1. Thank Its very help full when i create my applications.
    Thank you

    ReplyDelete