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 {
00034 m_parent->removeIqHandler( XMLNS_AUTH );
00035 m_parent->removeIDHandler( this );
00036 }
00037 }
00038
00039 void NonSaslAuth::doAuth( const std::string& sid )
00040 {
00041 m_sid = sid;
00042 const std::string& id = m_parent->getID();
00043
00044 Tag *iq = new Tag( "iq" );
00045 iq->addAttribute( "to", m_parent->jid().server() );
00046 iq->addAttribute( "id", id );
00047 iq->addAttribute( "type", "get" );
00048 Tag *q = new Tag( iq, "query" );
00049 q->addAttribute( "xmlns", XMLNS_AUTH );
00050 new Tag( q, "username", m_parent->username() );
00051
00052 m_parent->trackID( this, id, TRACK_REQUEST_AUTH_FIELDS );
00053 m_parent->send( iq );
00054 }
00055
00056 bool NonSaslAuth::handleIqID( Stanza *stanza, int context )
00057 {
00058 switch( stanza->subtype() )
00059 {
00060 case StanzaIqError:
00061 {
00062 m_parent->setAuthed( false );
00063 m_parent->disconnect( ConnAuthenticationFailed );
00064
00065 Tag *t = stanza->findChild( "error" );
00066 if( t )
00067 {
00068 if( t->hasChild( "conflict" ) || t->hasAttribute( "code", "409" ) )
00069 m_parent->setAuthFailure( NonSaslConflict );
00070 else if( t->hasChild( "not-acceptable" ) || t->hasAttribute( "code", "406" ) )
00071 m_parent->setAuthFailure( NonSaslNotAcceptable );
00072 else if( t->hasChild( "not-authorized" ) || t->hasAttribute( "code", "401" ) )
00073 m_parent->setAuthFailure( NonSaslNotAuthorized );
00074 }
00075 break;
00076 }
00077 case StanzaIqResult:
00078 switch( context )
00079 {
00080 case TRACK_REQUEST_AUTH_FIELDS:
00081 {
00082 const std::string& id = m_parent->getID();
00083
00084 Tag *iq = new Tag( "iq" );
00085 iq->addAttribute( "id", id );
00086 iq->addAttribute( "type", "set" );
00087 Tag *query = new Tag( iq, "query" );
00088 query->addAttribute( "xmlns", XMLNS_AUTH );
00089 new Tag( query, "username", m_parent->jid().username() );
00090 new Tag( query, "resource", m_parent->jid().resource() );
00091
00092 Tag *q = stanza->findChild( "query" );
00093 if( ( q->hasChild( "digest" ) ) && !m_sid.empty() )
00094 {
00095 SHA sha;
00096 sha.feed( m_sid );
00097 sha.feed( m_parent->password() );
00098 sha.finalize();
00099 new Tag( query, "digest", sha.hex() );
00100 }
00101 else
00102 {
00103 new Tag( query, "password", m_parent->password() );
00104 }
00105
00106 m_parent->trackID( this, id, TRACK_SEND_AUTH );
00107 m_parent->send( iq );
00108 break;
00109 }
00110 case TRACK_SEND_AUTH:
00111 m_parent->setAuthed( true );
00112 m_parent->connected();
00113 break;
00114 }
00115 break;
00116
00117 default:
00118 break;
00119 }
00120 return false;
00121 }
00122
00123 bool NonSaslAuth::handleIq( Stanza * )
00124 {
00125 return false;
00126 }
00127
00128 }