Skip to content

Authentication

ibc.utils.auth.InteractiveBrokersAuthentication

Client for managing authentication with the Interactive Brokers Client Portal Gateway.

__init__(ib_client, ib_session)

Initializes the InteractiveBrokersAuthentication client.

Parameters

ib_client : InteractiveBrokersClient The InteractiveBrokersClient Python Client.

InteractiveBrokersSession

The IB session handler.

login()

Logs the user in to the Client Portal Gateway.

Overview

Checks if the user already has an authenticated session before starting the gateway. If already authenticated, returns the current auth status. If the gateway is running but the session is not authenticated, attempts to reauthenticate. Only starts the gateway process and opens the browser login page when the gateway is not running at all.

Returns

AuthStatus: The authentication status response from the gateway.

wait_for_login(timeout=300, poll_interval=3)

Polls the gateway until the user has authenticated or the timeout expires.

Overview

Calls login() and then polls check_auth() at regular intervals until the session is authenticated. Designed to replace the manual while not authenticated polling loops in calling code.

Parameters

timeout : int (optional, Default=300) Maximum number of seconds to wait for authentication before raising an error. Defaults to 5 minutes.

int (optional, Default=3)

Number of seconds between authentication status checks.

Returns

bool: True when the user has successfully authenticated.

Raises

IBCAuthenticationError: If the timeout expires before the user authenticates.

close_gateway(pid=None)

Closes down the Client Portal Gateway.

Parameters

pid : int (optional, Default=None) The process ID to terminate. If not provided, uses the stored server_process_id from the last gateway start.

Returns

str: A message indicating whether the termination was successful.

Raises

IBCAuthenticationError: If no process ID is available to terminate.

is_authenticated()

Checks if session is authenticated.

Overview

Current Authentication status to the Brokerage system. Market Data and Trading is not possible if not authenticated, e.g. authenticated shows False.

Returns

AuthStatus: An AuthStatus model instance with authentication details.

update_server_account(account_id)

Sets the account for the session.

Overview

If a user has multiple accounts and wants to get orders, trades, etc. of an account other than the currently selected account, then the user can update the currently selected account using this API and then fetch required information for the newly updated account.

Parameters

account_id : str The account ID you wish to set for the API Session. This will be used to grab historical data and make orders.

Returns

dict: A ServerAccount resource.

Raises

IBCValidationError: If account_id is empty or not a string.

sso_validate()

Validates the current session for the SSO user.

Returns

dict : A Validation resource.

reauthenticate()

Reauthenticates to the Brokerage system.

Overview

When using the CP Gateway, this endpoint provides a way to reauthenticate to the Brokerage system as long as there is a valid SSO session, see sso_validate().

Returns

dict : An Authentication resource.

tickle()

Pings the server to keep the session alive.

Overview

If the gateway has not received any requests for several minutes an open session will automatically timeout. The tickle endpoint pings the server to prevent the session from ending.

Returns

AuthStatus: An AuthStatus model instance with session status details.

logout()

Logs the user out of the gateway session.

Overview

Logs the user out of the gateway session. Any further activity requires re-authentication.

Returns

dict: A logout confirmation resource.

check_auth()

Checks the authentication status and updates the authenticated flag.

Silently returns if the gateway is not reachable.