pub struct AuthTracker { /* private fields */ }Expand description
Generic authentication state tracker for WebSocket connections.
Coordinates authentication attempts by providing a channel-based signaling mechanism. Each authentication attempt receives a dedicated oneshot channel that will be resolved when the server responds.
§State Management
The tracker maintains a three-state machine:
Unauthenticated: afterbegin(),invalidate(), or initial construction.Authenticated: aftersucceed(). Queryable viais_authenticated().Failed: afterfail(). Causeswait_for_authenticated()to return early.
§Superseding Behavior
If a new authentication attempt begins while a previous one is pending, the old attempt is automatically cancelled with an error. This prevents auth response race conditions during rapid reconnections.
§Thread Safety
All operations are thread-safe and can be called concurrently from multiple tasks.
Implementations§
Source§impl AuthTracker
impl AuthTracker
Sourcepub fn auth_state(&self) -> AuthState
pub fn auth_state(&self) -> AuthState
Returns the current authentication state.
Sourcepub fn is_authenticated(&self) -> bool
pub fn is_authenticated(&self) -> bool
Returns whether the client is currently authenticated.
Sourcepub fn invalidate(&self)
pub fn invalidate(&self)
Clears the authentication state without affecting pending auth attempts.
Call this on disconnect or when the connection is closed to ensure operations requiring authentication are properly guarded.
Sourcepub fn begin(&self) -> AuthResultReceiver
pub fn begin(&self) -> AuthResultReceiver
Begins a new authentication attempt.
Returns a receiver that will be notified when authentication completes. If a previous authentication attempt is still pending, it will be cancelled with an error message indicating it was superseded.
Transitions to Unauthenticated since a new attempt invalidates any
previous status.
Sourcepub fn succeed(&self)
pub fn succeed(&self)
Marks the current authentication attempt as successful.
Transitions to Authenticated and notifies any waiting receiver
with Ok(()). This should be called when the server sends a successful
authentication response.
The state is always updated even if no receiver is waiting (e.g., after a timeout), since the server has confirmed authentication.
Sourcepub fn fail(&self, error: impl Into<String>)
pub fn fail(&self, error: impl Into<String>)
Marks the current authentication attempt as failed.
Transitions to Failed and notifies any waiting receiver
with Err(message). This should be called when the server sends an
authentication error response.
The state is always updated even if no receiver is waiting, since the server has rejected authentication.
Sourcepub async fn wait_for_result<E>(
&self,
timeout: Duration,
receiver: AuthResultReceiver,
) -> Result<(), E>
pub async fn wait_for_result<E>( &self, timeout: Duration, receiver: AuthResultReceiver, ) -> Result<(), E>
Waits for the authentication result with a timeout.
Returns Ok(()) if authentication succeeds, or an error if it fails,
times out, or the channel is closed.
§Type Parameters
E: Error type that implementsFrom<String>for error message conversion
§Errors
Returns an error in the following cases:
- Authentication fails (server rejects credentials)
- Authentication times out (no response within timeout duration)
- Authentication channel closes unexpectedly
- Authentication attempt is superseded by a new attempt
Sourcepub async fn wait_for_authenticated(&self, timeout: Duration) -> bool
pub async fn wait_for_authenticated(&self, timeout: Duration) -> bool
Waits for the tracker to enter the authenticated state.
Returns true if authenticated within the timeout, false if the timeout
expires or authentication explicitly fails. Uses event-driven notification
from succeed() / fail() / invalidate() to avoid polling.
Returns early with false when fail() is called (e.g., the exchange
rejects credentials), so callers are not blocked for the full timeout
on a definitive auth rejection.
This is intended for callers on a separate task who need to gate operations on authentication state (e.g., order sends that must wait for re-authentication after a WebSocket reconnection).
Trait Implementations§
Source§impl Clone for AuthTracker
impl Clone for AuthTracker
Source§fn clone(&self) -> AuthTracker
fn clone(&self) -> AuthTracker
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for AuthTracker
impl Debug for AuthTracker
Auto Trait Implementations§
impl Freeze for AuthTracker
impl RefUnwindSafe for AuthTracker
impl Send for AuthTracker
impl Sync for AuthTracker
impl Unpin for AuthTracker
impl UnsafeUnpin for AuthTracker
impl UnwindSafe for AuthTracker
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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