source: ralphm-patches/py3-server.patch @ 83:255aae0cf8c5

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

Start port to Python 3.

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  
    1111of the used terminology.
    1212"""
    1313
    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
     14import binascii
     15
     16from hashlib import sha256
     17digestmod = sha256
    2118
    2219import hmac
    2320
    24 from zope.interface import implements
     21from zope.interface import implementer
    2522
    2623from twisted.internet import defer, reactor
    2724from twisted.names.srvconnect import SRVConnector
    2825from twisted.python import log, randbytes
     26from twisted.python.compat import iteritems, unicode
    2927from twisted.words.protocols.jabber import error, ijabber, jid, xmlstream
    3028from twisted.words.xish import domish
    3129
     
    4341
    4442    @param secret: the shared secret known to the Originating Server and
    4543                   Authoritive Server.
    46     @type secret: C{str}
     44    @type secret: L{unicode}
    4745    @param receivingServer: the Receiving Server host name.
    48     @type receivingServer: C{str}
     46    @type receivingServer: L{unicode}
    4947    @param originatingServer: the Originating Server host name.
    50     @type originatingServer: C{str}
     48    @type originatingServer: L{unicode}
    5149    @param streamID: the Stream ID as generated by the Receiving Server.
    52     @type streamID: C{str}
     50    @type streamID: L{unicode}
    5351    @return: hexadecimal digest of the generated key.
    5452    @type: C{str}
    5553    """
    5654
    5755    hashObject = sha256()
    58     hashObject.update(secret)
     56    hashObject.update(secret.encode('ascii'))
    5957    hashedSecret = hashObject.hexdigest()
    6058    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)
    6262    return hash.hexdigest()
    6363
    6464
     
    7777    def wrappedObserver(element):
    7878        try:
    7979            observer(element)
    80         except error.StreamError, exc:
     80        except error.StreamError as exc:
    8181            xs.sendStreamError(exc)
    8282        except:
    8383            log.err()
     
    107107
    108108
    109109
     110@implementer(ijabber.IInitiatingInitializer)
    110111class OriginatingDialbackInitializer(object):
    111112    """
    112113    Server Dialback Initializer for the Orginating Server.
    113114    """
    114115
    115     implements(ijabber.IInitiatingInitializer)
    116 
    117116    _deferred = None
    118117
    119118    def __init__(self, xs, thisHost, otherHost, secret):
     
    160159
    161160
    162161
     162@implementer(ijabber.IInitiatingInitializer)
    163163class ReceivingDialbackInitializer(object):
    164164    """
    165165    Server Dialback Initializer for the Receiving Server.
    166166    """
    167167
    168     implements(ijabber.IInitiatingInitializer)
    169 
    170168    _deferred = None
    171169
    172170    def __init__(self, xs, thisHost, otherHost, originalStreamID, key):
     
    328326    def streamStarted(self, rootElement):
    329327        xmlstream.ListenAuthenticator.streamStarted(self, rootElement)
    330328
    331         # Compatibility fix for pre-8.2 implementations of ListenAuthenticator
    332         if not self.xmlstream.sid:
    333             self.xmlstream.sid = randbytes.secureRandom(8).encode('hex')
    334 
    335329        if self.xmlstream.thisEntity:
    336330            targetDomain = self.xmlstream.thisEntity.host
    337331        else:
     
    347341        try:
    348342            if xmlstream.NS_STREAMS != rootElement.uri or \
    349343               self.namespace != self.xmlstream.namespace or \
    350                ('db', NS_DIALBACK) not in rootElement.localPrefixes.iteritems():
     344               ('db', NS_DIALBACK) not in iteritems(rootElement.localPrefixes):
    351345                raise error.StreamError('invalid-namespace')
    352346
    353347            if targetDomain and targetDomain not in self.service.domains:
    354348                raise error.StreamError('host-unknown')
    355         except error.StreamError, exc:
     349        except error.StreamError as exc:
    356350            prepareStream(self.service.defaultDomain)
    357351            self.xmlstream.sendStreamError(exc)
    358352            return
     
    575569        if secret is not None:
    576570            self.secret = secret
    577571        else:
    578             self.secret = randbytes.secureRandom(16).encode('hex')
     572            self.secret = binascii.hexlify(randbytes.secureRandom(16))
    579573
    580574        self._outgoingStreams = {}
    581575        self._outgoingQueues = {}
  • wokkel/test/test_server.py

    diff --git a/wokkel/test/test_server.py b/wokkel/test/test_server.py
    a b  
    448448        self.service.dispatch(self.xmlstream, stanza)
    449449
    450450        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.