going-flying.com gemini git repository
ce6119df69a2d64ed35e51c0b9db2250568f9107 - Matthew Ernisse - 1614708952
add more to gmicgi
diff --git a/cgi-bin/clientcert.py b/cgi-bin/clientcert.py index 6904868..3654880 100755 --- a/cgi-bin/clientcert.py +++ b/cgi-bin/clientcert.py @@ -2,8 +2,15 @@ import os import sys +from gmicgi import GeminiCGI + +cgi = GeminiCGI() + +@cgi.route('') +@cgi.certificate_required +def default(): + -if __name__ == '__main__': if not os.environ.get('TLS_CLIENT_HASH'): print('60 Client Certificate Requested\r\n') sys.exit() @@ -29,3 +36,7 @@ And below here are any parameters passed to the script itself. print(sys.argv[1:]) print() print('```') + + +if __name__ == '__main__': + cgi.run() diff --git a/cgi-bin/converter b/cgi-bin/converter index 197b2b2..ded086a 100755 --- a/cgi-bin/converter +++ b/cgi-bin/converter @@ -99,6 +99,7 @@ def ender(i): 420: 'Nice', 1701: 'The Final Frontier', 2257: 'The dawn of the third age of mankind', + 3303: 'Remember...', 31337: 'Greetz to Zero Cool and Joey' } return f' ({s[i]})\n\n' if i in s.keys() else '\n\n' @@ -113,7 +114,6 @@ def pretty_binary(i): out.insert(0, j[len(j) - 8:]) j = j[:len(j) - 8] - return '0' * (8 - len(j)) + j + ' ' + ' '.join(out) diff --git a/cgi-bin/gmicgi/__init__.py b/cgi-bin/gmicgi/__init__.py index 93cf6e3..6c2bb12 100755 --- a/cgi-bin/gmicgi/__init__.py +++ b/cgi-bin/gmicgi/__init__.py @@ -41,6 +41,7 @@ class GeminiCGI(object): ''' Convenience class to create a Gemini Response. ''' codes = { 10: ('10', ''), + 11: ('11', ''), 20: ('20', ''), 30: ('30', ''), 42: ('42', 'CGI ERROR'), @@ -48,6 +49,7 @@ class GeminiCGI(object): 50: ('50', 'PERMANENT FAILURE'), 51: ('51', 'NOT FOUND'), 59: ('59', 'BAD REQUEST'), + 61: ('61', 'Client Certificate Requested') } def __init__(self, code=10, meta=''): @@ -67,6 +69,10 @@ class GeminiCGI(object): return cls(59) @classmethod + def CGIFail(cls): + return cls(42) + + @classmethod def Fail(cls): return cls(50) @@ -75,14 +81,18 @@ class GeminiCGI(object): return cls(43) @classmethod - def CGIFail(cls): - return cls(42) - - @classmethod def Input(cls, meta): return cls(10, meta) @classmethod + def SensativeInput(cls, meta): + return cls(11, meta) + + @classmethod + def NeedCert(cls, meta): + return cls(61) + + @classmethod def NotFound(cls): return cls(51) @@ -99,6 +109,7 @@ class GeminiCGI(object): process the PATH_INFO and QUERY_STRING environment variables. ''' + self.client_cert = os.environ.get('TLS_CLIENT_HASH', '') self.path_info = os.environ.get('PATH_INFO', '') self.routes = {} self.script_path = os.environ.get('SCRIPT_PATH', '') @@ -111,6 +122,16 @@ class GeminiCGI(object): self.query_dequoted = unquote(self.query_string) self.query_parsed = parse_qs(self.query_string) + def certificate_required(self): + ''' Return a 61 if a certificate isn't presented. ''' + def decorator(f): + if not self.client_cert: + return self.Response.NeedCert() + + return f + + return decorator + def route(self, path): ''' Decorator to create a route.''' def decorator(f):