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

Available on crate features 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());
}
Available on crate features 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"))
}
Available on crate features 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"))
}
Available on crate features cookie and cookie-private only.

Authenticates and decrypts cookie, returning the plaintext version if decryption succeeds or None otherwise.

Available on crate features cookie and cookie-private only.

Get an iterator over all cookies in the jar.

Only cookies with valid authenticity and integrity are yielded by the iterator.

Trait Implementations

Formats the value using the given formatter. Read more

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response. Read more

Perform the extraction.

Create a response.

The type returned in the event of an error. Read more

Set parts of the response

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more