1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +01:00

Add SameSite option to identity middleware cookie (#581)

This commit is contained in:
Julian Tescher 2018-11-07 12:24:06 -08:00 committed by Douman
parent 3b536ee96c
commit 8e354021d4
2 changed files with 14 additions and 1 deletions

View File

@ -9,6 +9,7 @@
### Added ### Added
* Add method to configure custom error handler to `Query` and `Path` extractors. * Add method to configure custom error handler to `Query` and `Path` extractors.
* Add method to configure `SameSite` option in `CookieIdentityPolicy`.
## [0.7.13] - 2018-10-14 ## [0.7.13] - 2018-10-14

View File

@ -48,7 +48,7 @@
//! ``` //! ```
use std::rc::Rc; use std::rc::Rc;
use cookie::{Cookie, CookieJar, Key}; use cookie::{Cookie, CookieJar, Key, SameSite};
use futures::future::{err as FutErr, ok as FutOk, FutureResult}; use futures::future::{err as FutErr, ok as FutOk, FutureResult};
use futures::Future; use futures::Future;
use time::Duration; use time::Duration;
@ -237,6 +237,7 @@ struct CookieIdentityInner {
domain: Option<String>, domain: Option<String>,
secure: bool, secure: bool,
max_age: Option<Duration>, max_age: Option<Duration>,
same_site: Option<SameSite>,
} }
impl CookieIdentityInner { impl CookieIdentityInner {
@ -248,6 +249,7 @@ impl CookieIdentityInner {
domain: None, domain: None,
secure: true, secure: true,
max_age: None, max_age: None,
same_site: None,
} }
} }
@ -268,6 +270,10 @@ impl CookieIdentityInner {
cookie.set_max_age(max_age); cookie.set_max_age(max_age);
} }
if let Some(same_site) = self.same_site {
cookie.set_same_site(same_site);
}
let mut jar = CookieJar::new(); let mut jar = CookieJar::new();
if some { if some {
jar.private(&self.key).add(cookie); jar.private(&self.key).add(cookie);
@ -370,6 +376,12 @@ impl CookieIdentityPolicy {
Rc::get_mut(&mut self.0).unwrap().max_age = Some(value); Rc::get_mut(&mut self.0).unwrap().max_age = Some(value);
self self
} }
/// Sets the `same_site` field in the session cookie being built.
pub fn same_site(mut self, same_site: SameSite) -> Self {
Rc::get_mut(&mut self.0).unwrap().same_site = Some(same_site);
self
}
} }
impl<S> IdentityPolicy<S> for CookieIdentityPolicy { impl<S> IdentityPolicy<S> for CookieIdentityPolicy {