diff options
author | Martin Fischer <martin@push-f.com> | 2021-04-09 15:42:53 +0200 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2021-04-10 19:33:43 +0200 |
commit | 0296cab7787b9d993ba2d932a0b261e6a9b77108 (patch) | |
tree | b07815ae56a9b0974f0e03fdbbf662dc64b0c628 /README.md | |
parent | f755eb02b4be1a2d97941f15c776d2391420ecad (diff) |
drop dependency on cookie & time crates
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 19 |
1 files changed, 12 insertions, 7 deletions
@@ -127,23 +127,28 @@ After a successful authentication you can build a session id cookie for example as follows: ```rust -let expiry_date = OffsetDateTime::now_utc() + Duration::hours(24); +let expiry_date = SystemTime::now() + Duration::from_secs(24 * 60 * 60); let mut cookie = Cookie::new("userid", key.sign( &encode_expiring_claim(&userid, expiry_date) )); -cookie.set_secure(Some(true)); -cookie.set_expires(expiry_date); -cookie.set_same_site(SameSite::Lax); -headers.set_cookie(cookie); +headers.set_cookie(Cookie{ + name: "userid".into(), + value: key.sign( + &encode_expiring_claim(&userid, expiry_date) + ), + secure: Some(true), + expires: Some(expiry_date), + same_site: SameSite::Lax, +}); ``` This session id cookie can then be retrieved and verified as follows: ```rust -let userid = req.cookies().get("userid") +let userid = req.cookies().find(|(name, _value)| *name == "userid") .ok_or_else(|| "expected userid cookie".to_owned()) - .and_then(|cookie| key.verify(cookie.value()) + .and_then(|(_name, value)| key.verify(value)) .and_then(|value| decode_expiring_claim(value).map_err(|e| format!("failed to decode userid cookie: {}", e))); ``` |