source:
ralphm-patches/py3-server.patch
Last change on this file was 78:361e2111a663, checked in by Ralph Meijer <ralphm@…>, 6 years ago | |
---|---|
File size: 4.9 KB |
-
wokkel/server.py
# HG changeset patch # Parent 5f037cb753354f3ed7a202798e3522a7247fb832 diff --git a/wokkel/server.py b/wokkel/server.py
a b 11 11 of the used terminology. 12 12 """ 13 13 14 # hashlib is new in Python 2.5, try that first. 15 try: 16 from hashlib import sha256 17 digestmod = sha256 18 except ImportError: 19 import Crypto.Hash.SHA256 as digestmod 20 sha256 = digestmod.new 14 import binascii 15 16 from hashlib import sha256 17 digestmod = sha256 21 18 22 19 import hmac 23 20 24 from zope.interface import implement s21 from zope.interface import implementer 25 22 26 23 from twisted.internet import defer, reactor 27 24 from twisted.names.srvconnect import SRVConnector 28 25 from twisted.python import log, randbytes 26 from twisted.python.compat import iteritems, unicode 29 27 from twisted.words.protocols.jabber import error, ijabber, jid, xmlstream 30 28 from twisted.words.xish import domish 31 29 … … 43 41 44 42 @param secret: the shared secret known to the Originating Server and 45 43 Authoritive Server. 46 @type secret: C{str}44 @type secret: L{unicode} 47 45 @param receivingServer: the Receiving Server host name. 48 @type receivingServer: C{str}46 @type receivingServer: L{unicode} 49 47 @param originatingServer: the Originating Server host name. 50 @type originatingServer: C{str}48 @type originatingServer: L{unicode} 51 49 @param streamID: the Stream ID as generated by the Receiving Server. 52 @type streamID: C{str}50 @type streamID: L{unicode} 53 51 @return: hexadecimal digest of the generated key. 54 52 @type: C{str} 55 53 """ 56 54 57 55 hashObject = sha256() 58 hashObject.update(secret )56 hashObject.update(secret.encode('ascii')) 59 57 hashedSecret = hashObject.hexdigest() 60 58 message = " ".join([receivingServer, originatingServer, streamID]) 61 hash = hmac.HMAC(hashedSecret, message, digestmod=digestmod) 59 hash = hmac.HMAC(hashedSecret.encode('ascii'), 60 message.encode('ascii'), 61 digestmod=digestmod) 62 62 return hash.hexdigest() 63 63 64 64 … … 77 77 def wrappedObserver(element): 78 78 try: 79 79 observer(element) 80 except error.StreamError ,exc:80 except error.StreamError as exc: 81 81 xs.sendStreamError(exc) 82 82 except: 83 83 log.err() … … 107 107 108 108 109 109 110 @implementer(ijabber.IInitiatingInitializer) 110 111 class OriginatingDialbackInitializer(object): 111 112 """ 112 113 Server Dialback Initializer for the Orginating Server. 113 114 """ 114 115 115 implements(ijabber.IInitiatingInitializer)116 117 116 _deferred = None 118 117 119 118 def __init__(self, xs, thisHost, otherHost, secret): … … 160 159 161 160 162 161 162 @implementer(ijabber.IInitiatingInitializer) 163 163 class ReceivingDialbackInitializer(object): 164 164 """ 165 165 Server Dialback Initializer for the Receiving Server. 166 166 """ 167 167 168 implements(ijabber.IInitiatingInitializer)169 170 168 _deferred = None 171 169 172 170 def __init__(self, xs, thisHost, otherHost, originalStreamID, key): … … 328 326 def streamStarted(self, rootElement): 329 327 xmlstream.ListenAuthenticator.streamStarted(self, rootElement) 330 328 331 # Compatibility fix for pre-8.2 implementations of ListenAuthenticator332 if not self.xmlstream.sid:333 self.xmlstream.sid = randbytes.secureRandom(8).encode('hex')334 335 329 if self.xmlstream.thisEntity: 336 330 targetDomain = self.xmlstream.thisEntity.host 337 331 else: … … 347 341 try: 348 342 if xmlstream.NS_STREAMS != rootElement.uri or \ 349 343 self.namespace != self.xmlstream.namespace or \ 350 ('db', NS_DIALBACK) not in rootElement.localPrefixes.iteritems():344 ('db', NS_DIALBACK) not in iteritems(rootElement.localPrefixes): 351 345 raise error.StreamError('invalid-namespace') 352 346 353 347 if targetDomain and targetDomain not in self.service.domains: 354 348 raise error.StreamError('host-unknown') 355 except error.StreamError ,exc:349 except error.StreamError as exc: 356 350 prepareStream(self.service.defaultDomain) 357 351 self.xmlstream.sendStreamError(exc) 358 352 return … … 575 569 if secret is not None: 576 570 self.secret = secret 577 571 else: 578 self.secret = randbytes.secureRandom(16).encode('hex')572 self.secret = binascii.hexlify(randbytes.secureRandom(16)) 579 573 580 574 self._outgoingStreams = {} 581 575 self._outgoingQueues = {} -
wokkel/test/test_server.py
diff --git a/wokkel/test/test_server.py b/wokkel/test/test_server.py
a b 448 448 self.service.dispatch(self.xmlstream, stanza) 449 449 450 450 self.assertEqual(1, len(errors)) 451 452 def test_generatedSecret(self): 453 self.router = component.Router() 454 self.service = server.ServerService(self.router, 455 domain='example.org') 456 self.assertEqual(32, len(self.service.secret))
Note: See TracBrowser
for help on using the repository browser.