| 1 | # -*- coding: iso8859-1 -*- |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2003, 2004 Edgewall Software |
|---|
| 4 | # Copyright (C) 2003, 2004 Jonas Borgstr�jonas@edgewall.com> |
|---|
| 5 | # |
|---|
| 6 | # Trac is free software; you can redistribute it and/or |
|---|
| 7 | # modify it under the terms of the GNU General Public License as |
|---|
| 8 | # published by the Free Software Foundation; either version 2 of the |
|---|
| 9 | # License, or (at your option) any later version. |
|---|
| 10 | # |
|---|
| 11 | # Trac is distributed in the hope that it will be useful, |
|---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 14 | # General Public License for more details. |
|---|
| 15 | # |
|---|
| 16 | # You should have received a copy of the GNU General Public License |
|---|
| 17 | # along with this program; if not, write to the Free Software |
|---|
| 18 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 19 | # |
|---|
| 20 | # Author: Jonas Borgstr�jonas@edgewall.com> |
|---|
| 21 | |
|---|
| 22 | import time |
|---|
| 23 | import util |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | class Authenticator: |
|---|
| 27 | def __init__(self, db, req): |
|---|
| 28 | self.db = db |
|---|
| 29 | self.authname = 'anonymous' |
|---|
| 30 | if req.incookie.has_key('trac_auth'): |
|---|
| 31 | cursor = db.cursor () |
|---|
| 32 | cookie = req.incookie['trac_auth'].value |
|---|
| 33 | cursor.execute ("SELECT name FROM auth_cookie " |
|---|
| 34 | "WHERE cookie=%s AND ipnr=%s" |
|---|
| 35 | ,cookie, req.remote_addr) |
|---|
| 36 | if cursor.rowcount >= 1: |
|---|
| 37 | self.authname = cursor.fetchone()[0] |
|---|
| 38 | |
|---|
| 39 | def login(self, req): |
|---|
| 40 | try: |
|---|
| 41 | if req.incookie['trac_auth'].value == 'logout': |
|---|
| 42 | req.outcookie['trac_auth'] = 'ok' |
|---|
| 43 | req.outcookie['trac_auth']['path'] = req.cgi_location |
|---|
| 44 | return True |
|---|
| 45 | except: |
|---|
| 46 | pass |
|---|
| 47 | |
|---|
| 48 | cursor = self.db.cursor () |
|---|
| 49 | cookie = util.hex_entropy() |
|---|
| 50 | cursor.execute ("INSERT INTO auth_cookie (cookie, name, ipnr, time)" + |
|---|
| 51 | "VALUES (%s, %s, %s, %d)", |
|---|
| 52 | cookie, req.remote_user, req.remote_addr, |
|---|
| 53 | int(time.time())); |
|---|
| 54 | self.db.commit () |
|---|
| 55 | self.authname = req.remote_user |
|---|
| 56 | req.outcookie['trac_auth'] = cookie |
|---|
| 57 | req.outcookie['trac_auth']['path'] = req.cgi_location |
|---|
| 58 | |
|---|
| 59 | def logout(self, req): |
|---|
| 60 | cursor = self.db.cursor () |
|---|
| 61 | cursor.execute ("DELETE FROM auth_cookie WHERE name=%s", |
|---|
| 62 | self.authname) |
|---|
| 63 | self.db.commit () |
|---|
| 64 | req.outcookie['trac_auth'] = 'logout' |
|---|
| 65 | req.outcookie['trac_auth']['path'] = req.cgi_location |
|---|
| 66 | |
|---|