服务器到服务器SASL验证示例:

Step 1: Server1 initiates stream to Server2:

 
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml<stream:stream
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml    xmlns='jabber:server'
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml    xmlns:stream='http://etherx.jabber.org/streams'
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml    to='example.com'
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml    version='1.0'>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml

Step 2: Server2 responds with a stream tag sent to Server1:
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml<stream:stream
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml    xmlns='jabber:server'
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml    xmlns:stream='http://etherx.jabber.org/streams'
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml    from='example.com'
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml    id='s2s_234'
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml    version='1.0'>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml
 
Step 3: Server2 informs Server1 of available authentication mechanisms:
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml<stream:features>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml <mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml    <mechanism>DIGEST-MD5</mechanism>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml    <mechanism>KERBEROS_V4</mechanism>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml </mechanisms>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml</stream:features>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml
 
Step 4: Server1 selects an authentication mechanism:
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl'
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml      mechanism='DIGEST-MD5'/>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml
Step 5: Server2 sends a BASE64 encoded challenge to Server1: 
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xmlcmVhbG09InNvbWVyZWFsbSIsbm9uY2U9Ik9BNk1HOXRFUUdtMmhoIixxb3A9
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xmlImF1dGgiLGNoYXJzZXQ9dXRmLTgsYWxnb3JpdGhtPW1kNS1zZXNz
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml</challenge>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml
 
The decoded challenge is: 
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xmlrealm="somerealm",nonce="OA6MG9tEQGm2hh","
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xmlqop="auth",charset=utf-8,algorithm=md5-sess
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml
 
Step 5 (alt): Server2 returns error to Server1: 
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml<failure xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml <incorrect-encoding/>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml</failure>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml</stream:stream>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml
 
Step 6: Server1 sends a BASE64 encoded response to the challenge: 
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xmldXNlcm5hbWU9ImV4YW1wbGUub3JnIixyZWFsbT0ic29tZXJlYWxtIixub25j
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xmlZT0iT0E2TUc5dEVRR20yaGgiLGNub25jZT0iT0E2TUhYaDZWcVRyUmsiLG5j
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xmlPTAwMDAwMDAxLHFvcD1hdXRoLGRpZ2VzdC11cmk9InhtcHAvZXhhbXBsZS5v
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xmlcmciLHJlc3BvbnNlPWQzODhkYWQ5MGQ0YmJkNzYwYTE1MjMyMWYyMTQzYWY3
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xmlLGNoYXJzZXQ9dXRmLTgK
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml</response>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml
 
The decoded response is:
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xmlusername="example.org",realm="somerealm","
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xmlnonce="OA6MG9tEQGm2hh",cnonce="OA6MHXh6VqTrRk","
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xmlnc=00000001,qop=auth,digest-uri="xmpp/example.org","
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xmlresponse=d388dad90d4bbd760a152321f2143af7,charset=utf-8
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml
 
Step 7: Server2 sends another  BASE64 encoded challenge to Server1: 
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xmlcnNwYXV0aD1lYTQwZjYwMzM1YzQyN2I1NTI3Yjg0ZGJhYmNkZmZmZAo=
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml</challenge>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml
 
The decoded challenge is:
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xmlrspauth=ea40f60335c427b5527b84dbabcdfffd
 

Step 7 (alt): Server2 returns error to Server1:

 
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml<failure xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml <invalid-authzid/>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml</failure>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml</stream:stream>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml

Step 8: Server1 responds to the challenge:
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>
 

Step 8 (alt): Server1 aborts negotiation:

《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml<abort xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>
 

Step 9: Server2 informs Server1 of successful authentication:

《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>
 

Step 9 (alt): Server2 informs Server1 of failed authentication:

 
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml<failure xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml <aborted/>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml</failure>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml</stream:stream>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml

Step 10: Server1 initiates a new stream to Server2:
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml<stream:stream
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml    xmlns='jabber:server'
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml    xmlns:stream='http://etherx.jabber.org/streams'
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml    to='example.com'
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml    version='1.0'>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml
 
Step 11: Server2 responds by sending a stream header to Server1 along with any additional features (or an empty features element):
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml<stream:stream
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml    xmlns='jabber:server'
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml    xmlns:stream='http://etherx.jabber.org/streams'
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml    from='example.com'
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml    id='s2s_345'
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml    version='1.0'>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml<stream:features/>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml

 

服务器回拨

 

      这是一种单向的,不安全的验证方式。

 

 
具体过程:
1,              服务器A发送流头部给服务器B:
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml<stream:stream
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml    xmlns:stream='http://etherx.jabber.org/streams'
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml    xmlns='jabber:server'
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml    xmlns:db='jabber:server:dialback'>
《Extensible Messaging and Presence Protocol (XMPP): Core》阅读笔记(二)_xml
 
2,服务器B回送一个应答给A,包含了此次交互的ID:
<stream:stream
    xmlns:stream='http://etherx.jabber.org/streams'
    xmlns='jabber:server'
    xmlns:db='jabber:server:dialback'
id='457F9224A0...'>
3,服务器A发送一个回拨key给B
<db:result
    to='Receiving Server'
    from='Originating Server'>
 98AF014EDC0...
</db:result>
 
4,服务器B同验证服务器C建立TCP连接,向C发送流头部:
<stream:stream
    xmlns:stream='http://etherx.jabber.org/streams'
    xmlns='jabber:server'
    xmlns:db='jabber:server:dialback'>
5,C回送应答:
<stream:stream
    xmlns:stream='http://etherx.jabber.org/streams'
    xmlns='jabber:server'
    xmlns:db='jabber:server:dialback'
    id='1251A342B...'>
6,B发送验证key的请求
<db:verify
    from='Receiving Server'
    to='Originating Server'
    id='457F9224A0...'>
 98AF014EDC0...
</db:verify>
 

7,验证服务器验证key是否合法:

<db:verify

    from='Originating Server'

    to='Receiving Server'

    type='valid'

    id='457F9224A0...'/>

or

<db:verify

    from='Originating Server'

    to='Receiving Server'

    type='invalid'

    id='457F9224A0...'/>

8,B通知A结果:

<db:result
    from='Receiving Server'
    to='Originating Server'
    type='valid'/>

上面这个示例只是验证了从A到B的流是否合法,但不能保证从B到A是否合法,因此需要在反方向再进行一次验证。