. minor code relayout (auth_data)

This commit is contained in:
Jeremy Chone
2024-12-08 09:44:11 -08:00
parent 6ad128fc89
commit 45935ace24
3 changed files with 71 additions and 73 deletions

68
src/resolver/auth_data.rs Normal file
View File

@ -0,0 +1,68 @@
use crate::resolver::{Error, Result};
use std::collections::HashMap;
/// `AuthData` specifies either how or the key itself for an authentication resolver call.
#[derive(Clone)]
pub enum AuthData {
/// Specify the environment name to get the key value from.
FromEnv(String),
/// The key value itself.
Key(String),
/// The key names/values when a credential has multiple pieces of credential information.
/// This will be adapter-specific.
/// NOTE: Not used yet.
MultiKeys(HashMap<String, String>),
}
/// Constructors
impl AuthData {
/// Create a new `AuthData` from an environment variable name.
pub fn from_env(env_name: impl Into<String>) -> Self {
AuthData::FromEnv(env_name.into())
}
/// Create a new `AuthData` from a single value.
pub fn from_single(value: impl Into<String>) -> Self {
AuthData::Key(value.into())
}
/// Create a new `AuthData` from multiple values.
pub fn from_multi(data: HashMap<String, String>) -> Self {
AuthData::MultiKeys(data)
}
}
/// Getters
impl AuthData {
/// Get the single value from the `AuthData`.
pub fn single_value(&self) -> Result<String> {
match self {
AuthData::FromEnv(env_name) => {
// Get value from the environment name.
let value = std::env::var(env_name).map_err(|_| Error::ApiKeyEnvNotFound {
env_name: env_name.to_string(),
})?;
Ok(value)
}
AuthData::Key(value) => Ok(value.to_string()),
AuthData::MultiKeys(_) => Err(Error::ResolverAuthDataNotSingleValue),
}
}
}
// region: --- AuthData Std Impls
// Implement Debug to redact sensitive information.
impl std::fmt::Debug for AuthData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
// NOTE: Here we also redact for `FromEnv` in case the developer confuses this with a key.
AuthData::FromEnv(_env_name) => write!(f, "AuthData::FromEnv(REDACTED)"),
AuthData::Key(_) => write!(f, "AuthData::Single(REDACTED)"),
AuthData::MultiKeys(_) => write!(f, "AuthData::Multi(REDACTED)"),
}
}
}
// endregion: --- AuthData Std Impls

View File

@ -6,9 +6,8 @@
//!
//! Note: `AuthData` is typically a single value but can be multiple for future adapters (e.g., AWS Bedrock).
use crate::resolver::{Error, Result};
use crate::resolver::{AuthData, Result};
use crate::ModelIden;
use std::collections::HashMap;
use std::sync::Arc;
// region: --- AuthResolver
@ -106,74 +105,3 @@ where
}
// endregion: --- IntoAuthResolverFn
// region: --- AuthData
/// `AuthData` specifies either how or the key itself for an authentication resolver call.
#[derive(Clone)]
pub enum AuthData {
/// Specify the environment name to get the key value from.
FromEnv(String),
/// The key value itself.
Key(String),
/// The key names/values when a credential has multiple pieces of credential information.
/// This will be adapter-specific.
/// NOTE: Not used yet.
MultiKeys(HashMap<String, String>),
}
/// Constructors
impl AuthData {
/// Create a new `AuthData` from an environment variable name.
pub fn from_env(env_name: impl Into<String>) -> Self {
AuthData::FromEnv(env_name.into())
}
/// Create a new `AuthData` from a single value.
pub fn from_single(value: impl Into<String>) -> Self {
AuthData::Key(value.into())
}
/// Create a new `AuthData` from multiple values.
pub fn from_multi(data: HashMap<String, String>) -> Self {
AuthData::MultiKeys(data)
}
}
/// Getters
impl AuthData {
/// Get the single value from the `AuthData`.
pub fn single_value(&self) -> Result<String> {
match self {
AuthData::FromEnv(env_name) => {
// Get value from the environment name.
let value = std::env::var(env_name).map_err(|_| Error::ApiKeyEnvNotFound {
env_name: env_name.to_string(),
})?;
Ok(value)
}
AuthData::Key(value) => Ok(value.to_string()),
AuthData::MultiKeys(_) => Err(Error::ResolverAuthDataNotSingleValue),
}
}
}
// endregion: --- AuthData
// region: --- AuthData Std Impls
// Implement Debug to redact sensitive information.
impl std::fmt::Debug for AuthData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
// NOTE: Here we also redact for `FromEnv` in case the developer confuses this with a key.
AuthData::FromEnv(_env_name) => write!(f, "AuthData::FromEnv(REDACTED)"),
AuthData::Key(_) => write!(f, "AuthData::Single(REDACTED)"),
AuthData::MultiKeys(_) => write!(f, "AuthData::Multi(REDACTED)"),
}
}
}
// endregion: --- AuthData Std Impls

View File

@ -5,10 +5,12 @@
// region: --- Modules
mod auth_data;
mod auth_resolver;
mod error;
mod model_mapper;
pub use auth_data::*;
pub use auth_resolver::*;
pub use error::{Error, Result};
pub use model_mapper::*;