diff --git a/crates/reqsign/src/sign/api.rs b/crates/reqsign/src/sign/api.rs index 8c8827d..3ec20a1 100644 --- a/crates/reqsign/src/sign/api.rs +++ b/crates/reqsign/src/sign/api.rs @@ -2,13 +2,13 @@ use super::SigningRequest; use std::fmt::Debug; use std::time::Duration; -/// Context is the trait used by signer as the signing context. -pub trait Context: Clone + Debug + Send + Sync + Unpin + 'static { - /// Check if the context is valid. +/// Key is the trait used by signer as the signing key. +pub trait Key: Clone + Debug + Send + Sync + Unpin + 'static { + /// Check if the key is valid. fn is_valid(&self) -> bool; } -impl Context for Option { +impl Key for Option { fn is_valid(&self) -> bool { let Some(ctx) = self else { return false; @@ -18,34 +18,34 @@ impl Context for Option { } } -/// Load is the trait used by signer to load the context from the environment. +/// Load is the trait used by signer to load the key from the environment. /// -/// Service may require different context to sign the request, for example, AWS require +/// Service may require different key to sign the request, for example, AWS require /// access key and secret key, while Google Cloud Storage require token. #[async_trait::async_trait] pub trait Load: Debug + Send + Sync + Unpin + 'static { - /// Context returned by this loader. + /// Key returned by this loader. /// /// Typically, it will be a credential. - type Context: Send + Sync + Unpin + 'static; + type Key: Send + Sync + Unpin + 'static; - /// Load signing context from current env. - async fn load(&self) -> anyhow::Result>; + /// Load signing key from current env. + async fn load(&self) -> anyhow::Result>; } /// Build is the trait used by signer to build the signing request. #[async_trait::async_trait] pub trait Build: Debug + Send + Sync + Unpin + 'static { - /// Context used by this builder. + /// Key used by this builder. /// /// Typically, it will be a credential. - type Context: Send + Sync + Unpin + 'static; + type Key: Send + Sync + Unpin + 'static; /// Construct the signing request. /// - /// ## Context + /// ## Key /// - /// The `ctx` parameter is the context required by the signer to sign the request. + /// The `key` parameter is the key required by the signer to sign the request. /// /// ## Expires In /// @@ -57,7 +57,7 @@ pub trait Build: Debug + Send + Sync + Unpin + 'static { async fn build( &self, req: &mut http::request::Parts, - ctx: Option<&Self::Context>, + key: Option<&Self::Key>, expires_in: Option, ) -> anyhow::Result; } diff --git a/crates/reqsign/src/sign/mod.rs b/crates/reqsign/src/sign/mod.rs index cc6c2b4..f863576 100644 --- a/crates/reqsign/src/sign/mod.rs +++ b/crates/reqsign/src/sign/mod.rs @@ -1,6 +1,6 @@ mod api; pub use api::Build; -pub use api::Context; +pub use api::Key; pub use api::Load; mod request; diff --git a/crates/reqsign/src/sign/signer.rs b/crates/reqsign/src/sign/signer.rs index 10d9096..9e19d4d 100644 --- a/crates/reqsign/src/sign/signer.rs +++ b/crates/reqsign/src/sign/signer.rs @@ -1,23 +1,23 @@ -use crate::{Build, Context, Load}; +use crate::{Build, Key, Load}; use anyhow::Result; use std::sync::{Arc, Mutex}; use std::time::Duration; /// Signer is the main struct used to sign the request. #[derive(Clone, Debug)] -pub struct Signer { - loader: Arc>, - builder: Arc>, - context: Arc>>, +pub struct Signer { + loader: Arc>, + builder: Arc>, + key: Arc>>, } -impl Signer { +impl Signer { /// Create a new signer. - pub fn new(loader: impl Load, builder: impl Build) -> Self { + pub fn new(loader: impl Load, builder: impl Build) -> Self { Self { loader: Arc::new(loader), builder: Arc::new(builder), - context: Arc::new(Mutex::new(None)), + key: Arc::new(Mutex::new(None)), } } @@ -27,16 +27,16 @@ impl Signer { req: &mut http::request::Parts, expires_in: Option, ) -> Result<()> { - let ctx = self.context.lock().expect("lock poisoned").clone(); - let ctx = if ctx.is_valid() { - ctx + let key = self.key.lock().expect("lock poisoned").clone(); + let key = if key.is_valid() { + key } else { let ctx = self.loader.load().await?; - *self.context.lock().expect("lock poisoned") = ctx.clone(); + *self.key.lock().expect("lock poisoned") = ctx.clone(); ctx }; - let signing = self.builder.build(req, ctx.as_ref(), expires_in).await?; + let signing = self.builder.build(req, key.as_ref(), expires_in).await?; signing.apply(req) } }