Cookie.kt

  1. package com.hexagonkt.http.model

  2. import java.time.Instant

  3. /**
  4.  * TODO .
  5.  *
  6.  * @property name
  7.  * @property value
  8.  * @property maxAge '-1' is the same as empty
  9.  * @property secure
  10.  * @property path '/' is the same as empty
  11.  * @property httpOnly
  12.  * @property domain
  13.  * @property sameSite
  14.  * @property expires
  15.  */
  16. data class Cookie(
  17.     val name: String,
  18.     val value: String = "",
  19.     val maxAge: Long = -1,
  20.     val secure: Boolean = false,
  21.     val path: String = "/",
  22.     val httpOnly: Boolean = false,
  23.     val domain: String? = null,
  24.     val sameSite: CookieSameSite? = null,
  25.     val expires: Instant? = null,
  26. ) {
  27.     val deleted: Boolean by lazy { value == "" && maxAge <= 0L }

  28.     init {
  29.         require(name.isNotBlank()) { "Cookie name can not be blank: $name" }
  30.     }

  31.     fun delete(): Cookie =
  32.         copy(value = "", maxAge = 0)
  33. }