nonsaslauth.cpp
/*
Copyright (c) 2005-2023 by Jakob Schröter <js@camaya.net>
This file is part of the gloox library. http://camaya.net/gloox
This software is distributed under a license. The full license
agreement can be found in the file LICENSE in this distribution.
This software may not be copied, modified, sold or distributed
other than expressed in the named license agreement.
This software is distributed without any warranty.
*/
#include "nonsaslauth.h"
#include "client.h"
#include "error.h"
#include "sha.h"
#include <string>
namespace gloox
{
// ---- NonSaslAuth::Query ----
NonSaslAuth::Query::Query( const std::string& user )
: StanzaExtension( ExtNonSaslAuth ), m_user( user ), m_digest( true )
{
}
NonSaslAuth::Query::Query( const Tag* tag )
: StanzaExtension( ExtNonSaslAuth )
{
if( !tag || tag->name() != "query" || tag->xmlns() != XMLNS_AUTH )
return;
m_digest = tag->hasChild( "digest" );
}
NonSaslAuth::Query* NonSaslAuth::Query::newInstance( const std::string& user,
const std::string& sid,
const std::string& pwd,
const std::string& resource ) const
{
Query* q = new Query( user );
if( m_digest && !sid.empty() )
{
SHA sha;
sha.feed( sid );
sha.feed( pwd );
q->m_pwd = sha.hex();
}
else
q->m_pwd = pwd;
q->m_resource = resource;
q->m_digest = m_digest;
return q;
}
const std::string& NonSaslAuth::Query::filterString() const
{
static const std::string filter = "/iq/query[@xmlns='" + XMLNS_AUTH + "']";
return filter;
}
Tag* NonSaslAuth::Query::tag() const
{
if( m_user.empty() )
return 0;
Tag* t = new Tag( "query" );
t->setXmlns( XMLNS_AUTH );
new Tag( t, "username", m_user );
if( !m_pwd.empty() && !m_resource.empty() )
{
new Tag( t, m_digest ? "digest" : "password", m_pwd );
new Tag( t, "resource", m_resource );
}
return t;
}
// ---- ~NonSaslAuth::Query ----
// ---- NonSaslAuth ----
NonSaslAuth::NonSaslAuth( Client* parent )
: m_parent( parent )
{
if( m_parent )
{
m_parent->registerStanzaExtension( new Query() );
m_parent->registerIqHandler( this, ExtNonSaslAuth );
}
}
NonSaslAuth::~NonSaslAuth()
{
if( m_parent )
{
m_parent->removeStanzaExtension( ExtNonSaslAuth );
m_parent->removeIqHandler( this, ExtNonSaslAuth );
m_parent->removeIDHandler( this );
}
}
void NonSaslAuth::doAuth( const std::string& sid )
{
m_sid = sid;
const std::string& id = m_parent->getID();
IQ iq( IQ::Get, m_parent->jid().server(), id );
iq.addExtension( new Query( m_parent->username() ) );
m_parent->send( iq, this, TrackRequestAuthFields );
}
void NonSaslAuth::handleIqID( const IQ& iq, int context )
{
switch( iq.subtype() )
{
case IQ::Error:
{
const Error* e = iq.error();
if( e )
{
switch( e->error() )
{
case StanzaErrorConflict:
m_parent->setAuthFailure( NonSaslConflict );
break;
case StanzaErrorNotAcceptable:
m_parent->setAuthFailure( NonSaslNotAcceptable );
break;
case StanzaErrorNotAuthorized:
m_parent->setAuthFailure( NonSaslNotAuthorized );
break;
default:
break;
}
}
m_parent->setAuthed( false );
m_parent->disconnect( ConnAuthenticationFailed );
break;
}
case IQ::Result:
switch( context )
{
case TrackRequestAuthFields:
{
const Query* q = iq.findExtension<Query>( ExtNonSaslAuth );
if( !q )
return;
const std::string& id = m_parent->getID();
IQ re( IQ::Set, JID(), id );
re.addExtension( q->newInstance( m_parent->username(), m_sid,
m_parent->password(),
m_parent->jid().resource() ) );
m_parent->send( re, this, TrackSendAuth );
break;
}
case TrackSendAuth:
m_parent->setAuthed( true );
m_parent->connected();
break;
}
break;
default:
break;
}
}
}
gloox::Client
This class implements a basic Jabber/XMPP Client.
Definition client.h:117
gloox::Error
A stanza error abstraction implemented as a StanzaExtension.
Definition error.h:35
gloox::Error::error
StanzaError error() const
Definition error.h:75
gloox::IQ
An abstraction of an IQ stanza.
Definition iq.h:34
gloox::IQ::subtype
IqType subtype() const
Definition iq.h:74
gloox::IQ::Set
@ Set
Definition iq.h:46
gloox::IQ::Error
@ Error
Definition iq.h:49
gloox::IQ::Result
@ Result
Definition iq.h:48
gloox::IQ::Get
@ Get
Definition iq.h:45
gloox::JID
An abstraction of a JID.
Definition jid.h:31
gloox::NonSaslAuth::doAuth
void doAuth(const std::string &sid)
Definition nonsaslauth.cpp:106
gloox::NonSaslAuth::handleIqID
virtual void handleIqID(const IQ &iq, int context)
Definition nonsaslauth.cpp:116
gloox::NonSaslAuth::NonSaslAuth
NonSaslAuth(Client *parent)
Definition nonsaslauth.cpp:86
gloox::NonSaslAuth::~NonSaslAuth
virtual ~NonSaslAuth()
Definition nonsaslauth.cpp:96
gloox::StanzaExtension
This class abstracts a stanza extension, which is usually an XML child element in a specific namespac...
Definition stanzaextension.h:204
gloox::Stanza::addExtension
void addExtension(const StanzaExtension *se)
Definition stanza.cpp:52
gloox::Stanza::error
const Error * error() const
Definition stanza.cpp:47
gloox::Stanza::findExtension
const StanzaExtension * findExtension(int type) const
Definition stanza.cpp:57
gloox
The namespace for the gloox library.
Definition adhoc.cpp:28
gloox::ConnAuthenticationFailed
@ ConnAuthenticationFailed
Definition gloox.h:712
gloox::NonSaslConflict
@ NonSaslConflict
Definition gloox.h:1044
gloox::NonSaslNotAuthorized
@ NonSaslNotAuthorized
Definition gloox.h:1046
gloox::NonSaslNotAcceptable
@ NonSaslNotAcceptable
Definition gloox.h:1045
gloox::StanzaErrorConflict
@ StanzaErrorConflict
Definition gloox.h:878
gloox::StanzaErrorNotAuthorized
@ StanzaErrorNotAuthorized
Definition gloox.h:906
gloox::StanzaErrorNotAcceptable
@ StanzaErrorNotAcceptable
Definition gloox.h:900
gloox::XMLNS_AUTH
const std::string XMLNS_AUTH
Definition gloox.cpp:43
gloox::ExtNonSaslAuth
@ ExtNonSaslAuth
Definition stanzaextension.h:68