💾 Archived View for gemini.smallweb.space › HOWTO › examples › helpers.py.txt captured on 2024-05-10 at 10:52:41.
⬅️ Previous capture (2023-09-08)
-=-=-=-=-=-=-
# This file adapted from @Tomasino's example project import os import string import random from urllib.parse import parse_qs allowedCerts={ "<user>" : "<user's TLS_CLIENT_HASH>" } def get_client_cert(cert_reqd = False, restricted_auth = False): TLS_CLIENT_HASH = os.getenv('TLS_CLIENT_HASH') if (TLS_CLIENT_HASH is None) and (cert_reqd): # Client hasn't provided client cert, so prompt for one show_header_cert_required() elif (restricted_auth == True and TLS_CLIENT_HASH not in allowedCerts.values()): # Reject login if we are restricting to allowed certificates show_wrong_cert() return TLS_CLIENT_HASH def show_wrong_cert(): print("61 Cert not authorized\r\n") def get_query_string(unparsed = False): qStr = os.getenv('QUERY_STRING') if (qStr == None) or (unparsed) : return qStr return parse_qs(qStr) def show_header_ok(): print("20 text/gemini; charset=utf-8", end = "\r\n") def temp_redirect(url): print(f"30 {url} ", end = "\r\n") 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() def get_path_info(): return os.getenv("PATH_INFO") def read_file(fullPath): #TODO: error handling for file path contents = "" with open(fullPath) as f: contents = f.read() return contents def id_generator(size=12, chars=string.ascii_uppercase + string.digits): return ''.join(random.choice(chars) for _ in range(size))