server/auth/
provider.rs

1use super::types::AuthType;
2use crate::service_bus_manager::ServiceBusError;
3use async_trait::async_trait;
4
5/// Authentication token containing access credentials for Azure Service Bus.
6///
7/// This struct represents an authentication token obtained from Azure AD
8/// that can be used to authenticate with Azure Service Bus resources.
9#[derive(Clone, Debug)]
10pub struct AuthToken {
11    /// The actual authentication token string
12    pub token: String,
13    /// The type of token (e.g., "Bearer")
14    pub token_type: String,
15    /// Optional expiration time in seconds from when the token was issued
16    pub expires_in_secs: Option<u64>,
17}
18
19/// Trait for authentication providers that can obtain Azure AD tokens.
20///
21/// This trait defines the interface for different authentication methods
22/// (Device Code Flow, Client Credentials, Connection String) to obtain
23/// access tokens for Azure Service Bus operations.
24///
25/// # Examples
26///
27/// ```no_run
28/// use quetty_server::auth::provider::{AuthProvider, AuthToken};
29/// use quetty_server::auth::types::AuthType;
30/// use quetty_server::service_bus_manager::ServiceBusError;
31/// use async_trait::async_trait;
32///
33/// struct MyAuthProvider;
34///
35/// #[async_trait]
36/// impl AuthProvider for MyAuthProvider {
37///     async fn authenticate(&self) -> Result<AuthToken, ServiceBusError> {
38///         // Implementation specific authentication logic
39///         Ok(AuthToken {
40///             token: "example_token".to_string(),
41///             token_type: "Bearer".to_string(),
42///             expires_in_secs: Some(3600),
43///         })
44///     }
45///
46///     fn auth_type(&self) -> AuthType {
47///         AuthType::AzureAd
48///     }
49/// }
50/// ```
51#[async_trait]
52pub trait AuthProvider: Send + Sync {
53    /// Performs authentication and returns an access token.
54    ///
55    /// This method should implement the specific authentication flow
56    /// for the provider (e.g., device code flow, client credentials).
57    ///
58    /// # Errors
59    ///
60    /// Returns [`ServiceBusError`] if authentication fails for any reason,
61    /// including network issues, invalid credentials, or service unavailability.
62    async fn authenticate(&self) -> Result<AuthToken, ServiceBusError>;
63
64    /// Refreshes the authentication token.
65    ///
66    /// Default implementation calls [`authenticate`] again. Providers that
67    /// support refresh tokens can override this method for more efficient
68    /// token renewal.
69    ///
70    /// # Errors
71    ///
72    /// Returns [`ServiceBusError`] if token refresh fails.
73    async fn refresh(&self) -> Result<AuthToken, ServiceBusError> {
74        self.authenticate().await
75    }
76
77    /// Returns the authentication type used by this provider.
78    ///
79    /// This is used for identifying the authentication method
80    /// and may affect how the token is used.
81    fn auth_type(&self) -> AuthType;
82
83    /// Indicates whether this provider's tokens require periodic refresh.
84    ///
85    /// Returns `true` by default. Providers with long-lived tokens
86    /// (like connection strings) can override this to return `false`.
87    fn requires_refresh(&self) -> bool {
88        true
89    }
90}