[Twisted-Python] authentification from a database

vicky lupien vlupien at drummonddesigns.com
Mon Oct 27 07:17:08 MST 2003


I have a program that needs authentification from a database.  For now,
it's working fine but I'm using
checkers.InMemoryUsernamePasswordDatabaseDontUse(test="test") (I want
many users not only "test").   
 
Here's my code:
 
class RootPage(pages.BasePage):
    templateFile = "main.html"
    
    def initialize(self, dbpool=None):
        self.dbpool = dbpool
 
    def wchild_login(self, request):
        a=request.getComponent(simpleguard.Authenticated)
        if not request.getComponent(simpleguard.Authenticated):
            return InfiniChild(login.LoginPage())
        return Authenticated()
 
class Authenticated(page.Page):
    isLeaf = 1
    template='''<html><h1>Hello <span model="name"/>!</h1></html>'''
 
    def wmfactory_name(self, request):
        return request.getComponent(simpleguard.Authenticated).name
    
class IRedirectAfterLogin(components.Interface):
    """The URLPath to which we will redirect after successful login.
    """
 
class Here(resource.Resource):
    def render(self, request):
        existing =
request.getSession().getComponent(IRedirectAfterLogin, None)
        if existing is not None:
            request.redirect(str(existing))
            request.getSession().setComponent(IRedirectAfterLogin, None)
            return "<html></html>"
        else:
            return util.redirectTo('.', request)
        
def callback(model):
    return Here()
 
class FullURLRequest:
    def __init__(self, request):
        self.request = request
 
    def prePathURL(self):
        r = self.request
        prepath = r.prepath
        r.prepath = r.prepath + r.postpath
        rv = r.prePathURL()
        r.prepath = prepath
        return rv
    
class InfiniChild(resource.Resource):
    def __init__(self, r):
        resource.Resource.__init__(self)
        self.r = r
 
    def getChild(self, name, request):
        return self
 
    def render(self, request):
        request.getSession().setComponent(
            IRedirectAfterLogin,
            urlpath.URLPath.fromRequest(FullURLRequest(request))
            )
        return self.r.render(request)
    
def createResource():
    dbpool = database.DataBase().getConnection()
    return simpleguard.guardResource(
        RootPage(dbpool=dbpool),
 
[checkers.InMemoryUsernamePasswordDatabaseDontUse(vicky="lupien")],
        callback=callback)    
 
 
 
#In an other module:
 
class LoginPage(pages.BasePage):
    templateFile = "login.html"
    
    def initialize(self):
        self.title = "Login"
   
    def wmfactory_title(self, request):
        return self.title
            
    def wchild_index(self, request):
        return self
    
    def wvupdate_loginform(self, request, widget, model):
        root = request.getRootURL()
        if root is None:
            root=request.prePathURL()
        url = urlpath.URLPath.fromString(root)
        lmx(widget.node).form(
            action=str(url.sibling(guard.INIT_PERSPECTIVE)),
            model="form")
 
    def wmfactory_form(self, request):
        return guard.newLoginSignature.method(None)
 
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20031027/dbc4bd92/attachment.html>


More information about the Twisted-Python mailing list