Struct axum_extra::extract::cookie::SignedCookieJar
source · [−]pub struct SignedCookieJar<K = Key> { /* private fields */ }
cookie
only.Expand description
Extractor that grabs signed cookies from the request and manages the jar.
All cookies will be signed and verified with a Key
. Do not use this to store private data
as the values are still transmitted in plaintext.
Note that methods like SignedCookieJar::add
, SignedCookieJar::remove
, etc updates the
SignedCookieJar
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::{SignedCookieJar, Cookie, Key};
async fn create_session(
TypedHeader(auth): TypedHeader<Authorization<Bearer>>,
jar: SignedCookieJar,
) -> Result<(SignedCookieJar, Redirect), StatusCode> {
if let Some(session_id) = authorize_and_create_session(auth.token()).await {
Ok((
// the updated jar must be returned for the changes
// to be included in the response
jar.add(Cookie::new("session_id", session_id)),
Redirect::to("/me"),
))
} else {
Err(StatusCode::UNAUTHORIZED)
}
}
async fn me(jar: SignedCookieJar) -> Result<(), StatusCode> {
if let Some(session_id) = jar.get("session_id") {
// fetch and render user...
} else {
Err(StatusCode::UNAUTHORIZED)
}
}
async fn authorize_and_create_session(token: &str) -> Option<String> {
// authorize the user and create a session...
}
// 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("/sessions", post(create_session))
.route("/me", get(me))
// add extension with the key so `SignedCookieJar` can access it
.layer(Extension(key));
Implementations
sourceimpl<K> SignedCookieJar<K>
impl<K> SignedCookieJar<K>
sourcepub fn get(&self, name: &str) -> Option<Cookie<'static>>
Available on crate feature cookie-signed
only.
pub fn get(&self, name: &str) -> Option<Cookie<'static>>
cookie-signed
only.Get a cookie from the jar.
If the cookie exists and its authenticity and integrity can be verified then it is returned in plaintext.
Example
use axum_extra::extract::cookie::SignedCookieJar;
use axum::response::IntoResponse;
async fn handle(jar: SignedCookieJar) {
let value: Option<String> = jar
.get("foo")
.map(|cookie| cookie.value().to_owned());
}
sourcepub fn remove(self, cookie: Cookie<'static>) -> Self
Available on crate feature cookie-signed
only.
pub fn remove(self, cookie: Cookie<'static>) -> Self
cookie-signed
only.Remove a cookie from the jar.
Example
use axum_extra::extract::cookie::{SignedCookieJar, Cookie};
use axum::response::IntoResponse;
async fn handle(jar: SignedCookieJar) -> SignedCookieJar {
jar.remove(Cookie::named("foo"))
}
sourcepub fn add(self, cookie: Cookie<'static>) -> Self
Available on crate feature cookie-signed
only.
pub fn add(self, cookie: Cookie<'static>) -> Self
cookie-signed
only.Add a cookie to the jar.
The value will automatically be percent-encoded.
Example
use axum_extra::extract::cookie::{SignedCookieJar, Cookie};
use axum::response::IntoResponse;
async fn handle(jar: SignedCookieJar) -> SignedCookieJar {
jar.add(Cookie::new("foo", "bar"))
}
Trait Implementations
sourceimpl<K> Debug for SignedCookieJar<K>
impl<K> Debug for SignedCookieJar<K>
sourceimpl<B, K> FromRequest<B> for SignedCookieJar<K> where
B: Send,
K: Into<Key> + Clone + Send + Sync + 'static,
impl<B, K> FromRequest<B> for SignedCookieJar<K> where
B: Send,
K: Into<Key> + Clone + Send + Sync + 'static,
sourceimpl<K> IntoResponse for SignedCookieJar<K>
impl<K> IntoResponse for SignedCookieJar<K>
sourcefn into_response(self) -> Response
fn into_response(self) -> Response
Create a response.
sourceimpl<K> IntoResponseParts for SignedCookieJar<K>
impl<K> IntoResponseParts for SignedCookieJar<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 SignedCookieJar<K> where
K: RefUnwindSafe,
impl<K> Send for SignedCookieJar<K> where
K: Send,
impl<K> Sync for SignedCookieJar<K> where
K: Sync,
impl<K> Unpin for SignedCookieJar<K> where
K: Unpin,
impl<K> UnwindSafe for SignedCookieJar<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