Changeset 206:a32614b03670 for wokkel


Ignore:
Timestamp:
Oct 3, 2016, 8:49:10 AM (3 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
rebase_source:
61f8edbf0d012a63234c405576ec2f5cdb67bab2
Message:

imported patch py3-server.patch

Location:
wokkel
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wokkel/server.py

    r184 r206  
    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
    21 
     14from __future__ import division, absolute_import
     15
     16import binascii
     17from hashlib import sha256
    2218import hmac
    2319
    24 from zope.interface import implements
     20from zope.interface import implementer
    2521
    2622from twisted.internet import defer, reactor
    2723from twisted.names.srvconnect import SRVConnector
    2824from twisted.python import log, randbytes
     25from twisted.python.compat import iteritems, unicode
    2926from twisted.words.protocols.jabber import error, ijabber, jid, xmlstream
    3027from twisted.words.xish import domish
     
    4441    @param secret: the shared secret known to the Originating Server and
    4542                   Authoritive Server.
    46     @type secret: C{str}
     43    @type secret: L{unicode}
    4744    @param receivingServer: the Receiving Server host name.
    48     @type receivingServer: C{str}
     45    @type receivingServer: L{unicode}
    4946    @param originatingServer: the Originating Server host name.
    50     @type originatingServer: C{str}
     47    @type originatingServer: L{unicode}
    5148    @param streamID: the Stream ID as generated by the Receiving Server.
    52     @type streamID: C{str}
     49    @type streamID: L{unicode}
    5350    @return: hexadecimal digest of the generated key.
    5451    @type: C{str}
     
    5653
    5754    hashObject = sha256()
    58     hashObject.update(secret)
     55    hashObject.update(secret.encode('ascii'))
    5956    hashedSecret = hashObject.hexdigest()
    6057    message = " ".join([receivingServer, originatingServer, streamID])
    61     hash = hmac.HMAC(hashedSecret, message, digestmod=digestmod)
     58    hash = hmac.HMAC(hashedSecret.encode('ascii'),
     59                     message.encode('ascii'),
     60                     digestmod=sha256)
    6261    return hash.hexdigest()
    6362
     
    7877        try:
    7978            observer(element)
    80         except error.StreamError, exc:
     79        except error.StreamError as exc:
    8180            xs.sendStreamError(exc)
    8281        except:
     
    108107
    109108
     109@implementer(ijabber.IInitiatingInitializer)
    110110class OriginatingDialbackInitializer(object):
    111111    """
    112112    Server Dialback Initializer for the Orginating Server.
    113113    """
    114 
    115     implements(ijabber.IInitiatingInitializer)
    116114
    117115    _deferred = None
     
    161159
    162160
     161@implementer(ijabber.IInitiatingInitializer)
    163162class ReceivingDialbackInitializer(object):
    164163    """
    165164    Server Dialback Initializer for the Receiving Server.
    166165    """
    167 
    168     implements(ijabber.IInitiatingInitializer)
    169166
    170167    _deferred = None
     
    329326        xmlstream.ListenAuthenticator.streamStarted(self, rootElement)
    330327
    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 
    335328        if self.xmlstream.thisEntity:
    336329            targetDomain = self.xmlstream.thisEntity.host
     
    348341            if xmlstream.NS_STREAMS != rootElement.uri or \
    349342               self.namespace != self.xmlstream.namespace or \
    350                ('db', NS_DIALBACK) not in rootElement.localPrefixes.iteritems():
     343               ('db', NS_DIALBACK) not in iteritems(rootElement.localPrefixes):
    351344                raise error.StreamError('invalid-namespace')
    352345
    353346            if targetDomain and targetDomain not in self.service.domains:
    354347                raise error.StreamError('host-unknown')
    355         except error.StreamError, exc:
     348        except error.StreamError as exc:
    356349            prepareStream(self.service.defaultDomain)
    357350            self.xmlstream.sendStreamError(exc)
     
    576569            self.secret = secret
    577570        else:
    578             self.secret = randbytes.secureRandom(16).encode('hex')
     571            self.secret = binascii.hexlify(randbytes.secureRandom(16))
    579572
    580573        self._outgoingStreams = {}
  • wokkel/test/test_server.py

    r96 r206  
    66"""
    77
     8from __future__ import division, absolute_import
    89from twisted.internet import defer
    910from twisted.python import failure
     
    449450
    450451        self.assertEqual(1, len(errors))
     452
     453
     454    def test_generatedSecret(self):
     455        self.router = component.Router()
     456        self.service = server.ServerService(self.router,
     457                                            domain='example.org')
     458        self.assertEqual(32, len(self.service.secret))
Note: See TracChangeset for help on using the changeset viewer.