cookie.go 920 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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
  5. import (
  6. "net/http"
  7. "time"
  8. )
  9. // Cookie names.
  10. const (
  11. CookieSessionID = "sessionID"
  12. CookieUserSessionID = "userSessionID"
  13. // Cookie duration in days.
  14. cookieDuration = 30
  15. )
  16. // New creates a new cookie.
  17. func New(name, value string, isHTTPS bool) *http.Cookie {
  18. return &http.Cookie{
  19. Name: name,
  20. Value: value,
  21. Path: "/",
  22. Secure: isHTTPS,
  23. HttpOnly: true,
  24. Expires: time.Now().Add(cookieDuration * 24 * time.Hour),
  25. }
  26. }
  27. // Expired returns an expired cookie.
  28. func Expired(name string, isHTTPS bool) *http.Cookie {
  29. return &http.Cookie{
  30. Name: name,
  31. Value: "",
  32. Path: "/",
  33. Secure: isHTTPS,
  34. HttpOnly: true,
  35. MaxAge: -1,
  36. Expires: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  37. }
  38. }