Commit a69da7ba authored by Romain Loth's avatar Romain Loth

add captcha validation (realperson lib)

parent 532ced09
...@@ -12,13 +12,40 @@ __status__ = "Test" ...@@ -12,13 +12,40 @@ __status__ = "Test"
from cgi import FieldStorage from cgi import FieldStorage
from traceback import format_tb from traceback import format_tb
from ctypes import c_int
# debug # debug
import cgitb import cgitb
cgitb.enable() cgitb.enable()
########### SUBS ###########
def re_hash(userinput, salt=""):
"""
Build the captcha's verification hash server side
(my rewrite of keith-wood.name/realPerson.html python's version)
"""
hashk = 5381
value = userinput.upper() + salt
for i, char in enumerate(value):
hashk = c_int( ((hashk << 5) + hashk + ord(char)) & 0xFFFFFFFF ).value
# bitwise masks 0xFFFFFFFF to go back to int32 each time
# c_int( previous ).value to go from unsigned ints to c signed ints each time
# debug iterations
# print(i, hashk, '<br/>')
return hashk
########### MAIN ###########
if __name__ == "__main__": if __name__ == "__main__":
# any response must have this
print("Content-type: text/html")
print() # blank line <=> end of headers
# reception: the cgi library gets vars from html form within received http POST # reception: the cgi library gets vars from html form within received http POST
this_data = FieldStorage() this_data = FieldStorage()
...@@ -39,21 +66,31 @@ if __name__ == "__main__": ...@@ -39,21 +66,31 @@ if __name__ == "__main__":
keywordsss = this_data['keywords'].value # single string but ','-separated keywordsss = this_data['keywords'].value # single string but ','-separated
# keywordzzz = this_data.getlist(keywords) # array # keywordzzz = this_data.getlist(keywords) # array
# todo # --------- todo ------>8--------------
# institution = this_data[].value # institution = this_data[].value
# optional # optional
# picture = form["user_picture"] # picture = form["user_picture"]
# if picture.file & picture.filename: # if picture.file & picture.filename:
# picture_bytes = picture.value # picture_bytes = picture.value
# --------------------->8---------------
# for captcha validation -----------------------------------------------
form_accepted = False
captcha_userinput = this_data['my-captcha'].value
captcha_verifhash = int(this_data['my-captchaHash'].value)
captcha_userhash = re_hash(captcha_userinput)
form_accepted = (captcha_userhash == captcha_verifhash)
# ----------------------------------------------------------------------
# response
print("Content-type: text/html")
print() # blank line <=> end of headers
# debug data keys
# print([k for k in this_data])
# show received values
print("<TITLE>CGI script output</TITLE>") print("<TITLE>CGI script output</TITLE>")
print([k for k in this_data])
# print("<p style='font-family:Calibri, sans-serif; font-size:80%'")
print('<br>first_name:',first_name) print('<br>first_name:',first_name)
print('<br>midle_name:',middle_name) print('<br>midle_name:',middle_name)
print('<br>last_name:',last_name) print('<br>last_name:',last_name)
...@@ -62,19 +99,16 @@ if __name__ == "__main__": ...@@ -62,19 +99,16 @@ if __name__ == "__main__":
print('<br>country:',country) print('<br>country:',country)
print('<br>jobtitle:',jobtitle) print('<br>jobtitle:',jobtitle)
print('<br>keywords:',keywordsss) print('<br>keywords:',keywordsss)
print('<br>captcha is correct ?:',form_accepted)
# print('instituton:',institution) # print('instituton:',institution)
except KeyError as kerrr: except KeyError as kerrr:
print("Content-type: text/html")
print() # blank line <=> end of headers
print("<h3>Your form was empty</h3") print("<h3>Your form was empty</h3")
print("<p style='font-family:monospace; font-size:80%'") print("<p style='font-family:monospace; font-size:80%'")
print("<br/>".join(format_tb(kerrr.__traceback__))) print("<br/>".join(format_tb(kerrr.__traceback__)))
print("</p>") print("</p>")
except Exception as errr: except Exception as errr:
print("Content-type: text/html")
print() # blank line <=> end of headers
print("<h3>There was an error:</h3") print("<h3>There was an error:</h3")
print("<p style='font-family:monospace; font-size:80%'") print("<p style='font-family:monospace; font-size:80%'")
print("<br/>".join(format_tb(errr.__traceback__))) print("<br/>".join(format_tb(errr.__traceback__)))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment