I am trying to understand how it works in the context of twisted.web and
Resources, but I always get an exception on the portal object
Here is the core code:

    passwords = {"admin":"password"}
    users = {"admin":"Administrator"}

    realm = TestRealm(users)

    p = portal.Portal(TestRealm(users),(PasswordDictChecker(passwords),))
    credentialFactory = BasicCredentialFactory("CorePost")
    resource = HTTPAuthSessionWrapper(portal, [credentialFactory])

    print "Running..."

    factory = Site(resource)
    reactor.listenTCP(8084, factory)
    reactor.run()

Error:

Unhandled Error
Traceback (most recent call last):
  File
"/usr/local/lib/python2.7/dist-packages/Twisted-12.0.0-py2.7-linux-x86_64.egg/twisted/protocols/basic.py",
line 564, in dataReceived
    why = self.lineReceived(line)
  File
"/usr/local/lib/python2.7/dist-packages/Twisted-12.0.0-py2.7-linux-x86_64.egg/twisted/web/http.py",
line 1551, in lineReceived
    self.allContentReceived()
  File
"/usr/local/lib/python2.7/dist-packages/Twisted-12.0.0-py2.7-linux-x86_64.egg/twisted/web/http.py",
line 1618, in allContentReceived
    req.requestReceived(command, path, version)
  File
"/usr/local/lib/python2.7/dist-packages/Twisted-12.0.0-py2.7-linux-x86_64.egg/twisted/web/http.py",
line 773, in requestReceived
    self.process()
--- <exception caught here> ---
  File
"/usr/local/lib/python2.7/dist-packages/Twisted-12.0.0-py2.7-linux-x86_64.egg/twisted/web/server.py",
line 131, in process
    resrc = self.site.getResourceFor(self)
  File
"/usr/local/lib/python2.7/dist-packages/Twisted-12.0.0-py2.7-linux-x86_64.egg/twisted/web/server.py",
line 562, in getResourceFor
    return resource.getChildForRequest(self.resource, request)
  File
"/usr/local/lib/python2.7/dist-packages/Twisted-12.0.0-py2.7-linux-x86_64.egg/twisted/web/resource.py",
line 65, in getChildForRequest
    resource = resource.getChildWithDefault(pathElement, request)
  File
"/usr/local/lib/python2.7/dist-packages/Twisted-12.0.0-py2.7-linux-x86_64.egg/twisted/web/_auth/wrapper.py",
line 142, in getChildWithDefault
    return self._authorizedResource(request)
  File
"/usr/local/lib/python2.7/dist-packages/Twisted-12.0.0-py2.7-linux-x86_64.egg/twisted/web/_auth/wrapper.py",
line 106, in _authorizedResource
    return util.DeferredResource(self._login(Anonymous()))
  File
"/usr/local/lib/python2.7/dist-packages/Twisted-12.0.0-py2.7-linux-x86_64.egg/twisted/web/_auth/wrapper.py",
line 152, in _login
    d = self._portal.login(credentials, None, IResource)
*exceptions.AttributeError: 'module' object has no attribute 'login'*
*
*
Attached the full file.

Not sure I understand what I am doing right here. The app starts up fine,
but throws the error on any request.
Also, how do I hook up actual Resources?
Do I do it do the HttpAuthSessionWrapper? It is an IResources, but
complains that it does not have a putChild() on it?
Should my TestRealm extend a Resource and I add my child Resource there?

Sorry for all the questions, but the docs don't really cover the A-Z
scenario of getting a running twisted.web app with Http auth... :-(

Jacek
'''
Server tests
@author: jacekf
'''

from twisted.cred import portal, checkers, error as credError
from zope.interface import implements, Interface, Attribute
from twisted.internet import reactor, defer
from twisted.web.resource import Resource
from twisted.web.server import Site
from twisted.web.guard import HTTPAuthSessionWrapper, BasicCredentialFactory

class IAvatar(Interface):
    username = Attribute("""User name""")

class PasswordDictChecker:
    implements(checkers.ICredentialsChecker)
    
    def __init__(self,passwords):
        self.__passwords = passwords
        self.credentialInterfaces = (IAvatar,)
        
    def requestAvatarId(self, credentials):
        username = credentials.username
        if self.__passwords.has_key(username):
            if credentials.password == self.__passwords[username]:
                return defer.succeed(username)
            else:
                credError.UnauthorizedLogin("Access denied")
        else:
            credError.UnauthorizedLogin("Access denied")
            
    
class TestAvatar():
    implements(IAvatar)
    
    def __init__(self,username):
        self.username = username
                
class TestRealm():
    implements(portal.IRealm)

    def __init__(self,users):
        self.__users = users
        
    def requestAvatar(self, avatarId, mind, *interfaces):
        if IAvatar in interfaces:
            logout = lambda: None
            return (IAvatar,TestAvatar(avatarId),logout)
        else:                
            raise KeyError("None of the requested interfaces are supported")

class TestResource(Resource):
    isLeaf = True
    
    def __init__(self,schema=None,filters=()):
        Resource.__init__(self)

    def render_GET(self,request):
        return "OK"
    

def run_security_app():
    passwords = {"admin":"password"}
    users = {"admin":"Administrator"}
    
    realm = TestRealm(users)
    
    p = portal.Portal(TestRealm(users),(PasswordDictChecker(passwords),))
    credentialFactory = BasicCredentialFactory("CorePost")
    resource = HTTPAuthSessionWrapper(portal, [credentialFactory])
    
    print "Running..."
    
    factory = Site(resource)
    reactor.listenTCP(8084, factory)    
    reactor.run()                       

    
if __name__ == "__main__":
    run_security_app()
_______________________________________________
Twisted-web mailing list
[email protected]
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web

Reply via email to