- 
                Notifications
    
You must be signed in to change notification settings  - Fork 32
 
🐛 Fixes invalid invitation link #7017
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
          
     Merged
      
      
            pcrespov
  merged 9 commits into
  ITISFoundation:master
from
pcrespov:is1792/fix-invitation-links
  
      
      
   
  Jan 9, 2025 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            9 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      99ac744
              
                ruffed
              
              
                pcrespov 71e3532
              
                rename
              
              
                pcrespov e75c4fd
              
                rename
              
              
                pcrespov 639d9cf
              
                updates error handling on server side
              
              
                pcrespov e8b68e0
              
                improves exception handling
              
              
                pcrespov e28b4f9
              
                error handling
              
              
                pcrespov 3cd1b6a
              
                adds tests
              
              
                pcrespov 41f17d5
              
                mypy
              
              
                pcrespov 3ca73bf
              
                mypy
              
              
                pcrespov 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
        
    
  
      
      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.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.