Skip to content

Instantly share code, notes, and snippets.

@lehrblogger
Created April 28, 2012 21:41
Show Gist options
  • Save lehrblogger/2522307 to your computer and use it in GitHub Desktop.
Save lehrblogger/2522307 to your computer and use it in GitHub Desktop.
XMPP XEP-0077 Authenticated In-Band Registration (develop branch), gist #1
root@lucid32:/vagrant/code/SleekXMPP# python /vagrant/code/SleekXMPP/examples/register_account.py -v -j 'admin1@localhost' -p 'test_password' -u 'new_username'
DEBUG setting jid to admin1@localhost
DEBUG Loaded Plugin: RFC 6120: Stream Feature: STARTTLS
DEBUG Loaded Plugin: RFC 6120: Stream Feature: Resource Binding
DEBUG Loaded Plugin: RFC 3920: Stream Feature: Start Session
DEBUG Loaded Plugin: RFC 6120: Stream Feature: SASL
DEBUG Loaded Plugin: RFC 6121: Stream Feature: Roster Versioning
DEBUG Loaded Plugin: XEP-0030: Service Discovery
DEBUG Loaded Plugin: XEP-0004: Data Forms
DEBUG Loaded Plugin: XEP-0066: Out of Band Data
DEBUG Loaded Plugin: XEP-0077: In-Band Registration
DEBUG Connecting to 127.0.0.1:5222
DEBUG ==== TRANSITION disconnected -> connected
DEBUG Starting HANDLER THREAD
DEBUG SEND (IMMED): <stream:stream to='localhost' xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client' version='1.0'>
DEBUG RECV: <stream:features><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>SCRAM-SHA-1</mechanism><mechanism>DIGEST-MD5</mechanism><mechanism>PLAIN</mechanism></mechanisms><c xmlns="http://jabber.org/protocol/caps" node="http://www.process-one.net/en/ejabberd/" ver="yy7di5kE0syuCXOQTXNBTclpNTo=" hash="sha-1" /><register xmlns="http://jabber.org/features/iq-register" /></stream:features>
DEBUG Loading event runner
DEBUG SEND (IMMED): <auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="SCRAM-SHA-1">eSwsbj1hZG1pbjEscj05MzkwNTAwMjg3Mjg=</auth>
DEBUG SEND (IMMED): <iq type="get" id="14a231e8-cd63-498a-9fe8-659ff6ecf59c-1"><query xmlns="jabber:iq:register" /></iq>
DEBUG RECV: <challenge xmlns="urn:ietf:params:xml:ns:xmpp-sasl">cj05MzkwNTAwMjg3MjhCdUVGb1VJQWY5MzhMLzNvUFAvT0pRPT0scz1TTmo4cnVqb3hqLzV0SmIzK1FFNDZBPT0saT00MDk2</challenge>
DEBUG RECV: <iq type="result" from="localhost" id="14a231e8-cd63-498a-9fe8-659ff6ecf59c-1"><query xmlns="jabber:iq:register"><instructions>Choose a username and password to register with this server</instructions><username /><password /></query></iq>
WARNING Timed out waiting for IqWait_14a231e8-cd63-498a-9fe8-659ff6ecf59c-2
ERROR No response from server.
DEBUG SEND (IMMED): </stream:stream>
INFO Waiting for </stream:stream> from server
DEBUG End of stream recieved
DEBUG Waiting for 3 threads to exit.
DEBUG SEND: <iq to="localhost" from="admin1@localhost" id="14a231e8-cd63-498a-9fe8-659ff6ecf59c-2" type="set"><query xmlns="jabber:iq:register"><username>temp0</username><password>nens3ag7hund3youg2tu</password></query></iq>
DEBUG Stopped send thread. 2 threads remain.
DEBUG Quitting Scheduler thread
DEBUG Stopped scheduler thread. 1 threads remain.
DEBUG Could not acquire lock
Done
^C
Killed
diff --git a/examples/register_account.py b/examples/register_account.py
index 17f075a..28af18c 100644
--- a/examples/register_account.py
+++ b/examples/register_account.py
@@ -39,8 +39,9 @@ class RegisterBot(sleekxmpp.ClientXMPP):
workflows will need to check for data forms, etc.
"""
- def __init__(self, jid, password):
+ def __init__(self, jid, password, to_register):
sleekxmpp.ClientXMPP.__init__(self, jid, password)
+ self.to_register = to_register
# The session_start event will be triggered when
# the bot establishes its connection with the server
@@ -74,9 +75,6 @@ class RegisterBot(sleekxmpp.ClientXMPP):
self.send_presence()
self.get_roster()
- # We're only concerned about registering, so nothing more to do here.
- self.disconnect()
-
def register(self, iq):
"""
Fill out and submit a registration form.
@@ -96,13 +94,16 @@ class RegisterBot(sleekxmpp.ClientXMPP):
iq['register']['fields']
"""
resp = self.Iq()
+ resp['from'] = self.boundjid.full
+ resp['to'] = self.boundjid.host
resp['type'] = 'set'
- resp['register']['username'] = self.boundjid.user
- resp['register']['password'] = self.password
+ resp['register']['username'] = self.to_register
+ resp['register']['password'] = 'nens3ag7hund3youg2tu'
try:
- resp.send(now=True)
+ resp.send()
logging.info("Account created for %s!" % self.boundjid)
+ self.disconnect(wait=True) # Since otherwise we won't ever disconnect
except IqError as e:
logging.error("Could not register account: %s" %
e.iq['error']['text'])
@@ -133,6 +134,9 @@ if __name__ == '__main__':
optp.add_option("-p", "--password", dest="password",
help="password to use")
+ optp.add_option("-u", "--username", dest="to_register",
+ help="username to register")
+
opts, args = optp.parse_args()
# Setup logging.
@@ -147,7 +151,7 @@ if __name__ == '__main__':
# Setup the RegisterBot and register plugins. Note that while plugins may
# have interdependencies, the order in which you register them does
# not matter.
- xmpp = RegisterBot(opts.jid, opts.password)
+ xmpp = RegisterBot(opts.jid, opts.password, opts.to_register)
xmpp.register_plugin('xep_0030') # Service Discovery
xmpp.register_plugin('xep_0004') # Data forms
xmpp.register_plugin('xep_0066') # Out-of-band Data
@@ -161,7 +165,7 @@ if __name__ == '__main__':
# xmpp.ca_certs = "path/to/ca/cert"
# Connect to the XMPP server and start processing XMPP stanzas.
- if xmpp.connect():
+ if xmpp.connect(('127.0.0.1', 5222)):
# If you do not have the dnspython library installed, you will need
# to manually specify the name of the server if it does not match
# the one in the JID. For example, to use Google Talk you would
diff --git a/sleekxmpp/features/feature_mechanisms/mechanisms.py b/sleekxmpp/features/feature_mechanisms/mechanisms.py
index b7af81d..9b23f05 100644
--- a/sleekxmpp/features/feature_mechanisms/mechanisms.py
+++ b/sleekxmpp/features/feature_mechanisms/mechanisms.py
@@ -93,7 +93,7 @@ class FeatureMechanisms(BasePlugin):
self.xmpp.register_feature('mechanisms',
self._handle_sasl_auth,
- restart=True,
+ restart=False,
order=self.config.get('order', 100))
def _handle_sasl_auth(self, features):
diff --git a/sleekxmpp/plugins/xep_0077/register.py b/sleekxmpp/plugins/xep_0077/register.py
index 53cc9ef..c1f377a 100644
--- a/sleekxmpp/plugins/xep_0077/register.py
+++ b/sleekxmpp/plugins/xep_0077/register.py
@@ -40,16 +40,12 @@ class XEP_0077(BasePlugin):
self.xmpp.register_feature('register',
self._handle_register_feature,
restart=False,
- order=self.config.get('order', 50))
+ order=self.config.get('order', 150))
register_stanza_plugin(Register, self.xmpp['xep_0004'].stanza.Form)
register_stanza_plugin(Register, self.xmpp['xep_0066'].stanza.OOB)
def _handle_register_feature(self, features):
- if 'mechanisms' in self.xmpp.features:
- # We have already logged in with an account
- return False
-
if self.create_account:
form = self.get_registration()
self.xmpp.event('register', form, direct=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment