[Twisted-Python] How to do basic authentication on twisted web

Colin Alston colin.alston at thusa.co.za
Mon Aug 17 06:02:25 EDT 2009


On Mon, Aug 17, 2009 at 11:45 AM, Reza Lotun <rlotun at gmail.com> wrote:

> The short answer is that you need to use twisted.cred.


You don't need to do that at all, it just happens to be the best way.


>   wrapper = guard.HTTPAuthSessionWrapper(
>                    Portal(SimpleRealm(), checkers),
>                    [guard.BasicCredentialFactory('yoursite.com')])
>


Where is Portal and SimpleRealm in your example? Those are actually quite
large constructs which are crucial to guard doing anything useful at all.

Since there are numerous circumstances that people need Htauth, it is
implemented as follows

from twisted.web import http
# and other stuff...

class HTTPAuthPage(rend.Page):
    def renderHTTP(self, ctx):
        request = inevow.IRequest(ctx)
        username, password = request.getUser(), request.getPassword()

        if [My auth details check out]:
            return rend.Page.renderHTTP(self, ctx)

        else:
            request.setHeader('WWW-Authenticate', 'Basic realm="My realm
name"')
            request.setResponseCode(http.UNAUTHORIZED)
            return "Authentication required."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20090817/6c8a77e7/attachment.htm 


More information about the Twisted-Python mailing list