use std::path::PathBuf;
use std::sync::Mutex;
use crate::auth::_helpers::jwt::{KeyPair, load_key_pair, sign_jwt};
use crate::auth::_helpers::oauth::fresh_access_token;
use crate::auth::_helpers::sign_in::terminal_io;
use crate::auth::_helpers::token_cache::token_cache_file;
use crate::auth::constants::{
JWT_LIFETIME_SECS, JWT_REFRESH_MARGIN_SECS, KEYPAIR_JWT, LOCAL_APPLICATION, OAUTH,
PROGRAMMATIC_ACCESS_TOKEN,
};
use crate::auth::main::account_url::account_url;
use crate::config::models::{AuthConfig, DestinationConfig};
use crate::http::errors::ApiError;
use crate::http::main::http_client::build_http_client;
use crate::http::models::ConnectionCounter;
use crate::time_text::main::now_ms::now_ms;
#[derive(Clone, PartialEq, Eq, serde::Deserialize)]
#[serde(transparent)]
pub struct Secret(pub String);
impl Secret {
pub fn expose(&self) -> &str {
&self.0
}
}
impl std::fmt::Debug for Secret {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("<redacted>")
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Bearer {
pub token: String,
pub token_type: &'static str,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LoginToken {
pub authenticator: &'static str,
pub token: String,
}
#[derive(Debug, Clone)]
pub struct OauthClient {
pub account_url: String,
pub client_id: String,
pub client_secret: Option<Secret>,
pub redirect_uri: Option<String>,
pub user: String,
pub role: Option<String>,
pub cache_file: PathBuf,
pub http: reqwest::Client,
}
#[derive(Debug, Default)]
pub struct OauthState {
pub tokens: Option<OauthTokens>,
pub failure: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OauthTokens {
pub access_token: Secret,
pub renew_after: u64,
pub refresh_token: Option<Secret>,
pub refresh_expires_at: Option<u64>,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub(crate) struct CachedTokens {
pub(crate) access_token: String,
pub(crate) renew_after: u64,
pub(crate) refresh_token: Option<String>,
pub(crate) refresh_expires_at: Option<u64>,
}
#[derive(Debug)]
enum Method {
Pat(Secret),
KeyPair {
key: Box<KeyPair>,
cached: Mutex<Option<(Secret, u64)>>,
},
Oauth {
client: Box<OauthClient>,
tokens: tokio::sync::Mutex<OauthState>,
},
}
#[derive(Debug)]
pub struct Authenticator {
qualified_user: String,
method: Method,
}
impl Authenticator {
pub fn from_config(destination: &DestinationConfig) -> Result<Self, ApiError> {
let account = destination
.account
.split('.')
.next()
.unwrap_or_default()
.to_ascii_uppercase();
let qualified_user = format!("{account}.{}", destination.user.to_ascii_uppercase());
let method = match &destination.auth {
AuthConfig::Pat { token } => Method::Pat(token.clone()),
AuthConfig::KeyPair { private_key_file } => {
let pem = std::fs::read_to_string(private_key_file).map_err(|error| {
ApiError::Auth(format!(
"cannot read {}: {error}",
private_key_file.display()
))
})?;
Method::KeyPair {
key: Box::new(load_key_pair(&pem).map_err(ApiError::Auth)?),
cached: Mutex::new(None),
}
}
AuthConfig::OauthAuthorizationCode {
client_id,
client_secret,
redirect_uri,
} => Method::Oauth {
client: Box::new(OauthClient {
account_url: account_url(destination),
client_id: client_id
.clone()
.unwrap_or_else(|| LOCAL_APPLICATION.to_owned()),
client_secret: client_secret.clone(),
redirect_uri: redirect_uri.clone(),
user: destination.user.clone(),
role: destination.role.clone(),
cache_file: token_cache_file().ok_or_else(|| {
ApiError::Auth(
"no folder to cache the Snowflake sign-in in: set XDG_CACHE_HOME or HOME"
.to_owned(),
)
})?,
http: build_http_client(&ConnectionCounter::default())?,
}),
tokens: tokio::sync::Mutex::new(OauthState::default()),
},
};
Ok(Self {
qualified_user,
method,
})
}
pub fn uses_key_pair(&self) -> bool {
matches!(self.method, Method::KeyPair { .. })
}
pub async fn bearer(&self) -> Result<Bearer, ApiError> {
match &self.method {
Method::Pat(token) => Ok(Bearer {
token: token.expose().to_owned(),
token_type: PROGRAMMATIC_ACCESS_TOKEN,
}),
Method::KeyPair { key, cached } => Ok(Bearer {
token: self.cached_jwt(key, cached)?,
token_type: KEYPAIR_JWT,
}),
Method::Oauth { client, tokens } => Ok(Bearer {
token: fresh_access_token(client, tokens, &terminal_io()).await?,
token_type: OAUTH,
}),
}
}
pub async fn login_token(&self) -> Result<LoginToken, ApiError> {
match &self.method {
Method::Pat(token) => Ok(LoginToken {
authenticator: PROGRAMMATIC_ACCESS_TOKEN,
token: token.expose().to_owned(),
}),
Method::KeyPair { key, cached } => Ok(LoginToken {
authenticator: "SNOWFLAKE_JWT",
token: self.cached_jwt(key, cached)?,
}),
Method::Oauth { client, tokens } => Ok(LoginToken {
authenticator: OAUTH,
token: fresh_access_token(client, tokens, &terminal_io()).await?,
}),
}
}
pub fn describe(&self) -> String {
match &self.method {
Method::Pat(_) => "a programmatic access token".to_owned(),
Method::KeyPair { .. } => "a key pair".to_owned(),
Method::Oauth { client, .. } => format!(
"browser sign-in (OAuth client {}), tokens cached in {}",
client.client_id,
client.cache_file.display()
),
}
}
fn cached_jwt(
&self,
key: &KeyPair,
cached: &Mutex<Option<(Secret, u64)>>,
) -> Result<String, ApiError> {
let now = (now_ms() / 1000) as u64;
let mut guard = cached
.lock()
.map_err(|_| ApiError::Auth("JWT cache lock poisoned".to_owned()))?;
if let Some((token, expires)) = guard.as_ref()
&& *expires > now + JWT_REFRESH_MARGIN_SECS
{
return Ok(token.expose().to_owned());
}
let token =
sign_jwt(key, &self.qualified_user, now, JWT_LIFETIME_SECS).map_err(ApiError::Auth)?;
*guard = Some((Secret(token.clone()), now + JWT_LIFETIME_SECS));
Ok(token)
}
}