async def register(request: Request): # returns: # * pgp encrypted cyphertext for 'grand solution' # * cyphertext encrypting URL for riddle 1 try: payload = json.loads(await request.body()) username: str = payload['username'] except json.JSONDecodeError as err: return error_response(str(err)) except KeyError: return error_response( 'expected JSON formatted data in the form ' '{"username":"******"}' ) else: username = username.lower() try: _sanitize_mail_addr(username) except ValueError as ex: return error_response(str(ex)) else: user_data = register_user(username) return JSONResponse( dict( status='success', user_data=user_data, next_riddle=config.riddle_url(routes.ROUTE1) ) )
async def query(request: Request): try: username = request.path_params['username'] user_data = get_user_data(username) except KeyError: return error_response(f'username could not be found') except Exception as ex: return error_response(str(ex)) return JSONResponse(user_data)
async def solution(request: Request): """" solution validation for graph riddle """ def _verify_solution(sd: str) -> bool: # TODO validate solution return True try: try: data = await request.json() solution = data['solution'] except json.JSONDecodeError as ex: # TODO improve this? return error_response('received malformed json') except KeyError: return error_response( 'expected format is {"solution":"<solution>"') if _verify_solution(solution): next_riddle = config.riddle_url(routes.ROUTE5) response_data = dict( solution_correct=True, next_riddle=next_riddle, # TODO piece of master key ) else: response_data = dict( solution_correct=False, data_received=data, ) except ValueError as ex: return error_response(str(ex)) return JSONResponse(response_data)
async def info(request: Request): """" graph riddle """ try: username = request.path_params['username'] except KeyError: return error_response(f'username could not be found') graph = _gen_graph(username) graph_data = json.dumps(nx.node_link_data(graph)) next_riddle = config.riddle_url(routes.ROUTE5) data = dict( description=RIDDLE_4_MSG, payload=graph_data, next_riddle=next_riddle, ) return JSONResponse(data)
def update_info(): token = request.get_json()['token'] ret = update_user_info(token) if ret.error is not Error.OK: return error_response(ret) return response(body=ret.body)
def get_user_token(): code = request.get_json()['code'] ret = get_qtb_user_token(code) if ret.error is not Error.OK: return error_response(ret) return response(body=ret.body)