Skip to content

Feature/totp 2fa handler #250

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

jcoffi
Copy link

@jcoffi jcoffi commented Apr 23, 2025

Add TOTP Two-Factor Authentication Handler

Overview

This PR adds a new built-in Two-Factor Authentication handler that automatically generates Time-based One-Time Password (TOTP) codes for authentication with Interactive Brokers. This eliminates the need for manual input during the 2FA process and enables fully automated authentication workflows.

Features

  • Fully automated TOTP code generation: No manual user input required
  • Compatible with standard TOTP implementations: Works with Google Authenticator, Authy, and other TOTP apps
  • Simple configuration: Only requires setting two environment variables
  • Secure logging: Only logs the last two digits of generated codes for security
  • Comprehensive error handling: Clear error messages for common issues
  • Standalone test mode: Easy verification of handler functionality

Implementation Details

  • Added TotpTwoFaHandler class that inherits from TwoFaHandler
  • Integrated with IBeam's existing 2FA handler selection mechanism
  • Uses the pyotp library for standards-compliant TOTP code generation
  • Follows Google Python Style Guide for code quality and consistency
  • Includes comprehensive unit tests with 100% code coverage

Configuration

Users can enable the TOTP handler by setting:

export IBEAM_TWO_FA_HANDLER=TOTP
export IBEAM_TOTP_SECRET=YOUR_TOTP_SECRET

Documentation

  • Added detailed documentation in docs/two_factor_authentication.md
  • Updated main README.md to highlight 2FA support
  • Created standalone documentation in docs/totp_handler.md
  • Added inline code documentation following Google Python Style Guide

Security Considerations

  • The handler reads secrets securely from environment variables
  • No hardcoded secrets in the codebase
  • Logs only the last two digits of generated codes to prevent exposure
  • Raises clear exceptions if secrets are missing or invalid

Testing

  • Added comprehensive unit tests in tests/test_totp_handler.py
  • Tests cover initialization, code generation, error handling, and logging
  • Added standalone test mode for easy verification

Dependencies

  • Added pyotp dependency to requirements.txt

This PR addresses the need for fully automated authentication when 2FA is enabled on IBKR accounts, making IBeam more versatile.

@Voyz
Copy link
Owner

Voyz commented Apr 24, 2025

hey @jcoffi thanks for the contribution. I assume that the code in this PR is at least partially written by AI, in this case the OpenHands it seems. While it's great to see AI being able to write such quality code, I'm want to ask a few things first:

  1. Have you read the code?
  2. Have you run manual tests to confirm that the implementation works? Can you successfully automate 2FA with this code?
  3. IBKR website sometimes displays a 'Failed' error when inputting 2FA code from Google Authenticator despite typing it in correctly, do you experience it here?
  4. Could you please redact the documentation as it seems to be repeating itself in various files
  5. While the documentation gives a good overview, it lacks an actual guide for how to use this method. Could you provide a step-by-step description on how to use this. Where would the user get the secret, where to put it, how to enable, etc.

Additionally, based on my conversations with IBKR support, I think that TOTP is not available to all IBKR users. Not sure what the criteria is, but I'd be careful with claiming that it completely solves it - we may want to rephrase the docs regarding this.

Appreciate your initiative here 🙌

@jcoffi
Copy link
Author

jcoffi commented Apr 26, 2025

The documentation doesn't mention TOTP being limited access. https://www.ibkrguides.com/securelogin/sls/twofactorauth.htm

I'll come back to this. Just very busy for the next few days.

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

Successfully merging this pull request may close these issues.

3 participants