Skip to content

Commit

Permalink
feat: add partitioned method to Cookie for checking Partitioned s…
Browse files Browse the repository at this point in the history
…tatus (#975)
  • Loading branch information
Cinea4678 authored Feb 23, 2025
1 parent a9069f2 commit 099ba43
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
12 changes: 12 additions & 0 deletions poem/src/session/cookie_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct CookieConfig {
http_only: bool,
max_age: Option<Duration>,
same_site: Option<SameSite>,
partitioned: bool,
}

impl Default for CookieConfig {
Expand All @@ -39,6 +40,7 @@ impl Default for CookieConfig {
http_only: true,
max_age: None,
same_site: None,
partitioned: false,
}
}
}
Expand Down Expand Up @@ -119,6 +121,15 @@ impl CookieConfig {
}
}

/// Sets the `Partitioned` to the session cookie. Default is `false`.
#[must_use]
pub fn partitioned(self, value: bool) -> Self {
Self {
partitioned: value,
..self
}
}

/// Sets the `MaxAge` to the session cookie.
#[must_use]
pub fn max_age(self, value: impl Into<Option<Duration>>) -> Self {
Expand Down Expand Up @@ -152,6 +163,7 @@ impl CookieConfig {
}

cookie.set_same_site(self.same_site);
cookie.set_partitioned(self.partitioned);

match &self.security {
CookieSecurity::Plain => cookie_jar.add(cookie),
Expand Down
19 changes: 19 additions & 0 deletions poem/src/web/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,20 @@ impl Cookie {
self.0.secure().unwrap_or_default()
}

/// Returns whether this cookie was marked `Partitioned` or not.
///
/// # Example
///
/// ```rust
/// use poem::web::cookie::Cookie;
///
/// let cookie = Cookie::parse("foo=bar; Partitioned").unwrap();
/// assert!(cookie.partitioned());
/// ```
pub fn partitioned(&self) -> bool {
self.0.partitioned().unwrap_or_default()
}

/// Sets the `domain` of `self` to `domain`.
pub fn set_domain(&mut self, domain: impl Into<String>) {
self.0.set_domain(domain.into());
Expand Down Expand Up @@ -278,6 +292,11 @@ impl Cookie {
self.0.set_secure(value);
}

/// Sets the value of `Partitioned` in `self` to `value`.
pub fn set_partitioned(&mut self, value: impl Into<Option<bool>>) {
self.0.set_partitioned(value);
}

/// Sets the value of `self` to `value`.
pub fn set_value_str(&mut self, value: impl Into<String>) {
self.0.set_value(value.into());
Expand Down

0 comments on commit 099ba43

Please sign in to comment.