name update working
This commit is contained in:
parent
f2ee12fc1e
commit
8cbd2d4176
|
@ -5,7 +5,7 @@ The goal of this project is to give a working example of a basic Gemini CGI appl
|
|||
## Features
|
||||
|
||||
* (DONE) New TLS certificates automatically create a new user account
|
||||
* Users can set data on their own account (a name field for example)
|
||||
* (DONE) Users can set data on their own account (a name field for example)
|
||||
* Users can add additional certificates to the same account by using a special code
|
||||
|
||||
## Files
|
||||
|
|
|
@ -5,16 +5,20 @@ import sys
|
|||
sys.path.append('..')
|
||||
|
||||
# import helpers from modules
|
||||
from helpers import get_client_cert
|
||||
from db import check_hash
|
||||
from helpers import get_query_string, get_client_cert
|
||||
from db import check_hash, set_name
|
||||
|
||||
TLS_CLIENT_HASH = get_client_cert()
|
||||
TLS_CLIENT_HASH = get_client_cert(False) # don't default to response 20
|
||||
user_name = get_query_string("What is your name?")
|
||||
user_id = check_hash(TLS_CLIENT_HASH)
|
||||
user_name = "[Not Set]"
|
||||
set_name(user_id, user_name)
|
||||
|
||||
print("# Name Change Page")
|
||||
print()
|
||||
|
||||
print("This feature coming soon.")
|
||||
print("Your name has been changed.")
|
||||
print()
|
||||
print("=> ../login Back to main page")
|
||||
|
||||
|
||||
#vim:fenc=utf-8:ts=4:sw=4:sta:noet:sts=4:fdm=marker:ai
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import sqlite3
|
||||
from sqlite3 import Error
|
||||
import os
|
||||
import urllib.parse
|
||||
|
||||
""" Create database with:
|
||||
sqlite3 db/cgi-example.sqlite3 < create_schema.sql
|
||||
|
@ -88,4 +89,36 @@ def check_hash(tls_client_hash):
|
|||
add_hash(conn, cert_user)
|
||||
return user_id
|
||||
|
||||
def get_name(user_id):
|
||||
"""
|
||||
Get user name if it exists
|
||||
:param user_id:
|
||||
:return: user name
|
||||
"""
|
||||
conn = create_connection()
|
||||
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:
|
||||
return None
|
||||
|
||||
def set_name(user_id, user_name):
|
||||
"""
|
||||
Update or set name on user
|
||||
:param user_name:
|
||||
"""
|
||||
conn = create_connection()
|
||||
with conn:
|
||||
sql = ''' UPDATE users SET name=(?) WHERE id=? '''
|
||||
cur = conn.cursor()
|
||||
cur.execute(sql, (urllib.parse.unquote(user_name), user_id))
|
||||
conn.commit()
|
||||
return cur.lastrowid
|
||||
|
||||
|
||||
#vim:fenc=utf-8:ts=4:sw=4:sta:noet:sts=4:fdm=marker:ai
|
||||
|
|
|
@ -1,13 +1,21 @@
|
|||
import os
|
||||
|
||||
def get_client_cert():
|
||||
def get_client_cert(ok_if_found = True):
|
||||
TLS_CLIENT_HASH = os.getenv('TLS_CLIENT_HASH')
|
||||
if(TLS_CLIENT_HASH is None):
|
||||
if (TLS_CLIENT_HASH is None):
|
||||
show_header_cert_required()
|
||||
else:
|
||||
elif ok_if_found:
|
||||
show_header_ok()
|
||||
return TLS_CLIENT_HASH
|
||||
|
||||
def get_query_string(msg):
|
||||
QUERY_STRING = os.getenv('QUERY_STRING')
|
||||
if(QUERY_STRING is None):
|
||||
show_query_string_required(msg)
|
||||
else:
|
||||
show_header_ok()
|
||||
return QUERY_STRING
|
||||
|
||||
def show_header_ok():
|
||||
print("20 text/gemini; charset=utf-8", end = "\r\n")
|
||||
|
||||
|
@ -15,4 +23,7 @@ def show_header_cert_required():
|
|||
print("60 text/gemini; charset=utf-8", end = "\r\n")
|
||||
quit()
|
||||
|
||||
def show_query_string_required(msg):
|
||||
print("10 " + msg, end = "\r\n")
|
||||
quit()
|
||||
#vim:fenc=utf-8:ts=4:sw=4:sta:noet:sts=4:fdm=marker:ai
|
||||
|
|
|
@ -6,11 +6,11 @@ sys.path.append('..')
|
|||
|
||||
# import helpers from modules
|
||||
from helpers import get_client_cert
|
||||
from db import check_hash
|
||||
from db import *
|
||||
|
||||
TLS_CLIENT_HASH = get_client_cert()
|
||||
user_id = check_hash(TLS_CLIENT_HASH)
|
||||
user_name = "[Not Set]"
|
||||
user_name = get_name(user_id) or "[Not Set]"
|
||||
user_cert_count = 1
|
||||
|
||||
print("# Welcome: User Logged In")
|
||||
|
|
Loading…
Reference in New Issue