Skip to content

Commit

Permalink
refactor: Rename to Key to better represents credentails and token
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <[email protected]>
  • Loading branch information
Xuanwo committed Sep 23, 2024
1 parent 5b804d3 commit af8072d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
30 changes: 15 additions & 15 deletions crates/reqsign/src/sign/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Context> Context for Option<T> {
impl<T: Key> Key for Option<T> {
fn is_valid(&self) -> bool {
let Some(ctx) = self else {
return false;
Expand All @@ -18,34 +18,34 @@ impl<T: Context> Context for Option<T> {
}
}

/// 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<Option<Self::Context>>;
/// Load signing key from current env.
async fn load(&self) -> anyhow::Result<Option<Self::Key>>;
}

/// 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
///
Expand All @@ -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<Duration>,
) -> anyhow::Result<SigningRequest>;
}
2 changes: 1 addition & 1 deletion crates/reqsign/src/sign/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod api;
pub use api::Build;
pub use api::Context;
pub use api::Key;
pub use api::Load;

mod request;
Expand Down
26 changes: 13 additions & 13 deletions crates/reqsign/src/sign/signer.rs
Original file line number Diff line number Diff line change
@@ -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<Ctx: Context> {
loader: Arc<dyn Load<Context = Ctx>>,
builder: Arc<dyn Build<Context = Ctx>>,
context: Arc<Mutex<Option<Ctx>>>,
pub struct Signer<K: Key> {
loader: Arc<dyn Load<Key = K>>,
builder: Arc<dyn Build<Key = K>>,
key: Arc<Mutex<Option<K>>>,
}

impl<Ctx: Context> Signer<Ctx> {
impl<K: Key> Signer<K> {
/// Create a new signer.
pub fn new(loader: impl Load<Context = Ctx>, builder: impl Build<Context = Ctx>) -> Self {
pub fn new(loader: impl Load<Key = K>, builder: impl Build<Key = K>) -> Self {
Self {
loader: Arc::new(loader),
builder: Arc::new(builder),
context: Arc::new(Mutex::new(None)),
key: Arc::new(Mutex::new(None)),
}
}

Expand All @@ -27,16 +27,16 @@ impl<Ctx: Context> Signer<Ctx> {
req: &mut http::request::Parts,
expires_in: Option<Duration>,
) -> 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)
}
}

0 comments on commit af8072d

Please sign in to comment.