def signin(): import hurricane_hash form = cgi.FieldStorage() email = form["email"].value password = form["password"].value data_store = open("account_store", "r") data = data_store.read() if not data: print '{"status": "error", "cause": "unexpected error occurred"}' return data_json = json.loads(data) if email not in data_json: print '{"status": "error", "cause": "incorrect username/password"}' else: hash_string = hurricane_hash.create_salt(password) if hash_string == data_json.get(email).get("h"): jw_token = encoded = jwt.encode({'e': email}, hurricane_hash.SECRET_CODE, algorithm='HS256') print '{"status": "success", "token": %s}' % jw_token else: print '{"status": "error", "cause": "incorrect username/password"}'
def register(): import hurricane_hash form = cgi.FieldStorage() try: name = form["name"].value except: print '{"status": "error", "cause": "name required"}' return try: email = form["email"].value except: print '{"status": "error", "cause": "email required"}' return try: password = form["password"].value except: print '{"status": "error", "cause": "password required"}' return try: phone_number = form["phone"].value except: phone_number = None try: user_type = form["type"].value except: user_type = "enduser" new_account = {email: {"n": name, "p": phone_number, "t": user_type, "h": hurricane_hash.create_salt(password)}} data_store = open("account_store", "r+") fcntl.flock(data_store, fcntl.LOCK_EX) data = data_store.read() if data: data_json = json.loads(data) if email in data_json: print '{"status": "error", "cause": "user exists"}' return data_json.update(new_account) else: data_json = new_account data_store.seek(0) data_store.write(json.dumps(data_json)) fcntl.flock(data_store, fcntl.LOCK_UN) jw_token = encoded = jwt.encode({'e': email}, hurricane_hash.SECRET_CODE, algorithm='HS256') print '{"status": "success", "token": %s}' % jw_token