00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "nonsaslauth.h"
00015 #include "client.h"
00016 #include "sha.h"
00017
00018 #include <string>
00019
00020 namespace gloox
00021 {
00022
00023 NonSaslAuth::NonSaslAuth( Client *parent )
00024 : m_parent( parent )
00025 {
00026 if( m_parent )
00027 m_parent->registerIqHandler( this, XMLNS_AUTH );
00028 }
00029
00030 NonSaslAuth::~NonSaslAuth()
00031 {
00032 if( m_parent )
00033 m_parent->removeIqHandler( XMLNS_AUTH );
00034 }
00035
00036 void NonSaslAuth::doAuth( const std::string& sid )
00037 {
00038 m_sid = sid;
00039 const std::string& id = m_parent->getID();
00040
00041 Tag *iq = new Tag( "iq" );
00042 iq->addAttribute( "to", m_parent->jid().server() );
00043 iq->addAttribute( "id", id );
00044 iq->addAttribute( "type", "get" );
00045 Tag *q = new Tag( iq, "query" );
00046 q->addAttribute( "xmlns", XMLNS_AUTH );
00047 new Tag( q, "username", m_parent->username() );
00048
00049 m_parent->trackID( this, id, TRACK_REQUEST_AUTH_FIELDS );
00050 m_parent->send( iq );
00051 }
00052
00053 bool NonSaslAuth::handleIqID( Stanza *stanza, int context )
00054 {
00055 switch( stanza->subtype() )
00056 {
00057 case StanzaIqError:
00058 {
00059 m_parent->setAuthed( false );
00060 m_parent->disconnect( ConnAuthenticationFailed );
00061
00062 Tag *t = stanza->findChild( "error" );
00063 if( t )
00064 {
00065 if( t->hasChild( "conflict" ) || t->hasAttribute( "code", "409" ) )
00066 m_parent->setAuthFailure( NonSaslConflict );
00067 else if( t->hasChild( "not-acceptable" ) || t->hasAttribute( "code", "406" ) )
00068 m_parent->setAuthFailure( NonSaslNotAcceptable );
00069 else if( t->hasChild( "not-authorized" ) || t->hasAttribute( "code", "401" ) )
00070 m_parent->setAuthFailure( NonSaslNotAuthorized );
00071 }
00072 break;
00073 }
00074 case StanzaIqResult:
00075 switch( context )
00076 {
00077 case TRACK_REQUEST_AUTH_FIELDS:
00078 {
00079 const std::string& id = m_parent->getID();
00080
00081 Tag *iq = new Tag( "iq" );
00082 iq->addAttribute( "id", id );
00083 iq->addAttribute( "type", "set" );
00084 Tag *query = new Tag( iq, "query" );
00085 query->addAttribute( "xmlns", XMLNS_AUTH );
00086 new Tag( query, "username", m_parent->jid().username() );
00087 new Tag( query, "resource", m_parent->jid().resource() );
00088
00089 Tag *q = stanza->findChild( "query" );
00090 if( ( q->hasChild( "digest" ) ) && !m_sid.empty() )
00091 {
00092 SHA sha;
00093 sha.feed( m_sid );
00094 sha.feed( m_parent->password() );
00095 sha.finalize();
00096 new Tag( query, "digest", sha.hex() );
00097 }
00098 else
00099 {
00100 new Tag( query, "password", m_parent->password() );
00101 }
00102
00103 m_parent->trackID( this, id, TRACK_SEND_AUTH );
00104 m_parent->send( iq );
00105 break;
00106 }
00107 case TRACK_SEND_AUTH:
00108 m_parent->setAuthed( true );
00109 m_parent->connected();
00110 break;
00111 }
00112 break;
00113
00114 default:
00115 break;
00116 }
00117 return false;
00118 }
00119
00120 bool NonSaslAuth::handleIq( Stanza * )
00121 {
00122 return false;
00123 }
00124
00125 }