# Path: wokkel/generic.py
# class DeferredXmlStreamFactory(BootstrapMixin, protocol.ClientFactory):
#     protocol = xmlstream.XmlStream
# 
#     def __init__(self, authenticator):
#         BootstrapMixin.__init__(self)
# 
#         self.authenticator = authenticator
# 
#         deferred = defer.Deferred()
#         self.deferred = deferred
#         self.addBootstrap(xmlstream.STREAM_AUTHD_EVENT, self.deferred.callback)
#         self.addBootstrap(xmlstream.INIT_FAILED_EVENT, deferred.errback)
# 
# 
#     def buildProtocol(self, addr):
#         """
#         Create an instance of XmlStream.
# 
#         A new authenticator instance will be created and passed to the new
#         XmlStream. Registered bootstrap event observers are installed as well.
#         """
#         xs = self.protocol(self.authenticator)
#         xs.factory = self
#         self.installBootstraps(xs)
#         return xs
# 
# 
#     def clientConnectionFailed(self, connector, reason):
#         self.deferred.errback(reason)
# 
# class XmlPipe(object):
#     """
#     XML stream pipe.
# 
#     Connects two objects that communicate stanzas through an XML stream like
#     interface. Each of the ends of the pipe (sink and source) can be used to
#     send XML stanzas to the other side, or add observers to process XML stanzas
#     that were sent from the other side.
# 
#     XML pipes are usually used in place of regular XML streams that are
#     transported over TCP. This is the reason for the use of the names source
#     and sink for both ends of the pipe. The source side corresponds with the
#     entity that initiated the TCP connection, whereas the sink corresponds with
#     the entity that accepts that connection. In this object, though, the source
#     and sink are treated equally.
# 
#     Unlike Jabber
#     L{XmlStream<twisted.words.protocols.jabber.xmlstream.XmlStream>}s, the sink
#     and source objects are assumed to represent an eternal connected and
#     initialized XML stream. As such, events corresponding to connection,
#     disconnection, initialization and stream errors are not dispatched or
#     processed.
# 
#     @ivar source: Source XML stream.
#     @ivar sink: Sink XML stream.
#     """
# 
#     def __init__(self):
#         self.source = utility.EventDispatcher()
#         self.sink = utility.EventDispatcher()
#         self.source.send = lambda obj: self.sink.dispatch(obj)
#         self.sink.send = lambda obj: self.source.dispatch(obj)


import binascii
import hmac
from hashlib import sha256
from zope.interface import implementer
from twisted.internet import defer, reactor
from twisted.names.srvconnect import SRVConnector
from twisted.python import log, randbytes
from twisted.python.compat import iteritems, unicode
from twisted.words.protocols.jabber import error, ijabber, jid, xmlstream
from twisted.words.xish import domish
from wokkel.generic import DeferredXmlStreamFactory, XmlPipe
            self.xmlstream.dispatch(self.xmlstream,
                                    xmlstream.STREAM_AUTHD_EVENT)

        def invalid(failure):
            log.err(failure)
            reply('invalid')

        receivingServer = result['to']
        originatingServer = result['from']
        key = unicode(result)

        d = self.service.validateConnection(receivingServer, originatingServer,
                                            self.xmlstream.sid, key)
        d.addCallbacks(valid, invalid)
        return d



class DeferredS2SClientFactory(DeferredXmlStreamFactory):
    """
    Deferred firing factory for initiating XMPP server-to-server connection.

    The deferred has its callbacks called upon succesful authentication with
    the other server. In case of failed authentication or connection, the
    deferred will have its errbacks called instead.
    """

    logTraffic = False

    def __init__(self, authenticator):

# Path: wokkel/generic.py
# class DeferredXmlStreamFactory(BootstrapMixin, protocol.ClientFactory):
#     protocol = xmlstream.XmlStream
# 
#     def __init__(self, authenticator):
#         BootstrapMixin.__init__(self)
# 
#         self.authenticator = authenticator
# 
#         deferred = defer.Deferred()
#         self.deferred = deferred
#         self.addBootstrap(xmlstream.STREAM_AUTHD_EVENT, self.deferred.callback)
#         self.addBootstrap(xmlstream.INIT_FAILED_EVENT, deferred.errback)
# 
# 
#     def buildProtocol(self, addr):
#         """
#         Create an instance of XmlStream.
# 
#         A new authenticator instance will be created and passed to the new
#         XmlStream. Registered bootstrap event observers are installed as well.
#         """
#         xs = self.protocol(self.authenticator)
#         xs.factory = self
#         self.installBootstraps(xs)
#         return xs
# 
# 
#     def clientConnectionFailed(self, connector, reason):
#         self.deferred.errback(reason)
# 
# class XmlPipe(object):
#     """
#     XML stream pipe.
# 
#     Connects two objects that communicate stanzas through an XML stream like
#     interface. Each of the ends of the pipe (sink and source) can be used to
#     send XML stanzas to the other side, or add observers to process XML stanzas
#     that were sent from the other side.
# 
#     XML pipes are usually used in place of regular XML streams that are
#     transported over TCP. This is the reason for the use of the names source
#     and sink for both ends of the pipe. The source side corresponds with the
#     entity that initiated the TCP connection, whereas the sink corresponds with
#     the entity that accepts that connection. In this object, though, the source
#     and sink are treated equally.
# 
#     Unlike Jabber
#     L{XmlStream<twisted.words.protocols.jabber.xmlstream.XmlStream>}s, the sink
#     and source objects are assumed to represent an eternal connected and
#     initialized XML stream. As such, events corresponding to connection,
#     disconnection, initialization and stream errors are not dispatched or
#     processed.
# 
#     @ivar source: Source XML stream.
#     @ivar sink: Sink XML stream.
#     """
# 
#     def __init__(self):
#         self.source = utility.EventDispatcher()
#         self.sink = utility.EventDispatcher()
#         self.source.send = lambda obj: self.sink.dispatch(obj)
#         self.sink.send = lambda obj: self.source.dispatch(obj)


# Path: wokkel/server.py
import binascii
import hmac
from hashlib import sha256
from zope.interface import implementer
from twisted.internet import defer, reactor
from twisted.names.srvconnect import SRVConnector
from twisted.python import log, randbytes
from twisted.python.compat import iteritems, unicode
from twisted.words.protocols.jabber import error, ijabber, jid, xmlstream
from twisted.words.xish import domish
from wokkel.generic import DeferredXmlStreamFactory, XmlPipe

            self.xmlstream.dispatch(self.xmlstream,
                                    xmlstream.STREAM_AUTHD_EVENT)

        def invalid(failure):
            log.err(failure)
            reply('invalid')

        receivingServer = result['to']
        originatingServer = result['from']
        key = unicode(result)

        d = self.service.validateConnection(receivingServer, originatingServer,
                                            self.xmlstream.sid, key)
        d.addCallbacks(valid, invalid)
        return d



class DeferredS2SClientFactory(DeferredXmlStreamFactory):
    """
    Deferred firing factory for initiating XMPP server-to-server connection.

    The deferred has its callbacks called upon succesful authentication with
    the other server. In case of failed authentication or connection, the
    deferred will have its errbacks called instead.
    """

    logTraffic = False

    def __init__(self, authenticator):

        DeferredXmlStreamFactory.__init__(self, authenticator)