cookie.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. "miniflux.app/v2/internal/config"
  8. )
  9. // Cookie names.
  10. const (
  11. CookieAppSessionID = "MinifluxAppSessionID"
  12. CookieUserSessionID = "MinifluxUserSessionID"
  13. )
  14. // New creates a new cookie.
  15. func New(name, value string, isHTTPS bool, path string) *http.Cookie {
  16. return &http.Cookie{
  17. Name: name,
  18. Value: value,
  19. Path: basePath(path),
  20. Secure: isHTTPS,
  21. HttpOnly: true,
  22. Expires: time.Now().Add(config.Opts.CleanupRemoveSessionsInterval()),
  23. SameSite: http.SameSiteLaxMode,
  24. }
  25. }
  26. // Expired returns an expired cookie.
  27. func Expired(name string, isHTTPS bool, path string) *http.Cookie {
  28. return &http.Cookie{
  29. Name: name,
  30. Value: "",
  31. Path: basePath(path),
  32. Secure: isHTTPS,
  33. HttpOnly: true,
  34. MaxAge: -1,
  35. Expires: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  36. SameSite: http.SameSiteLaxMode,
  37. }
  38. }
  39. func basePath(path string) string {
  40. if path == "" {
  41. return "/"
  42. }
  43. return path
  44. }