cipherstash-client 0.12.5

The official CipherStash SDK
Documentation
use async_trait::async_trait;
use miette::Diagnostic;
use thiserror::Error;

use crate::credentials::{ClearTokenError, Credentials, GetTokenError, TokenExpiry};

pub struct StaticCredentials<T: for<'a> TokenExpiry<'a> + Clone> {
    token: T,
}

#[derive(Diagnostic, Error, Debug)]
pub enum AcquireTokenError {
    #[error("Failed to acquire token: {0}")]
    RequestFailed(Box<dyn std::error::Error + Sync + Send>),

    #[error("Failed to parse json response: {0}")]
    BadResponse(Box<dyn std::error::Error + Sync + Send>),
}

impl<T: Clone + for<'a> TokenExpiry<'a>> StaticCredentials<T> {
    pub fn new(token: T) -> Self {
        Self { token }
    }
}

#[async_trait]
impl<T: for<'a> TokenExpiry<'a> + Clone + Send + Sync + 'static> Credentials
    for StaticCredentials<T>
{
    type Token = T;

    async fn get_token(&self) -> Result<Self::Token, GetTokenError> {
        Ok(self.token.clone())
    }

    async fn clear_token(&self) -> Result<(), ClearTokenError> {
        Ok(())
    }
}