Skip to content
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

Missing OAuth configuration error handling #62

Open
bfoz opened this issue Feb 26, 2025 · 4 comments
Open

Missing OAuth configuration error handling #62

bfoz opened this issue Feb 26, 2025 · 4 comments

Comments

@bfoz
Copy link

bfoz commented Feb 26, 2025

I'm using the environment variable method to configure OAuth, and it generally works. However, if the user does something dumb, eg. forgets to start the virtual environment that loads the environment variables, ibind throws an exception that isn't entirely helpful. It might be nice to catch this and re-raise a more descriptive exception.

Obviously this is a just a nice to have, and therefore not a high priority. Thanks for the OAuth support!

INFO:ibind_fh.IbkrClient:#################
INFO:ibind_fh.IbkrClient:New IbkrClient(base_url='https://api.ibkr.com/v1/api/', account_id=None, ssl=True, timeout=10, max_retries=3, use_oauth=True)
INFO:ibind.ibkr_client:IbkrClient: Initialising OAuth 1.0a
Traceback (most recent call last):
  File "fetch_broker.py", line 107, in <module>
    client = IbkrClient(use_oauth=True)
  File "/Users/bfoz/.local/share/virtualenvs/project04-bNStgEKY/lib/python3.13/site-packages/ibind/client/ibkr_client.py", line 95, in __init__
    self.oauth_init(
    ~~~~~~~~~~~~~~~^
        maintain_oauth=self.oauth_config.maintain_oauth,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        shutdown_oauth=self.oauth_config.shutdown_oauth,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        init_brokerage_session=self.oauth_config.init_brokerage_session,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Users/bfoz/.local/share/virtualenvs/project04-bNStgEKY/lib/python3.13/site-packages/ibind/client/ibkr_client.py", line 196, in oauth_init
    self.generate_live_session_token()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/Users/bfoz/.local/share/virtualenvs/project04-bNStgEKY/lib/python3.13/site-packages/ibind/client/ibkr_client.py", line 152, in generate_live_session_token
    = req_live_session_token(self, self.oauth_config)
      ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/bfoz/.local/share/virtualenvs/project04-bNStgEKY/lib/python3.13/site-packages/ibind/oauth/oauth1a.py", line 95, in req_live_session_token
    prepend, extra_headers, dh_random = prepare_oauth(oauth_config)
                                        ~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/Users/bfoz/.local/share/virtualenvs/project04-bNStgEKY/lib/python3.13/site-packages/ibind/oauth/oauth1a.py", line 123, in prepare_oauth
    dh_challenge = generate_dh_challenge(
        dh_prime=oauth_config.dh_prime,
        dh_random=dh_random,
        dh_generator=int(oauth_config.dh_generator),
    )
  File "/Users/bfoz/.local/share/virtualenvs/project04-bNStgEKY/lib/python3.13/site-packages/ibind/oauth/oauth1a.py", line 276, in generate_dh_challenge
    dh_challenge = pow(int(dh_generator), int(dh_random, INT_BASE), int(dh_prime, INT_BASE))
                                                                    ~~~^^^^^^^^^^^^^^^^^^^^
TypeError: int() can't convert non-string with explicit base
@Voyz
Copy link
Owner

Voyz commented Feb 26, 2025

Great observation @bfoz 🙌 I'll add it in!

@Voyz
Copy link
Owner

Voyz commented Feb 28, 2025

@bfoz do you think this would be sufficient?

def verify_oauth_config(oauth_config: OAuth1aConfig) -> None:
    """
    Validates the OAuth 1.0a configuration parameters.

    Checks if all required parameters are set and raises an exception if any are missing.

    Parameters:
        oauth_config (OAuth1aConfig): The OAuth 1.0a configuration object to validate.

    Raises:
        ValueError: If any required parameter is missing.
    """

    required_params = [
        'oauth_rest_url',
        'live_session_token_endpoint',
        'access_token',
        'access_token_secret',
        'consumer_key',
        'dh_prime',
        'encryption_key_fp',
        'signature_key_fp',
    ]
    missing_params = [param for param in required_params if getattr(oauth_config, param) is None]
    if missing_params:
        raise ValueError(f"OAuth1aConfig is missing required parameters: {', '.join(missing_params)}")

    required_filepaths = [
        'encryption_key_fp',
        'signature_key_fp',
    ]
    missing_filepaths = [filepath for filepath in required_filepaths if not Path(getattr(oauth_config, filepath)).exists()]
    if missing_filepaths:
        raise ValueError(f"OAuth1aConfig's filepaths don't exist: {', '.join(missing_filepaths)}")

    return

Please try pip install ibind==0.1.12rc2 with that addition

@bfoz
Copy link
Author

bfoz commented Mar 8, 2025

I haven't tested this yet, but it looks like the right idea to me.

Thanks!

@Voyz
Copy link
Owner

Voyz commented Mar 9, 2025

@bfoz already published this with 0.1.12 👍 give it a spin when you find a minute

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants