summaryrefslogtreecommitdiff
path: root/sleekxmpp/plugins/xep_0078.py
blob: 4b3ab8293a44d1852b1070f857faa727a3e8d2f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
    SleekXMPP: The Sleek XMPP Library
    Copyright (C) 2010 Nathanael C. Fritz
    This file is part of SleekXMPP.
    
    See the file LICENSE for copying permission.
"""
from __future__ import with_statement
from xml.etree import cElementTree as ET
import logging
import hashlib
from . import base


class xep_0078(base.base_plugin):
	"""
	XEP-0078 NON-SASL Authentication
	"""
	def plugin_init(self):
		self.description = "Non-SASL Authentication (broken)"
		self.xep = "0078"
		self.xmpp.add_event_handler("session_start", self.check_stream)
		#disabling until I fix conflict with PLAIN
		#self.xmpp.registerFeature("<auth xmlns='http://jabber.org/features/iq-auth'/>", self.auth)
		self.streamid = ''
	
	def check_stream(self, xml):
		self.streamid = xml.attrib['id']
		if xml.get('version', '0') != '1.0':
			self.auth()
	
	def auth(self, xml=None):
		logging.debug("Starting jabber:iq:auth Authentication")
		auth_request = self.xmpp.makeIqGet()
		auth_request_query = ET.Element('{jabber:iq:auth}query')
		auth_request.attrib['to'] = self.xmpp.server
		username = ET.Element('username')
		username.text = self.xmpp.username
		auth_request_query.append(username)
		auth_request.append(auth_request_query)
		result = auth_request.send()
		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:
			logging.warning("Authenticating via jabber:iq:auth Plain.")
			password = ET.Element('password')
			password.text = self.xmpp.password
			query.append(password)
		else:
			logging.debug("Authenticating via jabber:iq:auth Digest")
			digest = ET.Element('digest')
			digest.text = hashlib.sha1(b"%s%s" % (self.streamid, self.xmpp.password)).hexdigest()
			query.append(digest)
		attempt.append(query)
		result = attempt.send()
		if result.attrib['type'] == 'result':
			with self.xmpp.lock:
				self.xmpp.authenticated = True
				self.xmpp.sessionstarted = True
			self.xmpp.event("session_start")
		else:
			logging.info("Authentication failed")
			self.xmpp.disconnect()
			self.xmpp.event("failed_auth")