Struct axum_extra::extract::PrivateCookieJar
source · [−]pub struct PrivateCookieJar<K = Key> { /* private fields */ }
Expand description
Extractor that grabs private cookies from the request and manages the jar.
All cookies will be private and encrypted with a Key
. This makes it suitable for storing
private data.
Note that methods like PrivateCookieJar::add
, PrivateCookieJar::remove
, etc updates the
PrivateCookieJar
and returns it. This value must be returned from the handler as part of
the response for the changes to be propagated.
Example
use axum::{
Router,
Extension,
routing::{post, get},
extract::TypedHeader,
response::{IntoResponse, Redirect},
headers::authorization::{Authorization, Bearer},
http::StatusCode,
};
use axum_extra::extract::cookie::{PrivateCookieJar, Cookie, Key};
async fn set_secret(
jar: PrivateCookieJar,
) -> (PrivateCookieJar, Redirect) {
let updated_jar = jar.add(Cookie::new("secret", "secret-data"));
(updated_jar, Redirect::to("/get"))
}
async fn get_secret(jar: PrivateCookieJar) {
if let Some(data) = jar.get("secret") {
// ...
}
}
// Generate a secure key
//
// You probably don't wanna generate a new one each time the app starts though
let key = Key::generate();
let app = Router::new()
.route("/set", post(set_secret))
.route("/get", get(get_secret))
// add extension with the key so `PrivateCookieJar` can access it
.layer(Extension(key));
Implementations
sourceimpl<K> PrivateCookieJar<K>
impl<K> PrivateCookieJar<K>
sourcepub fn get(&self, name: &str) -> Option<Cookie<'static>>
Available on crate features cookie
and cookie-private
only.
pub fn get(&self, name: &str) -> Option<Cookie<'static>>
cookie
and cookie-private
only.Get a cookie from the jar.
If the cookie exists and can be decrypted then it is returned in plaintext.
Example
use axum_extra::extract::cookie::PrivateCookieJar;
use axum::response::IntoResponse;
async fn handle(jar: PrivateCookieJar) {
let value: Option<String> = jar
.get("foo")
.map(|cookie| cookie.value().to_owned());
}
sourcepub fn remove(self, cookie: Cookie<'static>) -> Self
Available on crate features cookie
and cookie-private
only.
pub fn remove(self, cookie: Cookie<'static>) -> Self
cookie
and cookie-private
only.Remove a cookie from the jar.
Example
use axum_extra::extract::cookie::{PrivateCookieJar, Cookie};
use axum::response::IntoResponse;
async fn handle(jar: PrivateCookieJar) -> PrivateCookieJar {
jar.remove(Cookie::named("foo"))
}
sourcepub fn add(self, cookie: Cookie<'static>) -> Self
Available on crate features cookie
and cookie-private
only.
pub fn add(self, cookie: Cookie<'static>) -> Self
cookie
and cookie-private
only.Add a cookie to the jar.
The value will automatically be percent-encoded.
Example
use axum_extra::extract::cookie::{PrivateCookieJar, Cookie};
use axum::response::IntoResponse;
async fn handle(jar: PrivateCookieJar) -> PrivateCookieJar {
jar.add(Cookie::new("foo", "bar"))
}
Trait Implementations
sourceimpl<K> Debug for PrivateCookieJar<K>
impl<K> Debug for PrivateCookieJar<K>
sourceimpl<B, K> FromRequest<B> for PrivateCookieJar<K> where
B: Send,
K: Into<Key> + Clone + Send + Sync + 'static,
impl<B, K> FromRequest<B> for PrivateCookieJar<K> where
B: Send,
K: Into<Key> + Clone + Send + Sync + 'static,
sourceimpl<K> IntoResponse for PrivateCookieJar<K>
impl<K> IntoResponse for PrivateCookieJar<K>
sourcefn into_response(self) -> Response
fn into_response(self) -> Response
Create a response.
sourceimpl<K> IntoResponseParts for PrivateCookieJar<K>
impl<K> IntoResponseParts for PrivateCookieJar<K>
type Error = Infallible
type Error = Infallible
The type returned in the event of an error. Read more
sourcefn into_response_parts(
self,
res: ResponseParts
) -> Result<ResponseParts, Self::Error>
fn into_response_parts(
self,
res: ResponseParts
) -> Result<ResponseParts, Self::Error>
Set parts of the response
Auto Trait Implementations
impl<K> RefUnwindSafe for PrivateCookieJar<K> where
K: RefUnwindSafe,
impl<K> Send for PrivateCookieJar<K> where
K: Send,
impl<K> Sync for PrivateCookieJar<K> where
K: Sync,
impl<K> Unpin for PrivateCookieJar<K> where
K: Unpin,
impl<K> UnwindSafe for PrivateCookieJar<K> where
K: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more