AuthProvider

Struct AuthProvider 

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

Authentication provider that integrates with UI authentication state.

Provides a bridge between the server-side authentication system and the UI authentication state, enabling seamless authentication flow coordination between the terminal interface and Azure Service Bus operations.

This provider prioritizes UI-managed authentication tokens and provides fallback mechanisms for automated scenarios where UI authentication is not available.

See also: AuthStateManager for state management, AuthProvider for the base trait.

§Architecture

The AuthProvider implements a hierarchical authentication strategy:

  1. UI State Priority - First checks for valid tokens from UI authentication
  2. State-based Authentication - Uses centralized authentication state management
  3. Fallback Provider - Falls back to alternative authentication methods if available
  4. Error Propagation - Provides detailed error feedback for authentication failures

§Authentication Flow

use quetty_server::auth::{AuthProvider, AuthStateManager};
use std::sync::Arc;

// Create with UI authentication state integration
let auth_state = Arc::new(AuthStateManager::new());
let provider = AuthProvider::new(auth_state, None);

// Authenticate using UI state or fallback methods
match provider.authenticate().await {
    Ok(token) => {
        println!("Authentication successful: {}", token.token);
        // Use token for Service Bus operations
    }
    Err(e) => eprintln!("Authentication failed: {}", e),
}

§Integration with UI Authentication

This provider seamlessly integrates with UI authentication flows:

  • Device Code Flow - Coordinates with UI device code authentication
  • Token Management - Uses UI-managed token cache and refresh logic
  • State Synchronization - Maintains consistency between UI and server authentication
  • Error Handling - Provides user-friendly error messages for UI display

§Fallback Authentication

When UI authentication is not available, the provider can use fallback methods:

For more details on fallback providers, see [ConnectionStringProvider].

use quetty_server::auth::{AuthProvider, AuthStateManager, ConnectionStringProvider};
use std::sync::Arc;

// Create fallback provider for automated scenarios
let connection_provider = Arc::new(ConnectionStringProvider::new(config)?);
let auth_state = Arc::new(AuthStateManager::new());

let provider = AuthProvider::new(
    auth_state,
    Some(connection_provider as Arc<dyn AuthProviderTrait>)
);

// Will use UI state if available, otherwise fall back to connection string
let token = provider.authenticate().await?;

§Thread Safety

The provider is designed for concurrent access and can be safely shared across multiple threads and async tasks. All internal state is protected by appropriate synchronization mechanisms.

§Error Handling

Provides comprehensive error handling for various authentication scenarios:

  • Not Authenticated - Clear guidance for users to authenticate through UI
  • Authentication in Progress - Informative messages during device code flow
  • Authentication Failed - Detailed error information from underlying providers

All errors are returned as ServiceBusError variants.

  • Token Refresh Failures - Graceful handling of token expiration scenarios

Implementations§

Source§

impl AuthProvider

Source

pub fn new( auth_state: Arc<AuthStateManager>, fallback_provider: Option<Arc<dyn AuthProviderTrait>>, ) -> Self

Creates a new authentication provider with UI state integration.

§Arguments
  • auth_state - Shared authentication state manager for UI coordination
  • fallback_provider - Optional fallback provider for automated scenarios
§Returns

A new AuthProvider instance ready for authentication operations

§Examples
use quetty_server::auth::{AuthProvider, AuthStateManager};
use std::sync::Arc;

// Basic provider with UI state only
let auth_state = Arc::new(AuthStateManager::new());
let provider = AuthProvider::new(auth_state, None);

// Provider with fallback for automated scenarios
let auth_state = Arc::new(AuthStateManager::new());
let fallback = Arc::new(connection_provider);
let provider = AuthProvider::new(auth_state, Some(fallback));

Trait Implementations§

Source§

impl AuthProvider for AuthProvider

Source§

fn authenticate<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<AuthToken, ServiceBusError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Performs authentication and returns an access token. Read more
Source§

fn refresh<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<AuthToken, ServiceBusError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Refreshes the authentication token. Read more
Source§

fn auth_type(&self) -> AuthType

Returns the authentication type used by this provider. Read more
Source§

fn requires_refresh(&self) -> bool

Indicates whether this provider’s tokens require periodic refresh. Read more

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> 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
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> SendBound for T
where T: Send,