Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 35 lines (26 sloc) 1.053 kb
0b0e89e just started the page off, please no body edit, I'm still working on …
bobson authored
1 ---
2 layout: default
3 title: user authentication
4 ---
5
6 # user authentication
7
b729886 edit
bobson authored
8 #I'm still working on this page, please no body else edit
9
3ca11d7 Add link to french translation
nux authored
10 Other languages : [français](/userauth/fr) | ...
11
0b0e89e just started the page off, please no body edit, I'm still working on …
bobson authored
12 ##Problem
13 You want a system to authenticate users.
14
15 ##Solution
16 A user authentication system is made up of a few parts. Adding users, logging users in, logging users out and checking if users are logged in. It also requires a database. For this example we'll be using MD5 and SQLite.
17
18 ##
cd0510e @nirs Add minimal md5 implementation and warning for naive readers
nirs authored
19 import hashlib
20 import web
21
0b0e89e just started the page off, please no body edit, I'm still working on …
bobson authored
22 def POST(self):
23 i = web.input()
24
b729886 edit
bobson authored
25 authdb = sqlite3.connect('users.db')
cd0510e @nirs Add minimal md5 implementation and warning for naive readers
nirs authored
26 pwdhash = hashlib.md5(i.password).hexdigest()
27 check = authdb.execute('select * from users where username=? and password=?', (i.username, pwdhash))
0b0e89e just started the page off, please no body edit, I'm still working on …
bobson authored
28 if check:
29 session.loggedin = True
30 session.username = i.username
31 raise web.seeother('/results')
32 else: return render.base("Those login details don't work.")
33
cd0510e @nirs Add minimal md5 implementation and warning for naive readers
nirs authored
34 ##Notes
35 Do not use this code on real site - this is only for illustration.
Something went wrong with that request. Please try again.