Skip to main content

Auth

Struct Auth 

Source
pub struct Auth { /* private fields */ }
Expand description

Authentication manager for IG Markets API

Handles all authentication operations including:

  • Login with API v2 or v3
  • Automatic OAuth token refresh
  • Account switching
  • Session management
  • Rate limiting for API requests

Implementations§

Source§

impl Auth

Source

pub fn try_new(config: Arc<Config>) -> Result<Self, AppError>

Creates a new Auth instance, returning an error if the HTTP client cannot be constructed.

This is the sole constructor for Auth: it surfaces a TLS / client-builder failure as a typed AppError instead of panicking, so callers can handle a broken TLS backend gracefully.

§Arguments
  • config - Configuration containing credentials and API settings
§Errors

Returns AppError::Network if the underlying reqwest client cannot be built (e.g. the system TLS backend fails to initialize).

Source

pub async fn ws_info(&self) -> Result<WebsocketInfo, AppError>

Gets WebSocket connection information for Lightstreamer, reusing the cached session.

This calls Auth::get_session, which returns the cached session when it is still valid and only logs in (storing the new session) when none exists or it has expired. The configured API version is honoured — no per-call v2 re-login is performed.

§Returns
  • Ok(WebsocketInfo) - Endpoint and authentication tokens for the current session.
  • Err(AppError) - If session retrieval (login / refresh) fails.
§Errors

Returns AppError when the session cannot be retrieved (login or token refresh failure).

Source

pub async fn get_ws_info(&self) -> WebsocketInfo

👎Deprecated:

use ws_info() which reuses the cached session and returns a typed error instead of a default-on-error WebsocketInfo

Gets the WebSocket password for Lightstreamer authentication

§Returns
  • WebSocket password in format “CST-{cst}|XST-{token}” or empty string if session is not available
Source

pub async fn get_session(&self) -> Result<Session, AppError>

Gets the current session, ensuring tokens are valid

This method automatically refreshes expired OAuth tokens or re-authenticates if needed.

§Returns
  • Ok(Session) - Valid session with fresh tokens
  • Err(AppError) - If authentication fails
Source

pub async fn login(&self) -> Result<Session, AppError>

Performs initial login to IG Markets API

Automatically detects API version from config and uses appropriate authentication method.

§Returns
  • Ok(Session) - Authenticated session
  • Err(AppError) - If login fails
Source

pub async fn refresh_token(&self) -> Result<Session, AppError>

Proactively refreshes the session when it is within its refresh margin.

This is the proactive path (driven by the local clock). It re-checks the cached session against the same margin get_session used to decide a refresh was due, so the two stay consistent and the refresh actually fires when the session is close to expiry. If the session is still comfortably valid it is returned unchanged; otherwise a full login is performed.

For the reactive 401 / server-side-invalidation path — where the local clock still considers the token valid but IG has already rejected it — use force_refresh, which re-authenticates unconditionally.

§Returns
  • Ok(Session) - A valid session (refreshed if it was within margin).
  • Err(AppError) - If re-authentication fails.
§Errors

Returns AppError when a required login fails (network, credentials, or rate limiting).

Source

pub async fn force_refresh(&self) -> Result<Session, AppError>

Forces a fresh re-authentication regardless of local expiry state.

This is the reactive 401 / server-side-invalidation path. When IG rejects a token that the local clock still considers valid (server-side invalidation, a concurrent login elsewhere, or clock skew), a proactive refresh_token would see a “valid” session and hand back the same stale token, so the replayed request would fail again. force_refresh ignores local expiry and performs a full login, which fetches and stores a brand-new session.

It cannot loop back through the 401 handler: login issues its HTTP requests through make_http_request directly, not through the HttpClient refresh-and-replay path, so a 401 encountered during login surfaces as a typed error rather than recursing into force_refresh.

§Returns
  • Ok(Session) - A freshly authenticated session with new tokens.
  • Err(AppError) - If re-authentication fails.
§Errors

Returns AppError when the login request fails (network, credentials, or rate limiting).

Source

pub async fn switch_account( &self, account_id: &str, default_account: Option<bool>, ) -> Result<Session, AppError>

Switches to a different trading account

§Arguments
  • account_id - The account ID to switch to
  • default_account - Whether to set as default account
§Returns
  • Ok(Session) - New session for the switched account
  • Err(AppError) - If account switch fails
Source

pub async fn logout(&self) -> Result<(), AppError>

Logs out and clears the current session.

Before clearing local state, this issues DELETE /session (IG API Version 1) with the current authentication headers. For a v2 session (CST / X-SECURITY-TOKEN) this invalidates the session server-side. For a v3 / OAuth session there is no IG token-revocation endpoint and the short-lived access token has usually already expired, so DELETE /session is best-effort: logout clears local state and the OAuth tokens lapse on their own expiry rather than being actively revoked. A 401 Unauthorized (or an already-invalid OAuth token) is treated as already-logged-out and reported as success.

The local session is always cleared, even when the server-side call fails, so the client is never wedged in an authenticated-but-unusable state; the underlying failure is still returned as a typed error.

§Errors

Returns AppError when the server-side logout request fails for a reason other than an already-invalid session (401). The local session is cleared regardless.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Auth

§

impl !UnwindSafe for Auth

§

impl Freeze for Auth

§

impl Send for Auth

§

impl Sync for Auth

§

impl Unpin for Auth

§

impl UnsafeUnpin for Auth

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more