rig_core/providers/internal/
auth.rs1use crate::http_client::{self, HttpClientExt};
11use crate::wasm_compat::{WasmCompatSend, WasmCompatSync};
12use std::sync::Arc;
13
14#[derive(Debug, Clone)]
16pub struct DeviceCodePrompt {
17 pub verification_uri: String,
19 pub user_code: String,
21}
22
23#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
26pub(crate) type DeviceCodeCallback = dyn Fn(DeviceCodePrompt) + Send + Sync;
27#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
28pub(crate) type DeviceCodeCallback = dyn Fn(DeviceCodePrompt);
29
30#[derive(Clone, Default)]
32pub struct DeviceCodeHandler(pub(crate) Option<Arc<DeviceCodeCallback>>);
33
34impl DeviceCodeHandler {
35 pub fn new<F>(handler: F) -> Self
37 where
38 F: Fn(DeviceCodePrompt) + WasmCompatSend + WasmCompatSync + 'static,
39 {
40 Self(Some(Arc::new(handler)))
41 }
42}
43
44impl std::fmt::Debug for DeviceCodeHandler {
45 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46 if self.0.is_some() {
47 f.write_str("DeviceCodeHandler(<callback>)")
48 } else {
49 f.write_str("DeviceCodeHandler(None)")
50 }
51 }
52}
53
54#[derive(Debug, thiserror::Error)]
55pub enum AuthError {
56 #[error("{0}")]
57 Message(String),
58 #[error(transparent)]
59 Io(#[from] std::io::Error),
60 #[error(transparent)]
61 Json(#[from] serde_json::Error),
62 #[error(transparent)]
66 Http(#[from] http_client::Error),
67}
68
69pub(crate) fn request(method: http::Method, url: &str) -> http::request::Builder {
71 http::Request::builder().method(method).uri(url)
72}
73
74pub(crate) async fn send_json<H, T>(
78 http: &H,
79 req: http::Result<http::Request<bytes::Bytes>>,
80) -> Result<T, AuthError>
81where
82 H: HttpClientExt,
83 T: serde::de::DeserializeOwned,
84{
85 let bytes = send_bytes(http, req).await?;
86 Ok(serde_json::from_slice(&bytes)?)
87}
88
89pub(crate) async fn send_bytes<H>(
91 http: &H,
92 req: http::Result<http::Request<bytes::Bytes>>,
93) -> Result<bytes::Bytes, AuthError>
94where
95 H: HttpClientExt,
96{
97 let req = req.map_err(http_client::Error::Protocol)?;
98 let response = http.send::<_, bytes::Bytes>(req).await?;
99 Ok(response.into_body().await?)
100}
101
102pub(crate) fn config_dir() -> Option<std::path::PathBuf> {
106 use std::path::PathBuf;
107
108 #[cfg(target_os = "windows")]
109 {
110 std::env::var_os("APPDATA").map(PathBuf::from)
111 }
112
113 #[cfg(not(target_os = "windows"))]
114 {
115 std::env::var_os("XDG_CONFIG_HOME")
116 .map(PathBuf::from)
117 .or_else(|| std::env::var_os("HOME").map(|home| PathBuf::from(home).join(".config")))
118 }
119}
120
121#[cfg(not(target_family = "wasm"))]
123pub(crate) mod device;