| 1 | # svntrac
|
|---|
| 2 | #
|
|---|
| 3 | # Copyright (C) 2003 Xyche Software
|
|---|
| 4 | #
|
|---|
| 5 | # svntrac is free software; you can redistribute it and/or
|
|---|
| 6 | # modify it under the terms of the GNU General Public License as
|
|---|
| 7 | # published by the Free Software Foundation; either version 2 of the
|
|---|
| 8 | # License, or (at your option) any later version.
|
|---|
| 9 | #
|
|---|
| 10 | # svntrac is distributed in the hope that it will be useful,
|
|---|
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 13 | # General Public License for more details.
|
|---|
| 14 | #
|
|---|
| 15 | # You should have received a copy of the GNU General Public License
|
|---|
| 16 | # along with this program; if not, write to the Free Software
|
|---|
| 17 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|---|
| 18 | #
|
|---|
| 19 | # Author: Jonas Borgström <jonas@xyche.com>
|
|---|
| 20 |
|
|---|
| 21 | import os
|
|---|
| 22 | import Cookie
|
|---|
| 23 | import time
|
|---|
| 24 | import random
|
|---|
| 25 | from db import get_connection
|
|---|
| 26 |
|
|---|
| 27 | AUTH_TIMEOUT = 60*60*24*30 # 30 days
|
|---|
| 28 |
|
|---|
| 29 | authname = 'anonymous'
|
|---|
| 30 |
|
|---|
| 31 | def get_authname ():
|
|---|
| 32 | return authname
|
|---|
| 33 |
|
|---|
| 34 | def logout (auth_cookie):
|
|---|
| 35 | cnx = get_connection ()
|
|---|
| 36 | cursor = cnx.cursor ()
|
|---|
| 37 | cursor.execute ("DELETE FROM auth_cookie WHERE cookie='%s'" % auth_cookie)
|
|---|
| 38 | cnx.commit ()
|
|---|
| 39 |
|
|---|
| 40 | def flush_auth_cookies ():
|
|---|
| 41 | """
|
|---|
| 42 | Delete auth cookies which are older then AUTH_TIMEOUT seconds.
|
|---|
| 43 | """
|
|---|
| 44 | cnx = get_connection ()
|
|---|
| 45 | cursor = cnx.cursor ()
|
|---|
| 46 | cursor.execute ('DELETE FROM auth_cookie WHERE time < %d'
|
|---|
| 47 | % int(time.time() - AUTH_TIMEOUT))
|
|---|
| 48 | cnx.commit ()
|
|---|
| 49 |
|
|---|
| 50 | def validate_auth_cookie (auth_cookie, ipnr):
|
|---|
| 51 | """
|
|---|
| 52 | Makes sure the auth_cookie is valid and that it comes from the correct host.
|
|---|
| 53 | """
|
|---|
| 54 | cnx = get_connection ()
|
|---|
| 55 | cursor = cnx.cursor ()
|
|---|
| 56 | cursor.execute ("SELECT name FROM auth_cookie WHERE cookie='%s' AND ipnr='%s'"
|
|---|
| 57 | % (auth_cookie, ipnr))
|
|---|
| 58 | if cursor.rowcount >= 1:
|
|---|
| 59 | global authname
|
|---|
| 60 | authname = cursor.fetchone()[0]
|
|---|
| 61 | return 1
|
|---|
| 62 | else:
|
|---|
| 63 | return 0
|
|---|
| 64 |
|
|---|
| 65 | def update_auth_cookie (auth_cookie, ipnr):
|
|---|
| 66 | """
|
|---|
| 67 | Update the timeout value for an auth cookie.
|
|---|
| 68 | """
|
|---|
| 69 | cnx = get_connection ()
|
|---|
| 70 | cursor = cnx.cursor ()
|
|---|
| 71 | cursor.execute ("UPDATE auth_cookie SET time=%d WHERE cookie='%s' AND ipnr='%s'" % (int(time.time()), auth_cookie, ipnr))
|
|---|
| 72 | cnx.commit ()
|
|---|
| 73 |
|
|---|
| 74 | def create_auth_cookie (name, ipnr):
|
|---|
| 75 | """
|
|---|
| 76 | Create a new auth_cookie which is stored in the db and sent to the user
|
|---|
| 77 | """
|
|---|
| 78 | global authname
|
|---|
| 79 | cnx = get_connection ()
|
|---|
| 80 | cursor = cnx.cursor ()
|
|---|
| 81 | # TODO: authenticate here
|
|---|
| 82 | cookie = str(random.random())
|
|---|
| 83 | cursor.execute ("INSERT INTO auth_cookie (cookie, name, ipnr, time)" +
|
|---|
| 84 | "VALUES ('%s', '%s', '%s', %d)"
|
|---|
| 85 | % (cookie, name, ipnr, int(time.time())));
|
|---|
| 86 | cnx.commit ()
|
|---|
| 87 | authname = name
|
|---|
| 88 | return cookie
|
|---|
| 89 |
|
|---|
| 90 | def authenticate_user ():
|
|---|
| 91 | flush_auth_cookies ()
|
|---|
| 92 |
|
|---|
| 93 | cookie = Cookie.Cookie(os.getenv('HTTP_COOKIE'))
|
|---|
| 94 | auth_cookie = create_auth_cookie (os.getenv('REMOTE_USER'),
|
|---|
| 95 | os.getenv('REMOTE_ADDR'))
|
|---|
| 96 | cookie['svntrac_auth'] = auth_cookie
|
|---|
| 97 | # send the cookie to the browser as a http header
|
|---|
| 98 | print cookie.output()
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 | def verify_authentication (args):
|
|---|
| 102 | flush_auth_cookies ()
|
|---|
| 103 |
|
|---|
| 104 | cookie = Cookie.Cookie(os.getenv('HTTP_COOKIE'))
|
|---|
| 105 | remote_addr = os.getenv ('REMOTE_ADDR')
|
|---|
| 106 |
|
|---|
| 107 | if cookie.has_key('svntrac_auth'):
|
|---|
| 108 | auth_cookie = cookie['svntrac_auth'].value
|
|---|
| 109 | if args.has_key ('logout'):
|
|---|
| 110 | logout (auth_cookie)
|
|---|
| 111 | elif validate_auth_cookie (auth_cookie, remote_addr):
|
|---|
| 112 | update_auth_cookie (auth_cookie, remote_addr)
|
|---|