3
Example of using HTTP Auth directly to require authentication before
4
rendering a page for a client.
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.
12
from twisted.web import http
14
from twisted.protocols import http
16
from nevow import rend, loaders, tags, inevow
18
class AuthorizationRequired(rend.Page):
19
## We are a directory-like resource because we are at the root
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)
32
docFactory = loaders.stan(tags.html[
34
tags.h1["Welcome user!"],
35
tags.div["You said: ",
36
tags.span(data=tags.directive('username'), render=str),
38
tags.span(data=tags.directive('password'), render=str)]]])