Class SetCookie

java.lang.Object
com.jsgalactic.axiom.security.SetCookie

public final class SetCookie extends Object
A Set-Cookie header with secure defaults, validated when it is created.

 var cookie = SetCookie.of("__Host-theme", "dark");           // Secure; HttpOnly; SameSite=Lax; Path=/
 return response.withHeader("Set-Cookie", cookie.header());
 

Defaults. Secure, HttpOnly, SameSite=Lax, Path=/, no Domain (so the cookie is host-only and no sub-domain receives it), no Max-Age (a browser-session cookie).

Rules enforced. The name is an HTTP token and the value consists of cookie octets; name plus value is at most 4,096 bytes. A name starting with __Host- (compared ignoring case, as browsers do) requires Secure, Path=/ and no Domain; __Secure- requires Secure. SameSite=None requires Secure. A Domain is a lower-case DNS name (no leading dot, not an IP address); a Path starts with / and has no control characters or semicolons; Max-Age is 0 (delete) to 400 days. Violations throw IllegalArgumentException.

One response can carry one Set-Cookie in Axiom's header model; addTo(com.jsgalactic.axiom.http.Response) refuses to replace another.

Immutable and thread-safe. toString() omits the value.

  • Method Details

    • of

      public static SetCookie of(String name, String value)
      Creates a cookie with the secure defaults.
      Parameters:
      name - cookie name
      value - cookie value, possibly empty
      Returns:
      the cookie
      Throws:
      IllegalArgumentException - if a rule above is violated
    • withValue

      public SetCookie withValue(String value)
      Returns a copy with another value.
      Parameters:
      value - cookie value
      Returns:
      the copy
    • secure

      public SetCookie secure(boolean secure)
      Returns a copy with the Secure attribute set or cleared. Browsers drop Secure cookies received over plain HTTP (except on localhost), so clear it only for local development.
      Parameters:
      secure - whether the cookie is Secure
      Returns:
      the copy
    • httpOnly

      public SetCookie httpOnly(boolean httpOnly)
      Returns a copy with HttpOnly set or cleared.
      Parameters:
      httpOnly - whether scripts are denied access
      Returns:
      the copy
    • sameSite

      public SetCookie sameSite(SetCookie.SameSite sameSite)
      Returns a copy with another SameSite attribute.
      Parameters:
      sameSite - the attribute
      Returns:
      the copy
    • path

      public SetCookie path(String path)
      Returns a copy with another Path.
      Parameters:
      path - path starting with /
      Returns:
      the copy
    • domain

      public SetCookie domain(String domain)
      Returns a copy with a Domain, which also sends the cookie to every sub-domain.
      Parameters:
      domain - lower-case DNS name without a leading dot, or null for a host-only cookie
      Returns:
      the copy
    • maxAge

      public SetCookie maxAge(Duration maxAge)
      Returns a copy with a Max-Age, in whole seconds (rounded down).
      Parameters:
      maxAge - lifetime from 0 to 400 days, or null for a browser-session cookie
      Returns:
      the copy
    • expire

      public SetCookie expire()
      Returns a copy that tells the browser to delete the cookie (empty value, Max-Age=0); name, path and domain must match the cookie being deleted.
      Returns:
      the deleting cookie
    • name

      public String name()
      Returns the cookie name.
      Returns:
      the name
    • value

      public String value()
      Returns the cookie value.
      Returns:
      the value, a credential for session and token cookies
    • isSecure

      public boolean isSecure()
      Returns whether the cookie is Secure.
      Returns:
      the attribute
    • isHttpOnly

      public boolean isHttpOnly()
      Returns whether the cookie is HttpOnly.
      Returns:
      the attribute
    • header

      public String header()
      Returns the header value, for example sid=abc; Path=/; Secure; HttpOnly; SameSite=Lax.
      Returns:
      the Set-Cookie value
    • addTo

      public Response addTo(Response response)
      Returns the response with this cookie as its Set-Cookie.
      Parameters:
      response - the response
      Returns:
      a copy carrying the header
      Throws:
      IllegalStateException - if the response already has a Set-Cookie
    • toString

      public String toString()
      Overrides:
      toString in class Object