generated from Game-as-a-Service/Gaas-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add lobby start game(/games) api endpoint * Add fastapi depends to verify jwt token * Create heath api * Add async * 1. Update JWT package 2. Add create jwt function to fix jwt_token expire problem * Update config.py code style
- Loading branch information
Showing
16 changed files
with
809 additions
and
599 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,19 @@ | ||
import os | ||
|
||
REPOSITORY_IMPL = os.environ.get("repository_impl") | ||
DB_HOST = os.environ.get("DB_HOST", "127.0.0.1") | ||
DB_PORT = os.environ.get("DB_PORT", 27017) | ||
DB_USER = os.environ.get("DB_USER") | ||
DB_PASSWORD = os.environ.get("DB_PASSWORD") | ||
DB_NAME = os.environ.get("DB_NAME", "love_letter") | ||
DB_COLLECTION = os.environ.get("DB_COLLECTION", "love_letter") | ||
|
||
class Configuration: | ||
REPOSITORY_IMPL = os.environ.get("repository_impl") | ||
DB_HOST = os.environ.get("DB_HOST", "127.0.0.1") | ||
DB_PORT = os.environ.get("DB_PORT", 27017) | ||
DB_USER = os.environ.get("DB_USER") | ||
DB_PASSWORD = os.environ.get("DB_PASSWORD") | ||
DB_NAME = os.environ.get("DB_NAME", "love_letter") | ||
DB_COLLECTION = os.environ.get("DB_COLLECTION", "love_letter") | ||
FRONTEND_HOST = os.environ.get("FRONTEND_HOST", "http://127.0.0.1") | ||
LOBBY_ISSUER = os.environ.get( | ||
"LOBBY_ISSUER", "https://dev-1l0ixjw8yohsluoi.us.auth0.com/" | ||
) | ||
LOBBY_AUDIENCE = os.environ.get("LOBBY_ISSUER", "https://api.gaas.waterballsa.tw") | ||
|
||
|
||
config = Configuration() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from love_letter.config import config | ||
from love_letter.models import Game, Player | ||
from love_letter.models.event import GameEvent | ||
from love_letter.usecase.common import Presenter, game_repository | ||
|
||
|
||
class LobbyStartGame: | ||
def execute(self, input: "LobbyPlayers", presenter: Presenter): | ||
game = Game() | ||
for player in input.players: | ||
game.join(Player(player.nickname, player.id)) | ||
game.start() | ||
game_repository.save_or_update(game) | ||
presenter.present([GameEvent(url=f"{config.FRONTEND_HOST}/games/{game.id}")]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import datetime | ||
|
||
import jwt | ||
from fastapi import HTTPException | ||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer | ||
from starlette.requests import Request | ||
|
||
from love_letter.config import config | ||
|
||
|
||
class JWTBearer(HTTPBearer): | ||
def __init__(self, auto_error: bool = True): | ||
super().__init__(auto_error=auto_error) | ||
|
||
async def __call__(self, request: Request): | ||
credentials: HTTPAuthorizationCredentials = await super().__call__(request) | ||
if credentials: | ||
if not credentials.scheme == "Bearer": | ||
raise HTTPException( | ||
status_code=403, detail="Invalid authentication scheme." | ||
) | ||
if not self.verify_jwt(credentials.credentials): | ||
raise HTTPException( | ||
status_code=403, detail="Invalid token or expired token." | ||
) | ||
return credentials.credentials | ||
else: | ||
raise HTTPException(status_code=403, detail="Invalid authorization code.") | ||
|
||
@staticmethod | ||
def verify_jwt(token: str) -> bool: | ||
""" | ||
Verify jwt token iss and aud are as expected, and not expired | ||
:param token: | ||
:return: | ||
""" | ||
try: | ||
jwt.decode( | ||
token, | ||
options={ | ||
"verify_signature": False, | ||
"verify_iss": True, | ||
"verify_aud": True, | ||
"verify_exp": True, | ||
}, | ||
audience=config.LOBBY_AUDIENCE, | ||
issuer=config.LOBBY_ISSUER, | ||
) | ||
return True | ||
except Exception as e: | ||
print(e) | ||
return False | ||
|
||
@staticmethod | ||
def create_jwt(): | ||
now = datetime.datetime.now() | ||
token = jwt.encode( | ||
payload={ | ||
"aud": config.LOBBY_AUDIENCE, | ||
"iss": config.LOBBY_ISSUER, | ||
"exp": now + datetime.timedelta(hours=1), | ||
}, | ||
key="", | ||
) | ||
return f"Bearer {token}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.