cookie.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package cookie // import "miniflux.app/http/cookie"
  5. import (
  6. "net/http"
  7. "time"
  8. )
  9. // Cookie names.
  10. const (
  11. CookieAppSessionID = "MinifluxAppSessionID"
  12. CookieUserSessionID = "MinifluxUserSessionID"
  13. // Cookie duration in days.
  14. cookieDuration = 30
  15. )
  16. // New creates a new cookie.
  17. func New(name, value string, isHTTPS bool, path string) *http.Cookie {
  18. return &http.Cookie{
  19. Name: name,
  20. Value: value,
  21. Path: basePath(path),
  22. Secure: isHTTPS,
  23. HttpOnly: true,
  24. Expires: time.Now().Add(cookieDuration * 24 * time.Hour),
  25. SameSite: http.SameSiteLaxMode,
  26. }
  27. }
  28. // Expired returns an expired cookie.
  29. func Expired(name string, isHTTPS bool, path string) *http.Cookie {
  30. return &http.Cookie{
  31. Name: name,
  32. Value: "",
  33. Path: basePath(path),
  34. Secure: isHTTPS,
  35. HttpOnly: true,
  36. MaxAge: -1,
  37. Expires: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  38. SameSite: http.SameSiteLaxMode,
  39. }
  40. }
  41. func basePath(path string) string {
  42. if path == "" {
  43. return "/"
  44. }
  45. return path
  46. }