~exarkun/divmod.org/remove-nevow

« back to all changes in this revision

Viewing changes to Nevow/examples/http_auth/http_auth.py

  • Committer: Jean-Paul Calderone
  • Date: 2014-06-08 12:14:27 UTC
  • Revision ID: exarkun@twistedmatrix.com-20140608121427-pe4b355va5guyy03
Remove the obvious stuff.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
"""
3
 
Example of using HTTP Auth directly to require authentication before
4
 
rendering a page for a client.
5
 
 
6
 
This is an example of extremely low-level authentication.  Most applications
7
 
are better served by using L{nevow.guard} instead of manually managing HTTP
8
 
Auth.  See the C{guarded/} example for more information.
9
 
"""
10
 
 
11
 
try:
12
 
    from twisted.web import http
13
 
except ImportError:
14
 
    from twisted.protocols import http
15
 
 
16
 
from nevow import rend, loaders, tags, inevow
17
 
 
18
 
class AuthorizationRequired(rend.Page):
19
 
    ## We are a directory-like resource because we are at the root
20
 
    addSlash = True
21
 
    def renderHTTP(self, ctx):
22
 
        request = inevow.IRequest(ctx)
23
 
        username, password = request.getUser(), request.getPassword()
24
 
        if (username, password) == ('', ''):
25
 
            request.setHeader('WWW-Authenticate', 'Basic realm="Whatever"')
26
 
            request.setResponseCode(http.UNAUTHORIZED)
27
 
            return "Authentication required."
28
 
        ## They provided a username and password, so let's let them in! horray
29
 
        self.data_username, self.data_password = username, password
30
 
        return rend.Page.renderHTTP(self, ctx)
31
 
 
32
 
    docFactory = loaders.stan(tags.html[
33
 
    tags.body[
34
 
        tags.h1["Welcome user!"],
35
 
        tags.div["You said: ",
36
 
            tags.span(data=tags.directive('username'), render=str),
37
 
            " ",
38
 
            tags.span(data=tags.directive('password'), render=str)]]])
39