generated from Game-as-a-Service/Gaas-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
串接大平台1 #93
Merged
+809
−599
Merged
串接大平台1 #93
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f0d3f9a
Add lobby start game(/games) api endpoint
s9891326 f6c4fb9
Add fastapi depends to verify jwt token
s9891326 3bcb4b9
Create heath api
s9891326 a8bbd8f
Add async
s9891326 6b6654c
1. Update JWT package
s9891326 a198011
Update config.py code style
s9891326 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
接近要上線的時刻,也許值得在追另新的 task,接上 sentry
https://sentry.io/welcome/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
酷~ 好喔,沒玩過之後來試試看