a tiny bit of code cleanup
This commit is contained in:
parent
b25b954181
commit
edaac5ee45
|
@ -8,19 +8,11 @@ The goal of this project is to give a working example of a basic Gemini CGI appl
|
|||
* (DONE) Users can set data on their own account (a name field for example)
|
||||
* (DONE) Users can add additional certificates to the same account by using a special code
|
||||
|
||||
## Files
|
||||
## Setup
|
||||
|
||||
* `index.gmi` - a python script using the `gmi` extension. It is an executable file to enable CGI use. Your server must be configured for CGI for this to work. Depending on your server software you may need to house this project within a special `cgi-bin` directory.
|
||||
Create database with:
|
||||
|
||||
Several sub-directories exist with their own index.gmi files to perform specific app actions.
|
||||
|
||||
* `db.py` - handles database connection and queries
|
||||
* `helpers.py` - provides some Gemini header operations and environment variable fetching
|
||||
* `create_schema.sql` - holds the database schema for sqlite used in this app.
|
||||
|
||||
Create database with:
|
||||
|
||||
```bash
|
||||
``` sqlite command to create database
|
||||
sqlite3 db/cgi-example.sqlite3 < create_schema.sql
|
||||
```
|
||||
|
||||
|
|
|
@ -10,7 +10,6 @@ db_file = os.path.join(dir_name, db_name)
|
|||
def create_connection():
|
||||
""" create a database connection to the SQLite database
|
||||
specified by db_file
|
||||
:param db_file: database file
|
||||
:return: Connection object or None
|
||||
"""
|
||||
conn = None
|
||||
|
@ -23,7 +22,7 @@ def create_connection():
|
|||
def add_user(conn):
|
||||
"""
|
||||
Create a new user record
|
||||
:param conn:
|
||||
:param conn: database connection object
|
||||
:return: user id
|
||||
"""
|
||||
sql = ''' INSERT INTO users DEFAULT VALUES '''
|
||||
|
@ -35,52 +34,49 @@ def add_user(conn):
|
|||
return 'error in insert'
|
||||
return cur.lastrowid
|
||||
|
||||
def add_hash(conn, cert_user):
|
||||
def add_hash(conn, tls_client_hash, user_id):
|
||||
"""
|
||||
Create a new record for cert_user
|
||||
:param conn:
|
||||
:param cert_user:
|
||||
:return: id
|
||||
Create a new record for this cert
|
||||
:param conn: database connection object
|
||||
:param tls_client_hash:
|
||||
:param user_id:
|
||||
:return: certificate id
|
||||
"""
|
||||
sql = ''' INSERT OR REPLACE INTO certs(hash,user_id) VALUES(?,?) '''
|
||||
cur = conn.cursor()
|
||||
cur.execute(sql, cert_user)
|
||||
cur.execute(sql, (tls_client_hash, user_id))
|
||||
conn.commit()
|
||||
return cur.lastrowid
|
||||
|
||||
def get_user(conn, tls_client_hash):
|
||||
"""
|
||||
Get user id of tls_client_hash if it exists
|
||||
:param conn:
|
||||
Get user id matching tls_client_hash if it exists
|
||||
:param conn: database connection object
|
||||
:param tls_client_hash:
|
||||
:return: user id
|
||||
:return: user id or None
|
||||
"""
|
||||
cur = conn.cursor()
|
||||
cur.execute("SELECT user_id FROM certs WHERE hash=?", (tls_client_hash,))
|
||||
rows = cur.fetchall()
|
||||
|
||||
if len(rows) > 0:
|
||||
row = rows[0]
|
||||
return row[0] # user_id
|
||||
else:
|
||||
row = cur.fetchone()
|
||||
if row is None:
|
||||
return None
|
||||
else:
|
||||
return row[0] # user_id
|
||||
|
||||
def get_user_by_keycode(conn, key_code):
|
||||
"""
|
||||
Get user id of tls_client_hash if it exists
|
||||
:param conn:
|
||||
:param key_code:
|
||||
:return: user id
|
||||
:param conn: database connection object
|
||||
:param key_code: authorization key code
|
||||
:return: user id or None
|
||||
"""
|
||||
cur = conn.cursor()
|
||||
cur.execute("SELECT id FROM users WHERE add_key_code=?", (key_code,))
|
||||
rows = cur.fetchall()
|
||||
|
||||
if len(rows) > 0:
|
||||
row = rows[0]
|
||||
return row[0] # user_id
|
||||
else:
|
||||
row = cur.fetchone()
|
||||
if row is None:
|
||||
return None
|
||||
else:
|
||||
return row[0] # user_id
|
||||
|
||||
def check_hash(tls_client_hash):
|
||||
"""
|
||||
|
@ -94,8 +90,7 @@ def check_hash(tls_client_hash):
|
|||
user_id = get_user(conn, tls_client_hash)
|
||||
if (user_id is None):
|
||||
user_id = add_user(conn)
|
||||
cert_user = (tls_client_hash, user_id)
|
||||
add_hash(conn, cert_user)
|
||||
add_hash(conn, tls_client_hash, user_id)
|
||||
return user_id
|
||||
|
||||
def get_name(user_id):
|
||||
|
@ -108,13 +103,11 @@ def get_name(user_id):
|
|||
with conn:
|
||||
cur = conn.cursor()
|
||||
cur.execute("SELECT name FROM users WHERE id=?", (user_id,))
|
||||
rows = cur.fetchall()
|
||||
|
||||
if len(rows) > 0:
|
||||
row = rows[0]
|
||||
return row[0] # user_name
|
||||
else:
|
||||
row = cur.fetchone()
|
||||
if row is None:
|
||||
return None
|
||||
else:
|
||||
return row[0] # user_name
|
||||
|
||||
def set_name(user_id, user_name):
|
||||
"""
|
||||
|
@ -160,8 +153,7 @@ def add_cert_to_user(key_code, tls_client_hash):
|
|||
user_id = get_user_by_keycode(conn, key_code)
|
||||
if (user_id is None):
|
||||
return False
|
||||
cert_user = (tls_client_hash, user_id)
|
||||
add_hash(conn, cert_user)
|
||||
add_hash(conn, tls_client_hash, user_id)
|
||||
return True
|
||||
|
||||
#vim:fenc=utf-8:ts=4:sw=4:sta:noet:sts=4:fdm=marker:ai
|
||||
|
|
|
@ -10,11 +10,11 @@ def get_client_cert(ok_if_found = True):
|
|||
show_header_ok()
|
||||
return TLS_CLIENT_HASH
|
||||
|
||||
def get_query_string(msg):
|
||||
def get_query_string(msg, ok_if_found = True):
|
||||
QUERY_STRING = os.getenv('QUERY_STRING')
|
||||
if(QUERY_STRING is None):
|
||||
show_query_string_required(msg)
|
||||
else:
|
||||
elif ok_if_found:
|
||||
show_header_ok()
|
||||
return QUERY_STRING
|
||||
|
||||
|
|
Loading…
Reference in New Issue