How do I use guard transparency with references? #2438
-
Hey Sergio and all other important people of this framework It is really amazing what you did! I'm quite done with the code and the docs. However, something bothers me before I want to release the new version of the crate: In the documentation of rocket (0.5-rc), I found the documentation about the guards and their examples. In the section about Guard Transparency, they talk about increased security when leveraging request guard transparency for all data access. However, when constructing the object during the My guard code can be found here: https://github.com/smartive/zitadel-rust/blob/feat/rocket-auth/src/rocket/introspection/guard.rs I really struggle on how I'm able to return a reference to my created struct instead of the normal type (it does not implement copy however.). Currently it is: #[async_trait]
impl<'request> FromRequest<'request> for IntrospectedUser { instead of: #[async_trait]
impl<'request> FromRequest<'request> for &'request IntrospectedUser { Can somebody nudge me in the right direction? Thank you all, and a happy new year! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Ok so, after another try, I found out that I'm able to return a reference if I use the local_cache (or local_cache_async). Is it correct to use this cache to create the request-bound object and return a reference to it? In my case, using the cache makes sense such that the introspection call is not repeated for the same request, but other use-cases may vary. |
Beta Was this translation helpful? Give feedback.
-
You are able to return references safely! This is what lifetimes are for. You simply need to let the type system know where the reference comes from. You do this using a lifetime annotation: fn f<'r>(v: &'r T) -> &'r T
{ v } Applied to request guards, your guard would need to hold a reference to the request. This implies that the guard's type must have a lifetime generic. Then simply concretize the generic as the lifetime of the request in the |
Beta Was this translation helpful? Give feedback.
You are able to return references safely! This is what lifetimes are for. You simply need to let the type system know where the reference comes from. You do this using a lifetime annotation:
Applied to request guards, your guard would need to hold a reference to the request. This implies that the guard's type must have a lifetime generic. Then simply concretize the generic as the lifetime of the request in the
impl
and Rust should take care of the rest.