use serde::de::StdError;
use std::env;
use std::fmt::Debug;
use std::sync::Arc;
pub trait Credentials: Sized {
fn ak(&self) -> &str;
fn sk(&self) -> &str;
fn security_token(&self) -> &str;
fn new(ak: impl Into<String>, sk: impl Into<String>, security_token: impl Into<String>) -> Result<Self, Box<dyn StdError + Send + Sync>>;
}
pub trait CredentialsProvider<C>: Sized
where
C: Credentials,
{
fn credentials(&self, expires: i64) -> Result<Arc<C>, Box<dyn StdError + Send + Sync>>;
fn new(c: C) -> Result<Self, Box<dyn StdError + Send + Sync>>;
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct CommonCredentialsProvider<C> {
pub(crate) credentials: Arc<C>,
}
impl<C> CredentialsProvider<C> for CommonCredentialsProvider<C>
where
C: Credentials,
{
fn credentials(&self, _: i64) -> Result<Arc<C>, Box<dyn StdError + Send + Sync>> {
Ok(self.credentials.clone())
}
fn new(c: C) -> Result<Self, Box<dyn StdError + Send + Sync>> {
Ok(CommonCredentialsProvider {
credentials: Arc::new(c),
})
}
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct CommonCredentials {
inner: Arc<(String, String, String)>,
}
impl Credentials for CommonCredentials {
fn ak(&self) -> &str {
&self.inner.0
}
fn sk(&self) -> &str {
&self.inner.1
}
fn security_token(&self) -> &str {
&self.inner.2
}
fn new(ak: impl Into<String>, sk: impl Into<String>, security_token: impl Into<String>) -> Result<Self, Box<dyn StdError + Send + Sync>> {
Ok(CommonCredentials {
inner: Arc::new((ak.into(), sk.into(), security_token.into()))
})
}
}
#[derive(Default)]
pub struct StaticCredentialsProvider<C> {
pub(crate) cred: Arc<C>,
}
impl<C> StaticCredentialsProvider<C>
where
C: Credentials,
{
pub(crate) fn new(ak: impl Into<String>, sk: impl Into<String>, security_token: impl Into<String>) -> Result<Self, Box<dyn StdError + Send + Sync>> {
let mut ak = ak.into().trim().to_owned();
let mut sk = sk.into().trim().to_owned();
let mut security_token = security_token.into().trim().to_owned();
if ak == "" {
sk = "".to_owned();
security_token = "".to_owned();
} else if sk == "" {
ak = "".to_owned();
security_token = "".to_owned();
}
let cred = C::new(ak, sk, security_token)?;
Ok(Self { cred: Arc::new(cred) })
}
}
impl<C> CredentialsProvider<C> for StaticCredentialsProvider<C>
where
C: Credentials,
{
fn credentials(&self, _: i64) -> Result<Arc<C>, Box<dyn StdError + Send + Sync>> {
Ok(self.cred.clone())
}
fn new(cred: C) -> Result<Self, Box<dyn StdError + Send + Sync>> {
Ok(Self {
cred: Arc::new(cred),
})
}
}
#[derive(Default)]
pub struct EnvCredentialsProvider<C> {
pub(crate) cred: Arc<C>,
}
impl<C> EnvCredentialsProvider<C>
where
C: Credentials,
{
pub(crate) fn new() -> Result<Self, Box<dyn StdError + Send + Sync>> {
let mut ak = "".to_string();
let mut sk = "".to_string();
let mut security_token = "".to_string();
if let Ok(_ak) = env::var("TOS_ACCESS_KEY") {
if let Ok(_sk) = env::var("TOS_SECRET_KEY") {
ak = _ak;
sk = _sk;
if let Ok(_security_token) = env::var("TOS_SECURITY_TOKEN") {
security_token = _security_token;
}
}
}
let cred = C::new(ak, sk, security_token)?;
Ok(Self { cred: Arc::new(cred) })
}
}
impl<C> CredentialsProvider<C> for EnvCredentialsProvider<C>
where
C: Credentials,
{
fn credentials(&self, _: i64) -> Result<Arc<C>, Box<dyn StdError + Send + Sync>> {
Ok(self.cred.clone())
}
fn new(cred: C) -> Result<Self, Box<dyn StdError + Send + Sync>> {
Ok(Self {
cred: Arc::new(cred),
})
}
}