basic JWT generation + rename gameid to game_id
This commit is contained in:
		
							parent
							
								
									4da6d75913
								
							
						
					
					
						commit
						361ade9abe
					
				| @ -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") | ||||
| 
 | ||||
|  | ||||
| @ -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 | ||||
| @ -22,11 +24,23 @@ 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: | ||||
|  | ||||
| @ -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 | ||||
|      | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user