[Twisted-Python] I am having severe problems with guard.

Francisco Miguel Colaço fcolaco at demnet.ubi.pt
Wed Aug 13 21:45:30 EDT 2003

  I am having severe problems with guard.  I am making a site in which
users must be authenticated, and a session must be following and
reacting to the user's activities.

  I have made the following module.

from twisted.cred.portal import Portal, IRealm
from twisted.web.util import Redirect

import page, model
reload (page)
reload (model)

class Realm:
    __implements__ = IRealm    
    def requestAvatar (self, id, mind, *interfaces):
        if IResource not in interfaces:
            raise NotImplementedError ('I cannot do non-web logins
        ## We are in a web login.        
        m = interfaces[0].model  ## This line should raise an
exception... thus, this is never executed.
        if id:
            m.orig['username'] = id
            m.orig['username'] = None
        resource = page.Page (m)
        resource.putChild ('', SessionWrapper (UsernamePasswordWrapper
(dem_portal, callback = redirectToIndex)))
        return IResource, resource, None

## Create an instance of the realm.
dem_realm = Realm ()

from twisted.cred.checkers import
InMemoryUsernamePasswordDatabaseDontUse, AllowAnonymousAccess
from twisted.web.woven.guard import UsernamePasswordWrapper,

def redirectToIndex (dummy):
    """I will redirect to the site main page.
    return Redirect ('/index.rpy')

def createResource ():
    ## Create a memory checker.
    checker = InMemoryUsernamePasswordDatabaseDontUse ()
    checker.addUser ('fhc', '12345')
    checker.addUser ('humberto', 'zzzzz')
    checker.addUser ('aes', 'qa')
    ## Allow access from unknowns.
    anon = AllowAnonymousAccess()
    ## Create the portal.
    dem_portal = Portal (Realm)
    dem_portal.registerChecker (checker)
    dem_portal.registerChecker (anon)
    m = model.get_new_model ()
    upass = UsernamePasswordWrapper (dem_portal, callback =
    resource = page.Page (m, templateFile = 'index.template.xhtml')
    resource.putChild ('', SessionWrapper (upass))
    return resource

page.Page is a customized page that works well, lest that the
'init-perspective' link keeps returning 'No such Resource'.  The page
as several wchild_, wvfactory_ and wvupdate_ attached to it.  I also
would like to contribute back the widgets I have done, as soon as I get
them ready and session aware, if someone has the need for them
(calendar, menu tabs, localized date, title boxes, etc.)

  I have sorted the documentation, logged just about any kind of object
dictionary information and even browsed through the sources, but I still
feel completely lost.

  If help can be provided by someone, I would appreciate if these
questions could be answered:

  1. Why doesn't perspective-init work?

  2. How can I put up a customized login page?

  3. How can I access the page (resource) within requestAvatar?

  4. How can I access the session object from a widget?

  5. How can I examine and issue cookies?

  I hope this does not give you a lot of trouble.  Programming twisted
has been both a wonderful and humbling experience, but I do prefer Woven
to anything I have tried before.  I find it an excellent piece of work
and wish to stick with it for my web programming, while I learn the rest
of Twisted for all the other communication needs.

  Thanks in advance.

     Francisco Colaço

More information about the Twisted-Python mailing list