2 Slixmpp: The Slick XMPP Library
3 Copyright (C) 2011 Nathanael C. Fritz
4 This file is part of Slixmpp.
6 See the file LICENSE for copying permission.
15 from slixmpp
.jid
import JID
16 from slixmpp
.exceptions
import IqError
, IqTimeout
17 from slixmpp
.stanza
import Iq
, StreamFeatures
18 from slixmpp
.xmlstream
import ElementBase
, ET
, register_stanza_plugin
19 from slixmpp
.plugins
import BasePlugin
20 from slixmpp
.plugins
.xep_0078
import stanza
23 log
= logging
.getLogger(__name__
)
26 class XEP_0078(BasePlugin
):
29 XEP-0078 NON-SASL Authentication
31 This XEP is OBSOLETE in favor of using SASL, so DO NOT use this plugin
32 unless you are forced to use an old XMPP server implementation.
36 description
= 'XEP-0078: Non-SASL Authentication'
43 def plugin_init(self
):
44 self
.xmpp
.register_feature('auth',
49 self
.xmpp
.add_event_handler('legacy_protocol',
50 self
._handle
_legacy
_protocol
)
52 register_stanza_plugin(Iq
, stanza
.IqAuth
)
53 register_stanza_plugin(StreamFeatures
, stanza
.AuthFeature
)
56 self
.xmpp
.del_event_handler('legacy_protocol',
57 self
._handle
_legacy
_protocol
)
58 self
.xmpp
.unregister_feature('auth', self
.order
)
60 def _handle_auth(self
, features
):
61 # If we can or have already authenticated with SASL, do nothing.
62 if 'mechanisms' in features
['features']:
64 return self
.authenticate()
66 def _handle_legacy_protocol(self
, event
):
69 def authenticate(self
):
70 if self
.xmpp
.authenticated
:
73 log
.debug("Starting jabber:iq:auth Authentication")
75 # Step 1: Request the auth form
78 iq
['to'] = self
.xmpp
.requested_jid
.host
79 iq
['auth']['username'] = self
.xmpp
.requested_jid
.user
83 except IqError
as err
:
84 log
.info("Authentication failed: %s", err
.iq
['error']['condition'])
85 self
.xmpp
.event('failed_auth')
86 self
.xmpp
.disconnect()
89 log
.info("Authentication failed: %s", 'timeout')
90 self
.xmpp
.event('failed_auth')
91 self
.xmpp
.disconnect()
94 # Step 2: Fill out auth form for either password or digest auth
97 iq
['auth']['username'] = self
.xmpp
.requested_jid
.user
99 # A resource is required, so create a random one if necessary
100 resource
= self
.xmpp
.requested_jid
.resource
102 resource
= str(uuid
.uuid4())
104 iq
['auth']['resource'] = resource
106 if 'digest' in resp
['auth']['fields']:
107 log
.debug('Authenticating via jabber:iq:auth Digest')
108 if sys
.version_info
< (3, 0):
109 stream_id
= bytes(self
.xmpp
.stream_id
)
110 password
= bytes(self
.xmpp
.password
)
112 stream_id
= bytes(self
.xmpp
.stream_id
, encoding
='utf-8')
113 password
= bytes(self
.xmpp
.password
, encoding
='utf-8')
115 digest
= hashlib
.sha1(b
'%s%s' % (stream_id
, password
)).hexdigest()
116 iq
['auth']['digest'] = digest
118 log
.warning('Authenticating via jabber:iq:auth Plain.')
119 iq
['auth']['password'] = self
.xmpp
.password
121 # Step 3: Send credentials
124 except IqError
as err
:
125 log
.info("Authentication failed")
126 self
.xmpp
.event("failed_auth")
127 self
.xmpp
.disconnect()
129 log
.info("Authentication failed")
130 self
.xmpp
.event("failed_auth")
131 self
.xmpp
.disconnect()
133 self
.xmpp
.features
.add('auth')
135 self
.xmpp
.authenticated
= True
137 self
.xmpp
.boundjid
= JID(self
.xmpp
.requested_jid
,
140 self
.xmpp
.event('session_bind', self
.xmpp
.boundjid
)
142 log
.debug("Established Session")
143 self
.xmpp
.sessionstarted
= True
144 self
.xmpp
.event('session_start')