From 361ade9abeec4925d8acba516eacb0c7414bcb18 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Tue, 29 Nov 2022 10:06:15 +0100 Subject: [PATCH] basic JWT generation + rename gameid to game_id --- truthseeker/__init__.py | 1 + truthseeker/game_functions.py | 26 ++++++++++++++++++++------ truthseeker/routes_api.py | 22 +++++++++++++++------- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/truthseeker/__init__.py b/truthseeker/__init__.py index dce4719..7a28619 100644 --- a/truthseeker/__init__.py +++ b/truthseeker/__init__.py @@ -3,6 +3,7 @@ import flask from truthseeker import routes_api app = flask.Flask("truthseeker") +app.config["SECRET_KEY"] = "temporary secret" app.register_blueprint(routes_api.api_routes, url_prefix="/api/v1") diff --git a/truthseeker/game_functions.py b/truthseeker/game_functions.py index d685f3c..8429a58 100644 --- a/truthseeker/game_functions.py +++ b/truthseeker/game_functions.py @@ -1,6 +1,8 @@ import string import random - +import jwt +from datetime import datetime, timedelta +import truthseeker # Map of all actively running games # game_lists["game.id"]-> game info linked to that id @@ -21,12 +23,24 @@ def random_string(length: int) ->str: class GameInfo: """ The game info class stores all information linked to a active game - - Game.start_token : str, - Game.id : str, the game identifier of the game + + GameInfo.id : str, the game identifier of the game """ def __init__(self): - self.start_token = None + self.game_id = None + + def gen_jwt(self, username, owner): + return jwt.encode( + payload={ + "game_id": self.game_id, + "username": username, + "owner": owner, + "exp": datetime.utcnow() + timedelta(hours = 1) # handled automatically on jwt.decode + }, + key=truthseeker.app.config["SECRET_KEY"], + algorithm="HS256" + ) + def create_game(): """ @@ -50,7 +64,7 @@ def get_game_info(game_id): : param game_id : the lenght of the random string : type game_id : str - : return : The GameInfo Object linked to the gameId + : return : The GameInfo Object linked to the game_id : return type : GameInfo """ if game_id in game_lists: diff --git a/truthseeker/routes_api.py b/truthseeker/routes_api.py index c67b9e7..fb0d041 100644 --- a/truthseeker/routes_api.py +++ b/truthseeker/routes_api.py @@ -6,25 +6,33 @@ api_routes = flask.Blueprint("api", __name__) @api_routes.route("/createGame") def create_game(): + username = flask.request.args.get("username") + if username==None: + response = {} + return {"status": "error, username not set"} + + response = {} response["status"] = "ok" - response["gameId"] = game_functions.create_game().id + game = game_functions.create_game() + response["game_id"] = game.id + response["jwt"] = game.gen_jwt(username=username, owner=True) return response @api_routes.route("/getGameInfo") def get_game_info(): response = {} - gameid = flask.request.args.get("gameid") - if gameid == None: - response["status"] = "No 'gameid' argument" + game_id = flask.request.args.get("game_id") + if game_id == None: + response["status"] = "No 'game_id' argument" return response - game = game_functions.get_game_info(gameid) + game = game_functions.get_game_info(game_id) if game == None: - response["status"] = "Game {} does not exist".format(gameid) + response["status"] = "Game {} does not exist".format(game_id) return response else: response["status"] = "ok" - response["gameid"] = gameid + response["game_id"] = game_id response["token"] = game.start_token return response