-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathprovider_token.rs
More file actions
112 lines (98 loc) · 3.2 KB
/
Copy pathprovider_token.rs
File metadata and controls
112 lines (98 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
//! BYOK bearer-token provider callbacks.
//!
//! <div class="warning">
//!
//! **Experimental.** These types are part of an experimental wire-protocol
//! surface and may change or be removed in future SDK or CLI releases.
//!
//! </div>
use std::future::Future;
use async_trait::async_trait;
/// Arguments passed to a BYOK bearer-token provider callback.
///
/// <div class="warning">
///
/// **Experimental.** This type is part of an experimental wire-protocol
/// surface and may change or be removed in future SDK or CLI releases.
///
/// </div>
#[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`].
///
/// <div class="warning">
///
/// **Experimental.** This type is part of an experimental wire-protocol
/// surface and may change or be removed in future SDK or CLI releases.
///
/// </div>
#[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<String>) -> 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<String> 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.
///
/// <div class="warning">
///
/// **Experimental.** This trait is part of an experimental wire-protocol
/// surface and may change or be removed in future SDK or CLI releases.
///
/// </div>
#[async_trait]
pub trait BearerTokenProvider: Send + Sync {
/// Acquire a bearer token without the `Bearer ` prefix.
async fn get_token(&self, args: ProviderTokenArgs) -> Result<String, BearerTokenError>;
}
#[async_trait]
impl<F, Fut> BearerTokenProvider for F
where
F: Fn(ProviderTokenArgs) -> Fut + Send + Sync,
Fut: Future<Output = Result<String, BearerTokenError>> + Send,
{
async fn get_token(&self, args: ProviderTokenArgs) -> Result<String, BearerTokenError> {
(self)(args).await
}
}