gloox 1.0.24
nonsaslauth.cpp
1/*
2 Copyright (c) 2005-2019 by Jakob Schröter <js@camaya.net>
3 This file is part of the gloox library. http://camaya.net/gloox
4
5 This software is distributed under a license. The full license
6 agreement can be found in the file LICENSE in this distribution.
7 This software may not be copied, modified, sold or distributed
8 other than expressed in the named license agreement.
9
10 This software is distributed without any warranty.
11*/
12
13
14#include "nonsaslauth.h"
15#include "client.h"
16#include "error.h"
17#include "sha.h"
18
19#include <string>
20
21namespace gloox
22{
23
24 // ---- NonSaslAuth::Query ----
25 NonSaslAuth::Query::Query( const std::string& user )
26 : StanzaExtension( ExtNonSaslAuth ), m_user( user ), m_digest( true )
27 {
28 }
29
30 NonSaslAuth::Query::Query( const Tag* tag )
31 : StanzaExtension( ExtNonSaslAuth )
32 {
33 if( !tag || tag->name() != "query" || tag->xmlns() != XMLNS_AUTH )
34 return;
35
36 m_digest = tag->hasChild( "digest" );
37 }
38
39 NonSaslAuth::Query* NonSaslAuth::Query::newInstance( const std::string& user,
40 const std::string& sid,
41 const std::string& pwd,
42 const std::string& resource ) const
43 {
44 Query* q = new Query( user );
45 if( m_digest && !sid.empty() )
46 {
47 SHA sha;
48 sha.feed( sid );
49 sha.feed( pwd );
50 q->m_pwd = sha.hex();
51 }
52 else
53 q->m_pwd = pwd;
54
55 q->m_resource = resource;
56 q->m_digest = m_digest;
57 return q;
58 }
59
60 const std::string& NonSaslAuth::Query::filterString() const
61 {
62 static const std::string filter = "/iq/query[@xmlns='" + XMLNS_AUTH + "']";
63 return filter;
64 }
65
66 Tag* NonSaslAuth::Query::tag() const
67 {
68 if( m_user.empty() )
69 return 0;
70
71 Tag* t = new Tag( "query" );
72 t->setXmlns( XMLNS_AUTH );
73 new Tag( t, "username", m_user );
74
75 if( !m_pwd.empty() && !m_resource.empty() )
76 {
77 new Tag( t, m_digest ? "digest" : "password", m_pwd );
78 new Tag( t, "resource", m_resource );
79 }
80
81 return t;
82 }
83 // ---- ~NonSaslAuth::Query ----
84
85 // ---- NonSaslAuth ----
86 NonSaslAuth::NonSaslAuth( Client* parent )
87 : m_parent( parent )
88 {
89 if( m_parent )
90 {
91 m_parent->registerStanzaExtension( new Query() );
92 m_parent->registerIqHandler( this, ExtNonSaslAuth );
93 }
94 }
95
97 {
98 if( m_parent )
99 {
101 m_parent->removeIqHandler( this, ExtNonSaslAuth );
102 m_parent->removeIDHandler( this );
103 }
104 }
105
106 void NonSaslAuth::doAuth( const std::string& sid )
107 {
108 m_sid = sid;
109 const std::string& id = m_parent->getID();
110
111 IQ iq( IQ::Get, m_parent->jid().server(), id );
112 iq.addExtension( new Query( m_parent->username() ) );
113 m_parent->send( iq, this, TrackRequestAuthFields );
114 }
115
116 void NonSaslAuth::handleIqID( const IQ& iq, int context )
117 {
118 switch( iq.subtype() )
119 {
120 case IQ::Error:
121 {
122 const Error* e = iq.error();
123 if( e )
124 {
125 switch( e->error() )
126 {
128 m_parent->setAuthFailure( NonSaslConflict );
129 break;
132 break;
135 break;
136 default:
137 break;
138 }
139 }
140 m_parent->setAuthed( false );
142 break;
143 }
144 case IQ::Result:
145 switch( context )
146 {
147 case TrackRequestAuthFields:
148 {
149 const Query* q = iq.findExtension<Query>( ExtNonSaslAuth );
150 if( !q )
151 return;
152
153 const std::string& id = m_parent->getID();
154
155 IQ re( IQ::Set, JID(), id );
156 re.addExtension( q->newInstance( m_parent->username(), m_sid,
157 m_parent->password(),
158 m_parent->jid().resource() ) );
159 m_parent->send( re, this, TrackSendAuth );
160 break;
161 }
162 case TrackSendAuth:
163 m_parent->setAuthed( true );
164 m_parent->connected();
165 break;
166 }
167 break;
168
169 default:
170 break;
171 }
172 }
173
174}
const std::string getID()
void setAuthFailure(AuthenticationError e)
Definition: clientbase.h:784
void setAuthed(bool authed)
Definition: clientbase.h:777
virtual const std::string & password() const
Definition: clientbase.h:227
void removeIqHandler(IqHandler *ih, int exttype)
void removeIDHandler(IqHandler *ih)
bool removeStanzaExtension(int ext)
void send(Tag *tag)
const JID & jid()
Definition: clientbase.h:147
virtual const std::string & username() const
Definition: clientbase.h:138
void registerIqHandler(IqHandler *ih, int exttype)
void registerStanzaExtension(StanzaExtension *ext)
This class implements a basic Jabber/XMPP Client.
Definition: client.h:117
void disconnect()
Definition: client.cpp:739
A stanza error abstraction implemented as a StanzaExtension.
Definition: error.h:35
StanzaError error() const
Definition: error.h:75
An abstraction of an IQ stanza.
Definition: iq.h:34
IqType subtype() const
Definition: iq.h:74
@ Set
Definition: iq.h:46
@ Error
Definition: iq.h:49
@ Result
Definition: iq.h:48
@ Get
Definition: iq.h:45
An abstraction of a JID.
Definition: jid.h:31
const std::string & server() const
Definition: jid.h:104
const std::string & resource() const
Definition: jid.h:116
void doAuth(const std::string &sid)
virtual void handleIqID(const IQ &iq, int context)
virtual ~NonSaslAuth()
Definition: nonsaslauth.cpp:96
void addExtension(const StanzaExtension *se)
Definition: stanza.cpp:52
const Error * error() const
Definition: stanza.cpp:47
const StanzaExtension * findExtension(int type) const
Definition: stanza.cpp:57
The namespace for the gloox library.
Definition: adhoc.cpp:28
@ ConnAuthenticationFailed
Definition: gloox.h:712
@ NonSaslConflict
Definition: gloox.h:1044
@ NonSaslNotAuthorized
Definition: gloox.h:1046
@ NonSaslNotAcceptable
Definition: gloox.h:1045
@ StanzaErrorConflict
Definition: gloox.h:878
@ StanzaErrorNotAuthorized
Definition: gloox.h:906
@ StanzaErrorNotAcceptable
Definition: gloox.h:900
const std::string XMLNS_AUTH
Definition: gloox.cpp:43
@ ExtNonSaslAuth