source: ralphm-patches/py3-server.patch @ 78:361e2111a663

Last change on this file since 78:361e2111a663 was 78:361e2111a663, checked in by Ralph Meijer <ralphm@…>, 6 years ago

Start port to Python 3.

File size: 4.9 KB
RevLine 
[78]1# HG changeset patch
2# Parent  5f037cb753354f3ed7a202798e3522a7247fb832
3
4diff --git a/wokkel/server.py b/wokkel/server.py
5--- a/wokkel/server.py
6+++ b/wokkel/server.py
7@@ -11,21 +11,19 @@
8 of the used terminology.
9 """
10 
11-# hashlib is new in Python 2.5, try that first.
12-try:
13-    from hashlib import sha256
14-    digestmod = sha256
15-except ImportError:
16-    import Crypto.Hash.SHA256 as digestmod
17-    sha256 = digestmod.new
18+import binascii
19+
20+from hashlib import sha256
21+digestmod = sha256
22 
23 import hmac
24 
25-from zope.interface import implements
26+from zope.interface import implementer
27 
28 from twisted.internet import defer, reactor
29 from twisted.names.srvconnect import SRVConnector
30 from twisted.python import log, randbytes
31+from twisted.python.compat import iteritems, unicode
32 from twisted.words.protocols.jabber import error, ijabber, jid, xmlstream
33 from twisted.words.xish import domish
34 
35@@ -43,22 +41,24 @@
36 
37     @param secret: the shared secret known to the Originating Server and
38                    Authoritive Server.
39-    @type secret: C{str}
40+    @type secret: L{unicode}
41     @param receivingServer: the Receiving Server host name.
42-    @type receivingServer: C{str}
43+    @type receivingServer: L{unicode}
44     @param originatingServer: the Originating Server host name.
45-    @type originatingServer: C{str}
46+    @type originatingServer: L{unicode}
47     @param streamID: the Stream ID as generated by the Receiving Server.
48-    @type streamID: C{str}
49+    @type streamID: L{unicode}
50     @return: hexadecimal digest of the generated key.
51     @type: C{str}
52     """
53 
54     hashObject = sha256()
55-    hashObject.update(secret)
56+    hashObject.update(secret.encode('ascii'))
57     hashedSecret = hashObject.hexdigest()
58     message = " ".join([receivingServer, originatingServer, streamID])
59-    hash = hmac.HMAC(hashedSecret, message, digestmod=digestmod)
60+    hash = hmac.HMAC(hashedSecret.encode('ascii'),
61+                     message.encode('ascii'),
62+                     digestmod=digestmod)
63     return hash.hexdigest()
64 
65 
66@@ -77,7 +77,7 @@
67     def wrappedObserver(element):
68         try:
69             observer(element)
70-        except error.StreamError, exc:
71+        except error.StreamError as exc:
72             xs.sendStreamError(exc)
73         except:
74             log.err()
75@@ -107,13 +107,12 @@
76 
77 
78 
79+@implementer(ijabber.IInitiatingInitializer)
80 class OriginatingDialbackInitializer(object):
81     """
82     Server Dialback Initializer for the Orginating Server.
83     """
84 
85-    implements(ijabber.IInitiatingInitializer)
86-
87     _deferred = None
88 
89     def __init__(self, xs, thisHost, otherHost, secret):
90@@ -160,13 +159,12 @@
91 
92 
93 
94+@implementer(ijabber.IInitiatingInitializer)
95 class ReceivingDialbackInitializer(object):
96     """
97     Server Dialback Initializer for the Receiving Server.
98     """
99 
100-    implements(ijabber.IInitiatingInitializer)
101-
102     _deferred = None
103 
104     def __init__(self, xs, thisHost, otherHost, originalStreamID, key):
105@@ -328,10 +326,6 @@
106     def streamStarted(self, rootElement):
107         xmlstream.ListenAuthenticator.streamStarted(self, rootElement)
108 
109-        # Compatibility fix for pre-8.2 implementations of ListenAuthenticator
110-        if not self.xmlstream.sid:
111-            self.xmlstream.sid = randbytes.secureRandom(8).encode('hex')
112-
113         if self.xmlstream.thisEntity:
114             targetDomain = self.xmlstream.thisEntity.host
115         else:
116@@ -347,12 +341,12 @@
117         try:
118             if xmlstream.NS_STREAMS != rootElement.uri or \
119                self.namespace != self.xmlstream.namespace or \
120-               ('db', NS_DIALBACK) not in rootElement.localPrefixes.iteritems():
121+               ('db', NS_DIALBACK) not in iteritems(rootElement.localPrefixes):
122                 raise error.StreamError('invalid-namespace')
123 
124             if targetDomain and targetDomain not in self.service.domains:
125                 raise error.StreamError('host-unknown')
126-        except error.StreamError, exc:
127+        except error.StreamError as exc:
128             prepareStream(self.service.defaultDomain)
129             self.xmlstream.sendStreamError(exc)
130             return
131@@ -575,7 +569,7 @@
132         if secret is not None:
133             self.secret = secret
134         else:
135-            self.secret = randbytes.secureRandom(16).encode('hex')
136+            self.secret = binascii.hexlify(randbytes.secureRandom(16))
137 
138         self._outgoingStreams = {}
139         self._outgoingQueues = {}
140diff --git a/wokkel/test/test_server.py b/wokkel/test/test_server.py
141--- a/wokkel/test/test_server.py
142+++ b/wokkel/test/test_server.py
143@@ -448,3 +448,9 @@
144         self.service.dispatch(self.xmlstream, stanza)
145 
146         self.assertEqual(1, len(errors))
147+
148+    def test_generatedSecret(self):
149+        self.router = component.Router()
150+        self.service = server.ServerService(self.router,
151+                                            domain='example.org')
152+        self.assertEqual(32, len(self.service.secret))
Note: See TracBrowser for help on using the repository browser.