Skip to main content

Session

Struct Session 

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

A session with the Comdirect API, managing tokens and automatic background refreshes.

Implementations§

Source§

impl Session

Source

pub async fn new( config: &ComdirectConfig, refresh_token: Option<String>, ) -> Result<Self, AuthError>

Initializes a new authenticated session with Comdirect.

This method orchestrates the complete OAuth2 handshake. Depending on whether a refresh_token is provided, it will either attempt to resume an existing session or perform a full “Interactive” login sequence.

§Authentication Flow
  1. Resumption: If refresh_token is Some, it attempts a refresh_token grant.
    • If successful, it proceeds to obtain a new session ID and sets up the auto-refresh worker.
    • If unsuccessful (e.g., token expired), it returns an error instead of falling back.
  2. Full Authentication: If no token is provided or resumption fails:
    • Performs a password grant using user and password from the config.
    • Triggers a Push-TAN challenge for session validation.
    • Performs a cd_secondary grant to finalize the session.
§Background Worker

Upon successful creation, a background task is spawned that monitors token expiration. It proactively refreshes the access token 60 seconds before it expires (or at 10% of its life if shorter than 120s) to ensure uninterrupted API access.

§Token Persistence

To persist authentication across application restarts, provide an on_refresh_token callback in the ComdirectConfig. This callback is triggered whenever a new refresh token is successfully obtained (both during initial login and background refreshes).

§Example
use comdirect_rest_api::oauth2::{ComdirectConfig, Session};
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = ComdirectConfig {
        user: "1234567".to_string(),
        password: "your_password".to_string(),
        client_id: "your_client_id".to_string(),
        client_secret: "your_client_secret".to_string(),
        on_awaits_user_confirm: Arc::new(|| Box::pin(async {
            println!("Please confirm the TAN on your mobile device.");
        })),
        on_refresh_token: Some(Arc::new(|new_token| {
            println!("New refresh token received: {}", new_token);
            // Save this token to a database or secure file for next time
        })),
    };

    // Try to resume with a previously saved token
    let saved_token = Some("...token from database...".to_string());
    let session = Session::new(&config, saved_token).await?;

    println!("Session established! ID: {}", session.session_id().await);
    Ok(())
}
Source

pub async fn session_id(&self) -> String

Returns the currently active session ID.

Auto Trait Implementations§

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<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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: 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: 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, 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<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