[Twisted-Python] Guard

Moshe Zadka m at moshez.org
Sun Jul 20 16:39:27 EDT 2003


As many of you know, using guard (the webby interface to authentication)
has been a thorny issue with Twisted. Christopher Armstrong and I wrote
a module which hides many of the details of using guard, and supplies
a much more usable interface.

So, here is an example of using twisted.web.woven.simpleguard:

from twisted.cred import checkers
from twisted.internet import reactor
from twisted.web import server, resource
from twisted.web.woven import simpleguard

class SimpleResource(resource.Resource):

    def getChild(self, path, request):
        return self

    def render(self, request):
        auth = request.getComponent(simpleguard.Authenticated)
        if auth:
            return "hello my friend "+auth.name
        else:
            return """
            I don't think we've met
            <a href="perspective-init">login</a>
            """

checker = checkers.InMemoryUsernamePasswordDatabaseDontUse()
checker.addUser("bob", "12345")
anon = checkers.AllowAnonymousAccess()

reactor.listenTCP(8889, server.Site(
      resource = simpleguard.guardResource(SimpleResource(), [checker, anon])))
reactor.run()

A simpler example would be using simpleguard to password-protect a
directory:

checker = checkers.OnDiskUsernamePasswordDatabase('/etc/www/htpasswd')
resource = static.File("/var/www/htdocs")
resource.putChild('secret',
    simpleguard.guardResource(static.File('/var/www/htdocs/secret'),
                              [checker])
reactor.listenTCP(8889, server.Site(resource))
reactor.run()

I hope these two examples show that using authentication in a web
application is easier than it used to be!

Thanks,
Moshe
-- 
Moshe Zadka -- http://moshez.org/
Buffy: I don't like you hanging out with someone that... short.
Riley: Yeah, a lot of young people nowadays are experimenting with shortness.
Agile Programming Language -- http://www.python.org/




More information about the Twisted-Python mailing list