Skip to content

Commit

Permalink
Add SameSite to DiscardingCookie
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurz committed Feb 8, 2021
1 parent f6917cb commit d66e142
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
23 changes: 22 additions & 1 deletion core/play/src/main/java/play/mvc/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,29 @@ public Result discardingCookie(String name, String path, String domain) {
* @param secure Whether the cookie to discard is secure
*/
public Result discardingCookie(String name, String path, String domain, boolean secure) {
return discardingCookie(name, path, domain, secure, null);
}

/**
* Discard a cookie in this result
*
* @param name The name of the cookie to discard, must not be null
* @param path The path of the cookie te discard, may be null
* @param domain The domain of the cookie to discard, may be null
* @param secure Whether the cookie to discard is secure
* @param sameSite The SameSite attribute of the cookie to discard, may be null
*/
public Result discardingCookie(
String name, String path, String domain, boolean secure, Cookie.SameSite sameSite) {
return withCookies(
new DiscardingCookie(name, path, Option.apply(domain), secure).toCookie().asJava());
new DiscardingCookie(
name,
path,
Option.apply(domain),
secure,
Option.apply(sameSite).map(ss -> ss.asScala()))
.toCookie()
.asJava());
}

/**
Expand Down
12 changes: 9 additions & 3 deletions core/play/src/main/scala/play/api/mvc/Cookie.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,14 @@ object Cookie {
* @param domain the cookie domain
* @param secure whether this cookie is secured
*/
case class DiscardingCookie(name: String, path: String = "/", domain: Option[String] = None, secure: Boolean = false) {
def toCookie = Cookie(name, "", Some(Cookie.DiscardedMaxAge), path, domain, secure, false)
case class DiscardingCookie(
name: String,
path: String = "/",
domain: Option[String] = None,
secure: Boolean = false,
sameSite: Option[SameSite] = None
) {
def toCookie = Cookie(name, "", Some(Cookie.DiscardedMaxAge), path, domain, secure, false, sameSite)
}

/**
Expand Down Expand Up @@ -487,7 +493,7 @@ trait CookieBaker[T <: AnyRef] { self: CookieDataCodec =>
}
}

def discard = DiscardingCookie(COOKIE_NAME, path, domain, secure)
def discard = DiscardingCookie(COOKIE_NAME, path, domain, secure, sameSite)

/**
* Builds the cookie object from the given data map.
Expand Down

0 comments on commit d66e142

Please sign in to comment.