summaryrefslogtreecommitdiff
path: root/sleekxmpp/util
diff options
context:
space:
mode:
Diffstat (limited to 'sleekxmpp/util')
-rw-r--r--sleekxmpp/util/__init__.py25
-rw-r--r--sleekxmpp/util/misc_ops.py18
-rw-r--r--sleekxmpp/util/sasl/__init__.py4
-rw-r--r--sleekxmpp/util/sasl/client.py4
-rw-r--r--sleekxmpp/util/sasl/mechanisms.py44
-rw-r--r--sleekxmpp/util/stringprep_profiles.py3
6 files changed, 70 insertions, 28 deletions
diff --git a/sleekxmpp/util/__init__.py b/sleekxmpp/util/__init__.py
index 1e4af02d..47a935af 100644
--- a/sleekxmpp/util/__init__.py
+++ b/sleekxmpp/util/__init__.py
@@ -11,21 +11,38 @@
from sleekxmpp.util.misc_ops import bytes, unicode, hashes, hash, \
- num_to_bytes, bytes_to_num, quote, XOR
+ num_to_bytes, bytes_to_num, quote, \
+ XOR, safedict
# =====================================================================
# Standardize import of Queue class:
import sys
-if 'gevent' in sys.modules:
+
+def _gevent_threads_enabled():
+ if not 'gevent' in sys.modules:
+ return False
+ try:
+ from gevent import thread as green_thread
+ thread = __import__('thread')
+ return thread.LockType is green_thread.LockType
+ except ImportError:
+ return False
+
+if _gevent_threads_enabled():
import gevent.queue as queue
- Queue = queue.JoinableQueue
+ _queue = queue.JoinableQueue
else:
try:
import queue
except ImportError:
import Queue as queue
- Queue = queue.Queue
+ _queue = queue.Queue
+class Queue(_queue):
+ def put(self, item, block=True, timeout=None):
+ if _queue.full(self):
+ _queue.get(self)
+ return _queue.put(self, item, block, timeout)
QueueEmpty = queue.Empty
diff --git a/sleekxmpp/util/misc_ops.py b/sleekxmpp/util/misc_ops.py
index 3b246625..18c919a8 100644
--- a/sleekxmpp/util/misc_ops.py
+++ b/sleekxmpp/util/misc_ops.py
@@ -8,7 +8,10 @@ def unicode(text):
text = text.decode('utf-8')
import __builtin__
return __builtin__.unicode(text)
- return str(text)
+ elif not isinstance(text, str):
+ return text.decode('utf-8')
+ else:
+ return text
def bytes(text):
@@ -126,6 +129,7 @@ def hashes():
hashes = ['SHA-' + h[3:] for h in dir(hashlib) if h.startswith('sha')]
return t + hashes
+
def setdefaultencoding(encoding):
"""
Set the current default string encoding used by the Unicode implementation.
@@ -148,4 +152,14 @@ def setdefaultencoding(encoding):
if func is None:
raise RuntimeError("Could not find setdefaultencoding")
sys.setdefaultencoding = func
- return func(encoding) \ No newline at end of file
+ return func(encoding)
+
+
+def safedict(data):
+ if sys.version_info < (2, 7):
+ safe = {}
+ for key in data:
+ safe[key.encode('utf8')] = data[key]
+ return safe
+ else:
+ return data
diff --git a/sleekxmpp/util/sasl/__init__.py b/sleekxmpp/util/sasl/__init__.py
index d054ce09..2d344e9b 100644
--- a/sleekxmpp/util/sasl/__init__.py
+++ b/sleekxmpp/util/sasl/__init__.py
@@ -7,7 +7,9 @@
Part of SleekXMPP: The Sleek XMPP Library
- :copyright: (c) 2012 Nathanael C. Fritz, Lance J.T. Stout
+ :copryight: (c) 2004-2013 David Alan Cridland
+ :copyright: (c) 2013 Nathanael C. Fritz, Lance J.T. Stout
+
:license: MIT, see LICENSE for more details
"""
diff --git a/sleekxmpp/util/sasl/client.py b/sleekxmpp/util/sasl/client.py
index 0bfb63f8..fd685547 100644
--- a/sleekxmpp/util/sasl/client.py
+++ b/sleekxmpp/util/sasl/client.py
@@ -7,7 +7,9 @@
Part of SleekXMPP: The Sleek XMPP Library
- :copyright: (c) 2012 Nathanael C. Fritz, Lance J.T. Stout
+ :copryight: (c) 2004-2013 David Alan Cridland
+ :copyright: (c) 2013 Nathanael C. Fritz, Lance J.T. Stout
+
:license: MIT, see LICENSE for more details
"""
diff --git a/sleekxmpp/util/sasl/mechanisms.py b/sleekxmpp/util/sasl/mechanisms.py
index 55ae44dd..7a7ebf7b 100644
--- a/sleekxmpp/util/sasl/mechanisms.py
+++ b/sleekxmpp/util/sasl/mechanisms.py
@@ -9,7 +9,9 @@
Part of SleekXMPP: The Sleek XMPP Library
- :copyright: (c) 2012 Nathanael C. Fritz, Lance J.T. Stout
+ :copryight: (c) 2004-2013 David Alan Cridland
+ :copyright: (c) 2013 Nathanael C. Fritz, Lance J.T. Stout
+
:license: MIT, see LICENSE for more details
"""
@@ -21,7 +23,8 @@ from base64 import b64encode, b64decode
from sleekxmpp.util import bytes, hash, XOR, quote, num_to_bytes
from sleekxmpp.util.sasl.client import sasl_mech, Mech, \
- SASLCancelled, SASLFailed
+ SASLCancelled, SASLFailed, \
+ SASLMutualAuthFailed
@sasl_mech(0)
@@ -86,7 +89,7 @@ class EXTERNAL(Mech):
return self.credentials['authzid']
-@sasl_mech(30)
+@sasl_mech(31)
class X_FACEBOOK_PLATFORM(Mech):
name = 'X-FACEBOOK-PLATFORM'
@@ -108,7 +111,7 @@ class X_FACEBOOK_PLATFORM(Mech):
b'api_key': self.credentials['api_key']
}
- resp = '&'.join(['%s=%s' % (k, v) for k, v in resp_data.items()])
+ resp = '&'.join(['%s=%s' % (k.decode("utf-8"), v.decode("utf-8")) for k, v in resp_data.items()])
return bytes(resp)
return b''
@@ -220,17 +223,16 @@ class SCRAM(Mech):
return self.hash(text).digest()
def saslname(self, value):
- escaped = b''
- for char in bytes(value):
- if char == b',':
- escaped += b'=2C'
- elif char == b'=':
- escaped += b'=3D'
+ value = value.decode("utf-8")
+ escaped = []
+ for char in value:
+ if char == ',':
+ escaped += '=2C'
+ elif char == '=':
+ escaped += '=3D'
else:
- if isinstance(char, int):
- char = chr(char)
- escaped += bytes(char)
- return escaped
+ escaped += char
+ return "".join(escaped).encode("utf-8")
def parse(self, challenge):
items = {}
@@ -284,7 +286,9 @@ class SCRAM(Mech):
if nonce[:len(self.cnonce)] != self.cnonce:
raise SASLCancelled('Invalid nonce')
- cbind_data = self.credentials['channel_binding']
+ cbind_data = b''
+ if self.use_channel_binding:
+ cbind_data = self.credentials['channel_binding']
cbind_input = self.gs2_header + cbind_data
channel_binding = b'c=' + b64encode(cbind_input).replace(b'\n', b'')
@@ -467,7 +471,8 @@ class DIGEST(Mech):
'qop': self.qop,
'digest-uri': quote(self.digest_uri()),
'response': self.response(b'AUTHENTICATE'),
- 'maxbuf': self.maxbuf
+ 'maxbuf': self.maxbuf,
+ 'charset': 'utf-8'
}
resp = b''
for key, value in data.items():
@@ -480,7 +485,7 @@ class DIGEST(Mech):
if self.cnonce and self.nonce and self.nonce_count and self.qop:
self.nonce_count += 1
return self.respond()
- return b''
+ return None
data = self.parse(challenge)
if 'rspauth' in data:
@@ -526,6 +531,9 @@ else:
result = kerberos.authGSSClientStep(self.gss, b64_challenge)
if result != kerberos.AUTH_GSS_CONTINUE:
self.step = 1
+ elif not challenge:
+ kerberos.authGSSClientClean(self.gss)
+ return b''
elif self.step == 1:
username = self.credentials['username']
@@ -535,7 +543,7 @@ else:
resp = kerberos.authGSSClientResponse(self.gss)
except kerberos.GSSError as e:
- raise SASLCancelled('Kerberos error: %s' % e.message)
+ raise SASLCancelled('Kerberos error: %s' % e)
if not resp:
return b''
else:
diff --git a/sleekxmpp/util/stringprep_profiles.py b/sleekxmpp/util/stringprep_profiles.py
index ad89d4cc..84326bc3 100644
--- a/sleekxmpp/util/stringprep_profiles.py
+++ b/sleekxmpp/util/stringprep_profiles.py
@@ -16,9 +16,8 @@
from __future__ import unicode_literals
-import sys
import stringprep
-import unicodedata
+from unicodedata import ucd_3_2_0 as unicodedata
from sleekxmpp.util import unicode