slixmpp/sleekxmpp/plugins/xep_0078.py

73 lines
2.2 KiB
Python
Raw Normal View History

2009-06-03 22:56:51 +00:00
"""
SleekXMPP: The Sleek XMPP Library
Copyright (C) 2010 Nathanael C. Fritz
This file is part of SleekXMPP.
See the file LICENSE for copying permission.
2009-06-03 22:56:51 +00:00
"""
from __future__ import with_statement
from xml.etree import cElementTree as ET
import logging
import hashlib
from . import base
2009-06-03 22:56:51 +00:00
log = logging.getLogger(__name__)
2009-06-03 22:56:51 +00:00
class xep_0078(base.base_plugin):
"""
XEP-0078 NON-SASL Authentication
"""
def plugin_init(self):
self.description = "Non-SASL Authentication (broken)"
self.xep = "0078"
2010-01-08 06:06:44 +00:00
self.xmpp.add_event_handler("session_start", self.check_stream)
2009-06-03 22:56:51 +00:00
#disabling until I fix conflict with PLAIN
#self.xmpp.registerFeature("<auth xmlns='http://jabber.org/features/iq-auth'/>", self.auth)
self.streamid = ''
2009-06-03 22:56:51 +00:00
def check_stream(self, xml):
self.streamid = xml.attrib['id']
if xml.get('version', '0') != '1.0':
self.auth()
2009-06-03 22:56:51 +00:00
def auth(self, xml=None):
log.debug("Starting jabber:iq:auth Authentication")
2009-06-03 22:56:51 +00:00
auth_request = self.xmpp.makeIqGet()
auth_request_query = ET.Element('{jabber:iq:auth}query')
auth_request.attrib['to'] = self.xmpp.boundjid.host
2009-06-03 22:56:51 +00:00
username = ET.Element('username')
username.text = self.xmpp.username
auth_request_query.append(username)
auth_request.append(auth_request_query)
result = auth_request.send()
2009-06-03 22:56:51 +00:00
rquery = result.find('{jabber:iq:auth}query')
attempt = self.xmpp.makeIqSet()
query = ET.Element('{jabber:iq:auth}query')
resource = ET.Element('resource')
resource.text = self.xmpp.resource
query.append(username)
query.append(resource)
if rquery.find('{jabber:iq:auth}digest') is None:
log.warning("Authenticating via jabber:iq:auth Plain.")
2009-06-03 22:56:51 +00:00
password = ET.Element('password')
password.text = self.xmpp.password
query.append(password)
else:
log.debug("Authenticating via jabber:iq:auth Digest")
2009-06-03 22:56:51 +00:00
digest = ET.Element('digest')
digest.text = hashlib.sha1(b"%s%s" % (self.streamid, self.xmpp.password)).hexdigest()
2009-06-03 22:56:51 +00:00
query.append(digest)
attempt.append(query)
result = attempt.send()
2009-06-03 22:56:51 +00:00
if result.attrib['type'] == 'result':
with self.xmpp.lock:
self.xmpp.authenticated = True
self.xmpp.sessionstarted = True
self.xmpp.event("session_start")
else:
log.info("Authentication failed")
2009-06-03 22:56:51 +00:00
self.xmpp.disconnect()
self.xmpp.event("failed_auth")