swift-api-client/Sources/MessagingAPIClient/Auth/AuthProvider.swift

30 lines
1.1 KiB
Swift

import Foundation
// MARK: - Auth Provider Protocol
/// Protocol for providing authentication tokens to the API and WebSocket clients.
///
/// Implementations handle token storage, retrieval, and refresh logic.
/// The REST client calls `getAccessToken()` before each request and
/// `refreshToken()` on 401 responses.
public protocol AuthProvider: Sendable {
/// The current access token, if available.
/// Returns `nil` when not authenticated.
var accessToken: String? { get }
/// Whether the provider currently holds valid credentials.
var isAuthenticated: Bool { get }
/// Retrieve a valid access token, refreshing if necessary.
/// - Returns: A valid access token string.
/// - Throws: `AuthError` if no valid token can be obtained.
func getAccessToken() async throws -> String
/// Force a token refresh using the stored refresh token.
/// - Throws: `AuthError` if the refresh fails or no refresh token is available.
func refreshToken() async throws
/// Clear all stored credentials and reset authentication state.
func clearCredentials()
}