/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
//! BYOK bearer-token provider callbacks.
//!
//!
//!
//! **Experimental.** These types are part of an experimental wire-protocol
//! surface and may change or be removed in future SDK or CLI releases.
//!
//!
use std::future::Future;
use async_trait::async_trait;
/// Arguments passed to a BYOK bearer-token provider callback.
///
///
///
/// **Experimental.** This type is part of an experimental wire-protocol
/// surface and may change or be removed in future SDK or CLI releases.
///
///
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProviderTokenArgs {
/// Name of the BYOK provider needing a token.
///
/// This is `"default"` for the singular whole-session provider, otherwise
/// the named provider's `name`.
pub provider_name: String,
/// Id of the session that triggered this token request.
///
/// A client-level shared callback registered for many sessions can use this
/// to resolve the owning session and scope token acquisition or caching per
/// session.
pub session_id: String,
}
/// Error returned by a [`BearerTokenProvider`].
///
///
///
/// **Experimental.** This type is part of an experimental wire-protocol
/// surface and may change or be removed in future SDK or CLI releases.
///
///
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BearerTokenError {
message: String,
}
impl BearerTokenError {
/// Construct a bearer-token error with a human-readable message.
pub fn message(message: impl Into) -> Self {
Self {
message: message.into(),
}
}
/// Return the human-readable error message.
pub fn as_str(&self) -> &str {
&self.message
}
}
impl std::fmt::Display for BearerTokenError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.message)
}
}
impl std::error::Error for BearerTokenError {}
impl From for BearerTokenError {
fn from(message: String) -> Self {
Self::message(message)
}
}
impl From<&str> for BearerTokenError {
fn from(message: &str) -> Self {
Self::message(message)
}
}
/// Provider-side callback used to acquire bearer tokens for BYOK providers.
///
///
///
/// **Experimental.** This trait is part of an experimental wire-protocol
/// surface and may change or be removed in future SDK or CLI releases.
///
///
#[async_trait]
pub trait BearerTokenProvider: Send + Sync {
/// Acquire a bearer token without the `Bearer ` prefix.
async fn get_token(&self, args: ProviderTokenArgs) -> Result;
}
#[async_trait]
impl BearerTokenProvider for F
where
F: Fn(ProviderTokenArgs) -> Fut + Send + Sync,
Fut: Future