pub struct PrivateCookieJar<K = Key> { /* private fields */ }
cookie-private
and cookie
only.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,
routing::{post, get},
extract::FromRef,
response::{IntoResponse, Redirect},
http::StatusCode,
};
use axum_extra::{
TypedHeader,
headers::authorization::{Authorization, Bearer},
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") {
// ...
}
}
// our application state
#[derive(Clone)]
struct AppState {
// that holds the key used to sign cookies
key: Key,
}
// this impl tells `SignedCookieJar` how to access the key from our state
impl FromRef<AppState> for Key {
fn from_ref(state: &AppState) -> Self {
state.key.clone()
}
}
let state = AppState {
// Generate a secure key
//
// You probably don't wanna generate a new one each time the app starts though
key: Key::generate(),
};
let app = Router::new()
.route("/set", post(set_secret))
.route("/get", get(get_secret))
.with_state(state);
If you have been using Arc<AppState>
you cannot implement FromRef<Arc<AppState>> for Key
.
You can use a new type instead:
use std::sync::Arc;
use std::ops::Deref;
#[derive(Clone)]
struct AppState(Arc<InnerState>);
// deref so you can still access the inner fields easily
impl Deref for AppState {
type Target = InnerState;
fn deref(&self) -> &Self::Target {
&self.0
}
}
struct InnerState {
key: Key
}
impl FromRef<AppState> for Key {
fn from_ref(state: &AppState) -> Self {
state.0.key.clone()
}
}
Implementations§
Source§impl PrivateCookieJar
impl PrivateCookieJar
Sourcepub fn from_headers(headers: &HeaderMap, key: Key) -> Self
pub fn from_headers(headers: &HeaderMap, key: Key) -> Self
Create a new PrivateCookieJar
from a map of request headers.
The valid cookies in headers
will be added to the jar.
This is intended to be used in middleware and other where places it might be difficult to
run extractors. Normally you should create PrivateCookieJar
s through FromRequestParts
.
Sourcepub fn new(key: Key) -> Self
pub fn new(key: Key) -> Self
Create a new empty PrivateCookieJarIter
.
This is intended to be used in middleware and other places where it might be difficult to
run extractors. Normally you should create PrivateCookieJar
s through FromRequestParts
.
Source§impl<K> PrivateCookieJar<K>
impl<K> PrivateCookieJar<K>
Sourcepub fn get(&self, name: &str) -> Option<Cookie<'static>>
pub fn get(&self, name: &str) -> Option<Cookie<'static>>
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<C: Into<Cookie<'static>>>(self, cookie: C) -> Self
pub fn remove<C: Into<Cookie<'static>>>(self, cookie: C) -> Self
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::from("foo"))
}
Sourcepub fn add<C: Into<Cookie<'static>>>(self, cookie: C) -> Self
pub fn add<C: Into<Cookie<'static>>>(self, cookie: C) -> Self
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§
Source§impl<K> Clone for PrivateCookieJar<K>
impl<K> Clone for PrivateCookieJar<K>
Source§impl<K> Debug for PrivateCookieJar<K>
impl<K> Debug for PrivateCookieJar<K>
Source§impl<S, K> FromRequestParts<S> for PrivateCookieJar<K>
impl<S, K> FromRequestParts<S> for PrivateCookieJar<K>
Source§type Rejection = Infallible
type Rejection = Infallible
Source§impl<K> IntoResponse for PrivateCookieJar<K>
impl<K> IntoResponse for PrivateCookieJar<K>
Source§fn into_response(self) -> Response
fn into_response(self) -> Response
Source§impl<K> IntoResponseParts for PrivateCookieJar<K>
impl<K> IntoResponseParts for PrivateCookieJar<K>
Source§type Error = Infallible
type Error = Infallible
Source§fn into_response_parts(
self,
res: ResponseParts,
) -> Result<ResponseParts, Self::Error>
fn into_response_parts( self, res: ResponseParts, ) -> Result<ResponseParts, Self::Error>
Auto Trait Implementations§
impl<K> Freeze for PrivateCookieJar<K>
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§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<S, T> FromRequest<S, ViaParts> for T
impl<S, T> FromRequest<S, ViaParts> for T
Source§type Rejection = <T as FromRequestParts<S>>::Rejection
type Rejection = <T as FromRequestParts<S>>::Rejection
Source§impl<T, S> Handler<IntoResponseHandler, S> for T
impl<T, S> Handler<IntoResponseHandler, S> for T
Source§fn call(
self,
_req: Request<Body>,
_state: S,
) -> <T as Handler<IntoResponseHandler, S>>::Future
fn call( self, _req: Request<Body>, _state: S, ) -> <T as Handler<IntoResponseHandler, S>>::Future
Source§fn layer<L>(self, layer: L) -> Layered<L, Self, T, S>where
L: Layer<HandlerService<Self, T, S>> + Clone,
<L as Layer<HandlerService<Self, T, S>>>::Service: Service<Request<Body>>,
fn layer<L>(self, layer: L) -> Layered<L, Self, T, S>where
L: Layer<HandlerService<Self, T, S>> + Clone,
<L as Layer<HandlerService<Self, T, S>>>::Service: Service<Request<Body>>,
tower::Layer
to the handler. Read moreSource§fn with_state(self, state: S) -> HandlerService<Self, T, S>
fn with_state(self, state: S) -> HandlerService<Self, T, S>
Service
by providing the stateSource§impl<H, T> HandlerWithoutStateExt<T> for H
impl<H, T> HandlerWithoutStateExt<T> for H
Source§fn into_service(self) -> HandlerService<H, T, ()>
fn into_service(self) -> HandlerService<H, T, ()>
Service
and no state.Source§fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>
fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>
MakeService
and no state. Read more