##// END OF EJS Templates
fixed sqlalchemy session bug,
fixed sqlalchemy session bug,

File last commit:

r48:8e250e86 default
r51:a699c008 default
Show More
auth.py
129 lines | 4.3 KiB | text/x-python | PythonLexer
Marcin Kuzminski
Added app basic auth....
r41 import sqlite3
import os
import logging
from os.path import dirname as dn
from datetime import datetime
import crypt
log = logging.getLogger(__name__)
ROOT = dn(dn(dn(os.path.realpath(__file__))))
Added managment pages....
r44 def get_sqlite_conn_cur():
Marcin Kuzminski
Added app basic auth....
r41 conn = sqlite3.connect(os.path.join(ROOT, 'auth.sqlite'))
cur = conn.cursor()
return conn, cur
Marcin Kuzminski
Static files for production fixed...
r46
def admin_auth(username, password):
conn, cur = get_sqlite_conn_cur()
password_crypt = crypt.crypt(password, '6a')
try:
cur.execute("SELECT * FROM users WHERE username=?", (username,))
data = cur.fetchone()
except sqlite3.OperationalError as e:
data = None
log.error(e)
if data:
if data[3]:
if data[1] == username and data[2] == password_crypt and data[4]:
log.info('user %s authenticated correctly', username)
return True
else:
log.error('user %s is disabled', username)
return False
Marcin Kuzminski
Added app basic auth....
r41 def authfunc(environ, username, password):
Added managment pages....
r44 conn, cur = get_sqlite_conn_cur()
Marcin Kuzminski
Added app basic auth....
r41 password_crypt = crypt.crypt(password, '6a')
try except error on non existing user table
r42 try:
cur.execute("SELECT * FROM users WHERE username=?", (username,))
data = cur.fetchone()
except sqlite3.OperationalError as e:
data = None
log.error(e)
Marcin Kuzminski
Added app basic auth....
r41 if data:
if data[3]:
if data[1] == username and data[2] == password_crypt:
log.info('user %s authenticated correctly', username)
implemented admin page login
r45 if environ:
http_accept = environ.get('HTTP_ACCEPT')
if http_accept.startswith('application/mercurial') or \
environ['PATH_INFO'].find('raw-file') != -1:
cmd = environ['PATH_INFO']
for qry in environ['QUERY_STRING'].split('&'):
if qry.startswith('cmd'):
cmd += "|" + qry
Marcin Kuzminski
Added app basic auth....
r41
implemented admin page login
r45 try:
cur.execute('''INSERT INTO
user_logs
VALUES(?,?,?,?)''',
(None, data[0], cmd, datetime.now()))
conn.commit()
except Exception as e:
conn.rollback()
log.error(e)
Marcin Kuzminski
Added app basic auth....
r41 return True
else:
log.error('user %s is disabled', username)
return False
def create_user_table():
'''
Create a auth database
'''
Added managment pages....
r44 conn, cur = get_sqlite_conn_cur()
Marcin Kuzminski
Added app basic auth....
r41 try:
log.info('creating table %s', 'users')
cur.execute('''DROP TABLE IF EXISTS users ''')
cur.execute('''CREATE TABLE users
Marcin Kuzminski
Css fixes, implemented removal of users, and display draft
r48 (user_id INTEGER PRIMARY KEY AUTOINCREMENT,
Marcin Kuzminski
Added app basic auth....
r41 username TEXT,
password TEXT,
Marcin Kuzminski
Static files for production fixed...
r46 active INTEGER,
admin INTEGER)''')
Marcin Kuzminski
Added app basic auth....
r41 log.info('creating table %s', 'user_logs')
cur.execute('''DROP TABLE IF EXISTS user_logs ''')
cur.execute('''CREATE TABLE user_logs
(id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
last_action TEXT,
last_action_date DATETIME)''')
conn.commit()
except:
conn.rollback()
raise
cur.close()
Marcin Kuzminski
Static files for production fixed...
r46 def create_user(username, password, admin=False):
Added managment pages....
r44 conn, cur = get_sqlite_conn_cur()
Marcin Kuzminski
Added app basic auth....
r41 password_crypt = crypt.crypt(password, '6a')
log.info('creating user %s', username)
try:
Marcin Kuzminski
Static files for production fixed...
r46 cur.execute('''INSERT INTO users values (?,?,?,?,?) ''',
(None, username, password_crypt, 1, admin))
Marcin Kuzminski
Added app basic auth....
r41 conn.commit()
except:
conn.rollback()
raise
if __name__ == "__main__":
create_user_table()
Marcin Kuzminski
Static files for production fixed...
r46 create_user('marcink', 'qweqwe', True)
Marcin Kuzminski
Added app basic auth....
r41 create_user('lukaszd', 'qweqwe')
create_user('adriand', 'qweqwe')
create_user('radek', 'qweqwe')
create_user('skrzeka', 'qweqwe')
create_user('bart', 'qweqwe')
create_user('maho', 'qweqwe')
create_user('michalg', 'qweqwe')
#authfunc('', 'marcink', 'qweqwe')