~ubuntu-branches/ubuntu/breezy/libapache2-mod-python/breezy-security

« back to all changes in this revision

Viewing changes to doc-html/tut-more-complicated.html

  • Committer: Bazaar Package Importer
  • Author(s): Thom May
  • Date: 2004-09-06 20:27:57 UTC
  • Revision ID: james.westby@ubuntu.com-20040906202757-yzpyu1bcabgpjtiu
Tags: upstream-3.1.3
ImportĀ upstreamĀ versionĀ 3.1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
 
2
<html>
 
3
<head>
 
4
<title>3.4 Now something More Complicated - Authentication</title>
 
5
<META NAME="description" CONTENT="3.4 Now something More Complicated - Authentication">
 
6
<META NAME="keywords" CONTENT="modpython">
 
7
<META NAME="resource-type" CONTENT="document">
 
8
<META NAME="distribution" CONTENT="global">
 
9
<link rel="STYLESHEET" href="modpython.css">
 
10
<link rel="first" href="modpython.html">
 
11
<link rel="contents" href="contents.html" title="Contents">
 
12
<link rel="index" href="genindex.html" title="Index">
 
13
<LINK REL="previous" href="tut-what-it-do.html">
 
14
<LINK REL="up" href="tutorial.html">
 
15
<LINK REL="next" href="pythonapi.html">
 
16
</head>
 
17
<body>
 
18
<DIV CLASS="navigation">
 
19
<table align="center" width="100%" cellpadding="0" cellspacing="2">
 
20
<tr>
 
21
<td><A href="tut-what-it-do.html"><img src="icons/previous.gif"
 
22
  border="0" height="32"
 
23
  alt="Previous Page" width="32"></A></td>
 
24
<td><A href="tutorial.html"><img src="icons/up.gif"
 
25
  border="0" height="32"
 
26
  alt="Up One Level" width="32"></A></td>
 
27
<td><A href="pythonapi.html"><img src="icons/next.gif"
 
28
  border="0" height="32"
 
29
  alt="Next Page" width="32"></A></td>
 
30
<td align="center" width="100%">Mod_python Manual</td>
 
31
<td><A href="contents.html"><img src="icons/contents.gif"
 
32
  border="0" height="32"
 
33
  alt="Contents" width="32"></A></td>
 
34
<td><img src="icons/blank.gif"
 
35
  border="0" height="32"
 
36
  alt="" width="32"></td>
 
37
<td><A href="genindex.html"><img src="icons/index.gif"
 
38
  border="0" height="32"
 
39
  alt="Index" width="32"></A></td>
 
40
</tr></table>
 
41
<b class="navlabel">Previous:</b> <a class="sectref" href="tut-what-it-do.html">3.3 So what Exactly</A>
 
42
<b class="navlabel">Up:</b> <a class="sectref" href="tutorial.html">3. Tutorial</A>
 
43
<b class="navlabel">Next:</b> <a class="sectref" href="pythonapi.html">4. Python API</A>
 
44
<br><hr>
 
45
</DIV>
 
46
<!--End of Navigation Panel-->
 
47
 
 
48
<H1><A NAME="SECTION005400000000000000000">&nbsp;</A>
 
49
<BR>
 
50
3.4 Now something More Complicated - Authentication
 
51
</H1>
 
52
 
 
53
<P>
 
54
Now that you know how to write a primitive handler, let's try
 
55
something more complicated.
 
56
 
 
57
<P>
 
58
Let's say we want to password-protect this directory. We want the
 
59
login to be "<tt class="samp">spam</tt>", and the password to be "<tt class="samp">eggs</tt>".
 
60
 
 
61
<P>
 
62
First, we need to tell Apache to call our <i>authentication</i>
 
63
handler when authentication is needed. We do this by adding the
 
64
<code>PythonAuthenHandler</code>. So now our config looks like this:
 
65
 
 
66
<P>
 
67
<dl><dd><pre class="verbatim">
 
68
  &lt;Directory /mywebdir&gt;
 
69
      AddHandler mod_python .py
 
70
      PythonHandler myscript
 
71
      PythonAuthenHandler myscript
 
72
      PythonDebug On
 
73
  &lt;/Directory&gt;
 
74
</pre></dl>
 
75
 
 
76
<P>
 
77
Notice that the same script is specified for two different
 
78
handlers. This is fine, because if you remember, mod_python will look
 
79
for different functions within that script for the different handlers.
 
80
 
 
81
<P>
 
82
Next, we need to tell Apache that we are using Basic HTTP
 
83
authentication, and only valid users are allowed (this is fairly basic
 
84
Apache stuff, so we're not going to go into details here). Our config
 
85
looks like this now:
 
86
 
 
87
<P>
 
88
<dl><dd><pre class="verbatim">
 
89
  &lt;Directory /mywebdir&gt;
 
90
     AddHandler mod_python .py
 
91
     PythonHandler myscript
 
92
     PythonAuthenHandler myscript
 
93
     PythonDebug On
 
94
     AuthType Basic
 
95
     AuthName "Restricted Area"
 
96
     require valid-user
 
97
  &lt;/Directory&gt;
 
98
</pre></dl>          
 
99
 
 
100
<P>
 
101
Now we need to write an authentication handler function in
 
102
<span class="file">myscript.py</span>. A basic authentication handler would look like
 
103
this:
 
104
 
 
105
<P>
 
106
<dl><dd><pre class="verbatim">
 
107
from mod_python import apache
 
108
 
 
109
def authenhandler(req):
 
110
 
 
111
    pw = req.get_basic_auth_pw()
 
112
    user = req.user
 
113
 
 
114
    if user == "spam" and pw == "eggs":
 
115
       return apache.OK
 
116
    else:
 
117
       return apache.HTTP_UNAUTHORIZED
 
118
</pre></dl>  
 
119
 
 
120
<P>
 
121
Let's look at this line by line: 
 
122
 
 
123
<P>
 
124
 
 
125
<UL>
 
126
<LI><dl><dd><pre class="verbatim">
 
127
def authenhandler(req):
 
128
</pre></dl>
 
129
 
 
130
<P>
 
131
This is the handler function declaration. This one is called
 
132
  <code>authenhandler</code> because, as we already described above,
 
133
  mod_python takes the name of the directive
 
134
  (<code>PythonAuthenHandler</code>), drops the word "<tt class="samp">Python</tt>" and converts
 
135
  it lower case.
 
136
 
 
137
<P>
 
138
</LI>
 
139
<LI><dl><dd><pre class="verbatim">
 
140
    pw = req.get_basic_auth_pw()
 
141
</pre></dl>
 
142
 
 
143
<P>
 
144
This is how we obtain the password. The basic HTTP authentication
 
145
  transmits the password in base64 encoded form to make it a little
 
146
  bit less obvious. This function decodes the password and returns it
 
147
  as a string. Note that we have to call this function before obtaining
 
148
  the user name.
 
149
 
 
150
<P>
 
151
</LI>
 
152
<LI><dl><dd><pre class="verbatim">
 
153
    user = req.user
 
154
</pre></dl>
 
155
 
 
156
<P>
 
157
This is how you obtain the username that the user entered. 
 
158
 
 
159
<P>
 
160
</LI>
 
161
<LI><dl><dd><pre class="verbatim">
 
162
    if user == "spam" and pw == "eggs":
 
163
        return apache.OK
 
164
</pre></dl>
 
165
 
 
166
<P>
 
167
We compare the values provided by the user, and if they are what we
 
168
  were expecting, we tell Apache to go ahead and proceed by returning
 
169
  <tt class="constant">apache.OK</tt>. Apache will then consider this phase of the
 
170
  request complete, and proceed to the next phase. (Which in this case
 
171
  would be <tt class="function">handler()</tt> if it's a <code>.py</code> file).
 
172
 
 
173
<P>
 
174
</LI>
 
175
<LI><dl><dd><pre class="verbatim">
 
176
    else:
 
177
        return apache.HTTP_UNAUTHORIZED
 
178
</pre></dl>
 
179
 
 
180
<P>
 
181
Else, we tell Apache to return <tt class="constant">HTTP_UNAUTHORIZED</tt> to the
 
182
  client, which usually causes the browser to pop a dialog box asking
 
183
  for username and password.
 
184
 
 
185
<P>
 
186
</LI>
 
187
</UL>
 
188
 
 
189
<P>
 
190
 
 
191
<DIV CLASS="navigation">
 
192
<p><hr>
 
193
<table align="center" width="100%" cellpadding="0" cellspacing="2">
 
194
<tr>
 
195
<td><A href="tut-what-it-do.html"><img src="icons/previous.gif"
 
196
  border="0" height="32"
 
197
  alt="Previous Page" width="32"></A></td>
 
198
<td><A href="tutorial.html"><img src="icons/up.gif"
 
199
  border="0" height="32"
 
200
  alt="Up One Level" width="32"></A></td>
 
201
<td><A href="pythonapi.html"><img src="icons/next.gif"
 
202
  border="0" height="32"
 
203
  alt="Next Page" width="32"></A></td>
 
204
<td align="center" width="100%">Mod_python Manual</td>
 
205
<td><A href="contents.html"><img src="icons/contents.gif"
 
206
  border="0" height="32"
 
207
  alt="Contents" width="32"></A></td>
 
208
<td><img src="icons/blank.gif"
 
209
  border="0" height="32"
 
210
  alt="" width="32"></td>
 
211
<td><A href="genindex.html"><img src="icons/index.gif"
 
212
  border="0" height="32"
 
213
  alt="Index" width="32"></A></td>
 
214
</tr></table>
 
215
<b class="navlabel">Previous:</b> <a class="sectref" href="tut-what-it-do.html">3.3 So what Exactly</A>
 
216
<b class="navlabel">Up:</b> <a class="sectref" href="tutorial.html">3. Tutorial</A>
 
217
<b class="navlabel">Next:</b> <a class="sectref" href="pythonapi.html">4. Python API</A>
 
218
<hr>
 
219
<span class="release-info">Release 3.1.3, documentation updated on February 17, 2004.</span>
 
220
</DIV>
 
221
<!--End of Navigation Panel-->
 
222
 
 
223
</BODY>
 
224
</HTML>