[Twisted-web] XML-RPC and Authentication

Andrey alpha04 at netvigator.com
Sat May 26 16:09:24 EDT 2007


In the xmlrpc auth with auth.wrapper example below, how to pass the avatar 
information to the EXAMPLE(XML_RPC) class?
e.g.
=============================
xmlrpc_welcome(){
    return "Welcome "+username
}
=============================
Cheers
Andrey


from zope.interface import Interface, implements
from twisted.cred import portal
from twisted.cred import checkers
from twisted.web2 import channel
from twisted.web2 import http
from twisted.web2 import responsecode
from twisted.web2 import server
from twisted.web2 import xmlrpc
from twisted.web2.auth import digest
from twisted.web2.auth import basic
from twisted.web2.auth import wrapper

from twisted.application import service, strports

class IHTTPUser(Interface):
    pass

class HTTPUser(object):
    implements(IHTTPUser)

class HTTPAuthRealm(object):
    implements(portal.IRealm)

    def requestAvatar(self, avatarId, mind, *interfaces):
        if IHTTPUser in interfaces:            return IHTTPUser, HTTPUser()

        raise NotImplementedError("Only IHTTPUser interface is supported")

class Example(xmlrpc.XMLRPC):
    """An example object to be published."""

    addSlash = True

    def xmlrpc_echo(self, request, x):
        """Return all passed args."""
        return x

    def xmlrpc_add(self, request, a, b):
        """Return sum of arguments."""
        return a + b

portal = portal.Portal(HTTPAuthRealm())
checker = checkers.InMemoryUsernamePasswordDatabaseDontUse(guest='guest123')
portal.registerChecker(checker)
rsrc = Example()
credFactories = (basic.BasicCredentialFactory('My Realm'),
    digest.DigestCredentialFactory('md5', 'My Realm'))
ifaces = (IHTTPUser,)
root = wrapper.HTTPAuthResource(rsrc, credFactories, portal, ifaces)

site = server.Site(root)
application = service.Application("XML-RPC Auth Demo")
s = strports.service('tcp:8080', channel.HTTPFactory(site))
s.setServiceParent(application) 






More information about the Twisted-web mailing list