cookie.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package cookie // import "miniflux.app/v2/internal/http/cookie"
  4. import (
  5. "net/http"
  6. "time"
  7. )
  8. // Cookie names.
  9. const (
  10. CookieAppSessionID = "MinifluxAppSessionID"
  11. CookieUserSessionID = "MinifluxUserSessionID"
  12. // Cookie duration in days.
  13. cookieDuration = 30
  14. )
  15. // New creates a new cookie.
  16. func New(name, value string, isHTTPS bool, path string) *http.Cookie {
  17. return &http.Cookie{
  18. Name: name,
  19. Value: value,
  20. Path: basePath(path),
  21. Secure: isHTTPS,
  22. HttpOnly: true,
  23. Expires: time.Now().Add(cookieDuration * 24 * time.Hour),
  24. SameSite: http.SameSiteLaxMode,
  25. }
  26. }
  27. // Expired returns an expired cookie.
  28. func Expired(name string, isHTTPS bool, path string) *http.Cookie {
  29. return &http.Cookie{
  30. Name: name,
  31. Value: "",
  32. Path: basePath(path),
  33. Secure: isHTTPS,
  34. HttpOnly: true,
  35. MaxAge: -1,
  36. Expires: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  37. SameSite: http.SameSiteLaxMode,
  38. }
  39. }
  40. func basePath(path string) string {
  41. if path == "" {
  42. return "/"
  43. }
  44. return path
  45. }