File size: 4,602 Bytes
e5034c3 | 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | use std::collections::HashMap;
use chrono::{DateTime, Utc};
use derive_setters::Setters;
use serde::{Deserialize, Serialize};
use crate::{AccessToken, ApiKey, OAuthConfig, ProviderId, RefreshToken, URLParam, URLParamValue};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Setters)]
pub struct AuthCredential {
pub id: ProviderId,
pub auth_details: AuthDetails,
#[serde(skip_serializing_if = "HashMap::is_empty", default)]
pub url_params: HashMap<URLParam, URLParamValue>,
}
impl AuthCredential {
pub fn new_api_key(id: ProviderId, api_key: ApiKey) -> Self {
Self {
id,
auth_details: AuthDetails::ApiKey(api_key),
url_params: HashMap::new(),
}
}
pub fn new_oauth(id: ProviderId, tokens: OAuthTokens, config: OAuthConfig) -> Self {
Self {
id,
auth_details: AuthDetails::OAuth { tokens, config },
url_params: HashMap::new(),
}
}
pub fn new_oauth_with_api_key(
id: ProviderId,
tokens: OAuthTokens,
api_key: ApiKey,
config: OAuthConfig,
) -> Self {
Self {
id,
auth_details: AuthDetails::OAuthWithApiKey { tokens, api_key, config },
url_params: HashMap::new(),
}
}
pub fn new_aws_profile(id: ProviderId, profile_name: ApiKey) -> Self {
Self {
id,
auth_details: AuthDetails::AwsProfile(profile_name),
url_params: HashMap::new(),
}
}
pub fn new_google_adc(id: ProviderId, access_token: ApiKey) -> Self {
Self {
id,
auth_details: AuthDetails::GoogleAdc(access_token),
url_params: HashMap::new(),
}
}
/// Checks if the credential needs to be refreshed.
pub fn needs_refresh(&self, buffer: chrono::Duration) -> bool {
match &self.auth_details {
AuthDetails::ApiKey(_) => false,
// AWS Profile credentials are managed by the AWS SDK internally
AuthDetails::AwsProfile(_) => false,
// Google ADC tokens are short-lived (1 hour) and should always be checked/refreshed
AuthDetails::GoogleAdc(_) => true,
AuthDetails::OAuth { tokens, .. } | AuthDetails::OAuthWithApiKey { tokens, .. } => {
tokens.needs_refresh(buffer)
}
}
}
/// Gets the OAuth config if this credential is OAuth-based
pub fn oauth_config(&self) -> Option<&OAuthConfig> {
match &self.auth_details {
AuthDetails::OAuth { config, .. } | AuthDetails::OAuthWithApiKey { config, .. } => {
Some(config)
}
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AuthDetails {
#[serde(alias = "ApiKey")]
ApiKey(ApiKey),
#[serde(alias = "GoogleAdc")]
GoogleAdc(ApiKey),
#[serde(alias = "AwsProfile")]
AwsProfile(ApiKey),
#[serde(alias = "OAuth")]
OAuth {
tokens: OAuthTokens,
config: OAuthConfig,
},
#[serde(alias = "OAuthWithApiKey")]
OAuthWithApiKey {
tokens: OAuthTokens,
api_key: ApiKey,
config: OAuthConfig,
},
}
impl AuthDetails {
pub fn api_key(&self) -> Option<&ApiKey> {
match self {
AuthDetails::ApiKey(api_key) => Some(api_key),
AuthDetails::GoogleAdc(api_key) => Some(api_key),
AuthDetails::AwsProfile(_) => None,
AuthDetails::OAuth { .. } => None,
AuthDetails::OAuthWithApiKey { .. } => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OAuthTokens {
pub access_token: AccessToken,
pub refresh_token: Option<RefreshToken>,
pub expires_at: DateTime<Utc>,
}
impl OAuthTokens {
pub fn new(
access_token: impl ToString,
refresh_token: Option<impl ToString>,
expires_at: DateTime<Utc>,
) -> Self {
Self {
access_token: access_token.to_string().into(),
refresh_token: refresh_token.map(|a| a.to_string().into()),
expires_at,
}
}
/// Checks if the token is expired or will expire within the given buffer
/// duration
pub fn needs_refresh(&self, buffer: chrono::Duration) -> bool {
let now = Utc::now();
now + buffer >= self.expires_at
}
/// Checks if the token is currently expired
pub fn is_expired(&self) -> bool {
Utc::now() >= self.expires_at
}
}
|