Skip to content
This repository was archived by the owner on Aug 22, 2023. It is now read-only.

Conversation

@nh916
Copy link
Contributor

@nh916 nh916 commented Nov 29, 2022

Issue

Link to issue card: Raise CRIPT exception for invalid Globus tokens

Changes

A custom Exception was written and added to src/cript/storage_clients/exceptions.py that can be raised whenever a user is trying to connect to a storage client (Globus, S3) and gives an invalid authentication code. The error here is more helpful and understandable

Code

InvalidAuthCode in storage_clients/exceptions.py:

class InvalidAuthCode(CRIPTError):
    """
    Raised when an Authentication code to connect to a storage client is invalid
    """

    def __init__(self):
        pass

    def __str__(self):
        return "Unable to authenticate with storage client. Please use a valid authentication code"

The exception was implemented in src/cript/storage_clients/globus.py within a try except block

from cript.storage_clients.exceptions import InvalidAuthCode, FileUploadError, FileDownloadError

        try:
            token_response = self.auth_client.oauth2_exchange_code_for_tokens(auth_code)
        except globus_sdk.services.auth.errors.AuthAPIError as error:
            raise InvalidAuthCode

Limitations

I have implemented the Exception for Globus storage only.

Before

When a user would give an invalid authentication code here is the error they would get in the terminal

....
File "C:\Users\navid\OneDrive\Desktop\MIT\cript-SDK\src\cript\storage_clients\globus.py", line 157, in set_tokens
    token_response = self.auth_client.oauth2_exchange_code_for_tokens(auth_code)
  File "C:\Users\navid\OneDrive\Desktop\MIT\cript-excel-uploader\venv\lib\site-packages\globus_sdk\services\auth\client\base.py", line 245, in oauth2_exchange_code_for_tokens
    return self.current_oauth2_flow_manager.exchange_code_for_tokens(auth_code)
  File "C:\Users\navid\OneDrive\Desktop\MIT\cript-excel-uploader\venv\lib\site-packages\globus_sdk\services\auth\flow_managers\native_app.py", line 213, in exchange_code_for_tokens
    return self.auth_client.oauth2_token(
  File "C:\Users\navid\OneDrive\Desktop\MIT\cript-excel-uploader\venv\lib\site-packages\globus_sdk\services\auth\client\base.py", line 453, in oauth2_token
    self.post(
  File "C:\Users\navid\OneDrive\Desktop\MIT\cript-excel-uploader\venv\lib\site-packages\globus_sdk\client.py", line 155, in post
    return self.request(
  File "C:\Users\navid\OneDrive\Desktop\MIT\cript-excel-uploader\venv\lib\site-packages\globus_sdk\client.py", line 310, in request
    raise self.error_class(r)
globus_sdk.services.auth.errors.AuthAPIError: ('POST', 'https://auth.globus.org/v2/oauth2/token', None, 400, 'Error', '{"error":"invalid_grant"}')

After

With the new Exception here is the error the users would get when they input an invalid authentication code for Globus

....
File "C:\Users\navid\OneDrive\Desktop\MIT\cript-SDK\src\cript\storage_clients\globus.py", line 160, in set_tokens
    raise InvalidAuthCode
cript.storage_clients.exceptions.InvalidAuthCode: Unable to authenticate with storage client. Please use a valid Authentication code

Tests

I manually tested this with the Excel Uploader because that uses the SDK under the hood. I installed the SDK in edit mode pip install -e ..\cript-SDK\ and when I got to the Globus Authentication code screen I would enter an invalid authentication code and get the errors in the terminal

@nh916 nh916 requested review from CVilla17, brili and dylanwal November 29, 2022 00:13
@nh916 nh916 changed the base branch from master to develop November 29, 2022 00:17
@nh916 nh916 requested a review from dylanwal November 30, 2022 00:05
@nh916 nh916 changed the title Develop storage code error storage code error Nov 30, 2022
Copy link
Contributor

@CVilla17 CVilla17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great man! Nice work :)

@nh916 nh916 merged commit a2673dd into develop Nov 30, 2022
@nh916 nh916 deleted the develop_storage_code_error branch November 30, 2022 22:49
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants