-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Fixes invalid invitation link (#7017)
- Loading branch information
Showing
11 changed files
with
282 additions
and
196 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
40 changes: 40 additions & 0 deletions
40
services/invitations/src/simcore_service_invitations/core/exceptions_handlers.py
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,40 @@ | ||
import logging | ||
|
||
from fastapi import FastAPI, Request, status | ||
from fastapi.responses import JSONResponse | ||
from servicelib.logging_errors import create_troubleshotting_log_kwargs | ||
|
||
from ..services.invitations import InvalidInvitationCodeError | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
INVALID_INVITATION_URL_MSG = "Invalid invitation link" | ||
|
||
|
||
def handle_invalid_invitation_code_error(request: Request, exception: Exception): | ||
assert isinstance(exception, InvalidInvitationCodeError) # nosec | ||
user_msg = INVALID_INVITATION_URL_MSG | ||
|
||
_logger.warning( | ||
**create_troubleshotting_log_kwargs( | ||
user_msg, | ||
error=exception, | ||
error_context={ | ||
"request.method": f"{request.method}", | ||
"request.url": f"{request.url}", | ||
"request.body": getattr(request, "_json", None), | ||
}, | ||
tip="An invitation link could not be extracted", | ||
) | ||
) | ||
|
||
return JSONResponse( | ||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, | ||
content={"detail": user_msg}, | ||
) | ||
|
||
|
||
def setup(app: FastAPI): | ||
app.add_exception_handler( | ||
InvalidInvitationCodeError, handle_invalid_invitation_code_error | ||
) |
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,10 +1,16 @@ | ||
# pylint: disable=redefined-outer-name | ||
# pylint: disable=unused-argument | ||
# pylint: disable=unused-variable | ||
# pylint: disable=too-many-arguments | ||
|
||
import base64 | ||
import json | ||
import os | ||
from urllib.parse import parse_qsl, urlparse | ||
|
||
import pytest | ||
from cryptography.fernet import Fernet, InvalidToken | ||
from faker import Faker | ||
from starlette.datastructures import URL | ||
|
||
|
||
|
@@ -44,18 +50,45 @@ def consume(url): | |
raise | ||
|
||
except InvalidToken as err: | ||
# TODO: cannot decode | ||
print("Invalid Key", err) | ||
raise | ||
|
||
|
||
def test_encrypt_and_decrypt(monkeypatch: pytest.MonkeyPatch): | ||
@pytest.fixture( | ||
params=[ | ||
"en_US", # English (United States) | ||
"fr_FR", # French (France) | ||
"de_DE", # German (Germany) | ||
"ru_RU", # Russian | ||
"ja_JP", # Japanese | ||
"zh_CN", # Chinese (Simplified) | ||
"ko_KR", # Korean | ||
"ar_EG", # Arabic (Egypt) | ||
"he_IL", # Hebrew (Israel) | ||
"hi_IN", # Hindi (India) | ||
"th_TH", # Thai (Thailand) | ||
"vi_VN", # Vietnamese (Vietnam) | ||
"ta_IN", # Tamil (India) | ||
] | ||
) | ||
def fake_email(request): | ||
locale = request.param | ||
faker = Faker(locale) | ||
# Use a localized name for the username part of the email | ||
name = faker.name().replace(" ", "").replace(".", "").lower() | ||
# Construct the email address | ||
return f"{name}@example.{locale.split('_')[-1].lower()}" | ||
|
||
|
||
def test_encrypt_and_decrypt(monkeypatch: pytest.MonkeyPatch, fake_email: str): | ||
secret_key = Fernet.generate_key() | ||
monkeypatch.setenv("SECRET_KEY", secret_key.decode()) | ||
|
||
# invitation generator app | ||
invitation_url = produce(guest_email="[email protected]") | ||
invitation_url = produce(guest_email=fake_email) | ||
assert invitation_url.fragment | ||
|
||
# osparc side | ||
invitation_data = consume(invitation_url) | ||
print(json.dumps(invitation_data, indent=1)) | ||
assert invitation_data["guest"] == fake_email |
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.