Skip to main content

authplane_sdk/
auth_provider.rs

1//! Pluggable authentication providers for AS credentials.
2//!
3//! Defines the `AuthProvider` trait and
4//! `ClientCredentialsProvider` implementation.
5
6use crate::transport::build_basic_auth_header;
7
8/// Trait for providing HTTP authentication headers to the AS.
9///
10/// Implement this trait to plug in custom authentication strategies
11/// (e.g., mTLS, JWT client assertion). The default implementation
12/// [`ClientCredentialsProvider`] uses HTTP Basic authentication with
13/// percent-encoded client credentials.
14pub trait AuthProvider: Send + Sync {
15    /// Return the `Authorization` header value (e.g., `"Basic ..."`)
16    /// to include in outbound AS requests.
17    fn auth_header(&self) -> String;
18}
19
20/// HTTP Basic authentication provider that pre-computes the header
21/// from `client_id` and `client_secret`.
22///
23/// Client-credentials provider.
24#[derive(Debug, Clone)]
25pub struct ClientCredentialsProvider {
26    header: String,
27    client_id: String,
28}
29
30impl ClientCredentialsProvider {
31    pub fn new(client_id: impl Into<String>, client_secret: &str) -> Self {
32        let client_id = client_id.into();
33        let header = build_basic_auth_header(&client_id, client_secret);
34        Self { header, client_id }
35    }
36
37    pub fn client_id(&self) -> &str {
38        &self.client_id
39    }
40}
41
42impl AuthProvider for ClientCredentialsProvider {
43    fn auth_header(&self) -> String {
44        self.header.clone()
45    }
46}
47
48/// Convenience wrapper: treat a pair of `(client_id, client_secret)` as
49/// an ad-hoc auth provider without constructing one explicitly.
50pub struct InlineCredentials {
51    header: String,
52}
53
54impl InlineCredentials {
55    pub fn new(client_id: &str, client_secret: &str) -> Self {
56        Self {
57            header: build_basic_auth_header(client_id, client_secret),
58        }
59    }
60}
61
62impl AuthProvider for InlineCredentials {
63    fn auth_header(&self) -> String {
64        self.header.clone()
65    }
66}