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 persistent authentication state that is:
- Set to
truewhensucceed()is called. - Set to
falsewhenfail(),begin(), orinvalidate()is called. - Queryable via
is_authenticated()for guard checks.
§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 is_authenticated(&self) -> bool
pub fn is_authenticated(&self) -> bool
Returns whether the client is currently authenticated.
This state is set to true after succeed() is called and
cleared to false after fail(), invalidate(), or begin().
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.
This clears the authentication state since a new attempt invalidates any previous authenticated status.
Sourcepub fn succeed(&self)
pub fn succeed(&self)
Marks the current authentication attempt as successful.
Sets the authentication state to true 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.
Sets the authentication state to false 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
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