Skip to content

Commit 36e15ad

Browse files
committed
Use cookie builder to generate session cookie
1 parent d288a26 commit 36e15ad

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

Cargo.lock

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ rand = "0.8.4"
3131
base64 = "0.13.0"
3232
futures = "0.3.19"
3333
serde_urlencoded = "0.7.0"
34+
cookie = "0.16"
3435

src/session.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use axum::{
1414
response::{IntoResponse, Response},
1515
};
1616
use axum_extra::middleware::{self, Next};
17+
use cookie::{self, SameSite};
1718
use futures::future::TryFutureExt;
1819
use rand::RngCore;
1920
use redis::AsyncCommands;
@@ -148,14 +149,15 @@ pub async fn session_uuid_middleware<B>(mut req: Request<B>, next: Next<B>) -> i
148149
)
149150
};
150151

151-
let set_cookie = HeaderValue::from_str(
152-
format!(
153-
"{}={}; Secure; HttpOnly; Path=/; Domain={}",
154-
AXUM_SESSION_COOKIE_NAME, cookie, domain
155-
)
156-
.as_str(),
157-
)
158-
.unwrap();
152+
let full_cookie = cookie::Cookie::build(AXUM_SESSION_COOKIE_NAME, &cookie)
153+
.domain(&domain)
154+
.path("/")
155+
.secure(true)
156+
.same_site(SameSite::Strict)
157+
.http_only(true)
158+
.finish();
159+
160+
let set_cookie = HeaderValue::from_str(full_cookie.to_string().as_str()).unwrap();
159161
headers.insert(http::header::SET_COOKIE, set_cookie);
160162

161163
// It is also possible to call `let res = res.map(axum::body::boxed)`

0 commit comments

Comments
 (0)