cookie.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. cookie := &http.Cookie{
  17. Name: name,
  18. Value: value,
  19. Path: basePath(path),
  20. Secure: isHTTPS,
  21. HttpOnly: true,
  22. Expires: time.Now().Add(time.Duration(config.Opts.CleanupRemoveSessionsDays()) * 24 * time.Hour),
  23. SameSite: http.SameSiteStrictMode,
  24. }
  25. // OAuth doesn't work when cookies are in strict mode.
  26. if config.Opts.OAuth2Provider() != "" {
  27. cookie.SameSite = http.SameSiteLaxMode
  28. }
  29. return cookie
  30. }
  31. // Expired returns an expired cookie.
  32. func Expired(name string, isHTTPS bool, path string) *http.Cookie {
  33. cookie := &http.Cookie{
  34. Name: name,
  35. Value: "",
  36. Path: basePath(path),
  37. Secure: isHTTPS,
  38. HttpOnly: true,
  39. MaxAge: -1,
  40. Expires: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  41. SameSite: http.SameSiteStrictMode,
  42. }
  43. // OAuth doesn't work when cookies are in strict mode.
  44. if config.Opts.OAuth2Provider() != "" {
  45. cookie.SameSite = http.SameSiteLaxMode
  46. }
  47. return cookie
  48. }
  49. func basePath(path string) string {
  50. if path == "" {
  51. return "/"
  52. }
  53. return path
  54. }