Skip to content

Commit 99654fd

Browse files
committed
add arbitrary function context resolvers
1 parent 1d78468 commit 99654fd

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

src/keys.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,36 @@ impl<T: IntoKey + Sync> IntoKey for TimeCachingKey<T> {
298298
}
299299
}
300300

301+
/// A wrapper for a closure that implements `IntoSignature`.
302+
/// This uses the newtype pattern to avoid conflicting blanket impls.
303+
pub struct FnSigner<F>(pub F);
304+
305+
/// A wrapper for a closure that implements `IntoKey`.
306+
/// This uses the newtype pattern to avoid conflicting blanket impls.
307+
pub struct FnKey<F>(pub F);
308+
309+
/// Blanket implementation for the FnSigner wrapper.
310+
impl<F, Fut> IntoSignature for FnSigner<F>
311+
where
312+
F: Fn(&[u8]) -> Fut,
313+
Fut: Future<Output = Result<Signature, SigningError>> + Send,
314+
{
315+
fn sign(&self, message: &[u8]) -> impl Future<Output = Result<Signature, SigningError>> + Send {
316+
(self.0)(message)
317+
}
318+
}
319+
320+
/// Blanket implementation for the FnKey wrapper.
321+
impl<F, Fut> IntoKey for FnKey<F>
322+
where
323+
F: Fn() -> Fut,
324+
Fut: Future<Output = Result<Key, KeyError>> + Send,
325+
{
326+
fn get_key(&self) -> impl Future<Output = Result<Key, KeyError>> + Send {
327+
(self.0)()
328+
}
329+
}
330+
301331
/// A key that is sourced from the user identified by the provided JWT.
302332
///
303333
/// This is used in JWT-based authentication. When attempting to sign,

0 commit comments

Comments
 (0)