Philipp Hörist pushed to branch master at gajim / python-nbxmpp
Commits: 8eb805d2 by Philipp Hörist at 2018-12-21T12:07:15Z Add jid validation
- - - - - 9da76c1b by Philipp Hörist at 2018-12-21T12:21:51Z Move code into setup.cfg
- - - - - 960a3ef0 by Philipp Hörist at 2018-12-21T12:24:22Z Update README.md
- - - - - ec4c203b by Philipp Hörist at 2018-12-21T12:29:09Z Use GPL v3 on new code
- - - - -
9 changed files:
- README.md - nbxmpp/misc.py - nbxmpp/modules/eme.py - nbxmpp/modules/http_auth.py - nbxmpp/protocol.py - nbxmpp/smacks.py - nbxmpp/util.py - setup.cfg - setup.py
Changes:
===================================== README.md ===================================== @@ -5,7 +5,7 @@ python-nbxmpp is a Python library that provides a way for Python applications to ## Features
* Asynchronous - * Supports ANONYMOUS, EXTERNAL, GSSAPI, SCRAM-SHA-1, DIGEST-MD5, PLAIN, and X-MESSENGER-OAUTH2 authentication mechanisms. + * Supports ANONYMOUS, EXTERNAL, GSSAPI, SCRAM-SHA-1(PLUS), SCRAM-SHA-256(PLUS) and PLAIN authentication mechanisms. * Supports connection via proxies * Suppors TLS * Supports [BOSH (XEP-0124)](https://xmpp.org/extensions/xep-0124.html)
===================================== nbxmpp/misc.py ===================================== @@ -4,7 +4,7 @@ # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 +# as published by the Free Software Foundation; either version 3 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful,
===================================== nbxmpp/modules/eme.py ===================================== @@ -4,7 +4,7 @@ # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 +# as published by the Free Software Foundation; either version 3 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful,
===================================== nbxmpp/modules/http_auth.py ===================================== @@ -4,7 +4,7 @@ # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 +# as published by the Free Software Foundation; either version 3 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful,
===================================== nbxmpp/protocol.py ===================================== @@ -650,6 +650,9 @@ class XMLNotWellFormed(StreamError): class InvalidStanza(Exception): pass
+class InvalidJid(Exception): + pass + stream_exceptions = {'bad-format': BadFormat, 'bad-namespace-prefix': BadNamespacePrefix, 'conflict': Conflict,
===================================== nbxmpp/smacks.py ===================================== @@ -4,7 +4,7 @@ # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 +# as published by the Free Software Foundation; either version 3 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful,
===================================== nbxmpp/util.py ===================================== @@ -4,7 +4,7 @@ # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 +# as published by the Free Software Foundation; either version 3 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, @@ -15,9 +15,19 @@ # You should have received a copy of the GNU General Public License # along with this program; If not, see http://www.gnu.org/licenses/.
+import logging +import socket import base64 from collections import namedtuple
+import precis_i18n.codec + +from nbxmpp.protocol import JID +from nbxmpp.protocol import InvalidJid +from nbxmpp.stringprepare import nameprep + +log = logging.getLogger('nbxmpp.util') + StanzaHandler = namedtuple('StanzaHandler', 'name callback typ ns xmlns system priority') StanzaHandler.__new__.__defaults__ = ('', '', None, False, 50)
@@ -84,3 +94,74 @@ def get_property_dict(name): if name == 'iq': return IqPropertyDict() return PropertyBase() + + +def validate_jid(jid_string): + jid = JID(jid_string) + return prep(jid.getNode(), + jid.getDomain(), + jid.getResource()) + + +def prep(user, server, resource): + """ + Perform stringprep on all JID fragments and return the full jid + """ + + ip_address = False + + try: + socket.inet_aton(server) + ip_address = True + except socket.error: + pass + + if not ip_address and hasattr(socket, 'inet_pton'): + try: + socket.inet_pton(socket.AF_INET6, server.strip('[]')) + server = '[%s]' % server.strip('[]') + ip_address = True + except (socket.error, ValueError): + pass + + if not ip_address: + if server is not None: + if server.endswith('.'): # RFC7622, 3.2 + server = server[:-1] + if not server or len(server.encode('utf-8')) > 1023: + raise InvalidJid('Server must be between 1 and 1023 bytes') + try: + server = nameprep.prepare(server) + except UnicodeError: + raise InvalidJid('Invalid character in hostname') + else: + raise InvalidJid('Server address required') + + if user is not None: + if not user or len(user.encode('utf-8')) > 1023: + raise InvalidJid('Username must be between 1 and 1023 bytes') + try: + user = user.encode('UsernameCaseMapped').decode('utf-8') + except UnicodeError: + raise InvalidJid('Invalid character in username') + else: + user = None + + if resource is not None: + if not resource or len(resource.encode('utf-8')) > 1023: + raise InvalidJid('Resource must be between 1 and 1023 bytes') + try: + resource = resource.encode('OpaqueString').decode('utf-8') + except UnicodeError: + raise InvalidJid('Invalid character in resource') + else: + resource = None + + if user: + if resource: + return '%s@%s/%s' % (user, server, resource) + return '%s@%s' % (user, server) + + if resource: + return '%s/%s' % (server, resource) + return server
===================================== setup.cfg ===================================== @@ -1,2 +1,21 @@ [metadata] +name = nbxmpp +version = attr: nbxmpp.__version__ +description = Non blocking Jabber/XMPP module +author = Yann Leboulanger, Philipp Hoerist +author_email = gajim-devel@gajim.org +url = http://dev.gajim.org/gajim/python-nbxmpp license_file = COPYING +license = GPL v3 +classifiers = + Programming Language :: Python :: 3 + +[options] +python_requires = >=3.5 +packages = find: +install_requires = + precis-i18n>=1.0.0 + +[options.packages.find] +exclude = + test*
===================================== setup.py ===================================== @@ -2,11 +2,4 @@
from setuptools import setup
-setup(name='nbxmpp', - version='0.9.90.2', - description='Non blocking Jabber/XMPP module', - author='Yann Leboulanger', - author_email='asterix@lagaule.org', - url='http://dev.gajim.org/gajim/python-nbxmpp', - packages=['nbxmpp', 'nbxmpp.modules'], - ) +setup()
View it on GitLab: https://dev.gajim.org/gajim/python-nbxmpp/compare/5bea073ba9ef04f5fa58e440ae...