Skip to content

Commit

Permalink
add SameSite as a cookie attribute option
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Feb 6, 2020
1 parent df7421c commit c03b8a2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
32 changes: 26 additions & 6 deletions unirest/src/main/java/kong/unirest/Cookie.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

/**
Expand All @@ -51,6 +49,7 @@ public class Cookie {
private Integer maxAge;
private ZonedDateTime expires;
private boolean secure;
private SameSite sameSite;

public Cookie(String name, String value){
this.name = name;
Expand Down Expand Up @@ -125,6 +124,9 @@ private void parseSection(String[] sub) {
secure = true;
break;
}
case "samesite": {
sameSite = SameSite.parse(sub[1]);
}
}
}

Expand Down Expand Up @@ -181,7 +183,6 @@ public void setHttpOnly(boolean httpOnly) {
this.httpOnly = httpOnly;
}


private static class Pair {
final String key;
final String value;
Expand Down Expand Up @@ -243,7 +244,7 @@ public boolean isHttpOnly() {
* Per Wikipedia:
* The Secure attribute is meant to keep cookie communication limited to encrypted transmission,
* directing browsers to use cookies only via secure/encrypted connections.
* @return
* @return a boolean of if the cookie is secure
*/
public boolean isSecure() {
return secure;
Expand All @@ -253,7 +254,7 @@ public boolean isSecure() {
* Per Wikipedia:
* the Max-Age attribute can be used to set the cookie's expiration as an interval of seconds in the future,
* relative to the time the browser received the cookie.
* @return
* @return Max-Age attribute
*/
public int getMaxAge() {
return maxAge;
Expand All @@ -268,5 +269,24 @@ public ZonedDateTime getExpiration() {
return expires;
}

/**
* returns the SameSite attribute
* @return the SameSite attribute if set. or null
*/
public SameSite getSameSite() {
return sameSite;
}

public enum SameSite {
None, Strict, Lax;

private static EnumSet<SameSite> all = EnumSet.allOf(SameSite.class);

public static SameSite parse(String value) {
return all.stream()
.filter(e -> e.name().equalsIgnoreCase(value))
.findFirst()
.orElse(null);
}
}
}
8 changes: 8 additions & 0 deletions unirest/src/test/java/kong/unirest/CookieParsingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public void parseFull() {
assertTrue(c.isHttpOnly());
assertFalse(c.isSecure());
assertEquals(42, c.getMaxAge());
assertNull(c.getSameSite());
}

@Test
Expand All @@ -62,6 +63,13 @@ public void parseBackOutToString() {
assertEquals(v, c.toString());
}

@Test
public void sameSite() {
String v = "color=blue;SameSite=Strict";
Cookie c = new Cookie(v);
assertEquals(Cookie.SameSite.Strict, c.getSameSite());
}

@Test
public void emptyValue() {
String v = "SignOnDefault=; domain=.admin.virginia.edu; path=/; HttpOnly";
Expand Down

0 comments on commit c03b8a2

Please sign in to comment.