cookie_store.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package sessions
  2. import (
  3. "github.com/gorilla/sessions"
  4. )
  5. // CookieStore is an interface that represents a Cookie based storage
  6. // for Sessions.
  7. type CookieStore interface {
  8. // Store is an embedded interface so that CookieStore can be used
  9. // as a session store.
  10. Store
  11. // Options sets the default options for each session stored in this
  12. // CookieStore.
  13. Options(Options)
  14. }
  15. // NewCookieStore returns a new CookieStore.
  16. //
  17. // Keys are defined in pairs to allow key rotation, but the common case is to set a single
  18. // authentication key and optionally an encryption key.
  19. //
  20. // The first key in a pair is used for authentication and the second for encryption. The
  21. // encryption key can be set to nil or omitted in the last pair, but the authentication key
  22. // is required in all pairs.
  23. //
  24. // It is recommended to use an authentication key with 32 or 64 bytes. The encryption key,
  25. // if set, must be either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256 modes.
  26. func NewCookieStore(keyPairs ...[]byte) CookieStore {
  27. return &cookieStore{sessions.NewCookieStore(keyPairs...)}
  28. }
  29. type cookieStore struct {
  30. *sessions.CookieStore
  31. }
  32. func (c *cookieStore) Options(options Options) {
  33. c.CookieStore.Options = &sessions.Options{
  34. Path: options.Path,
  35. Domain: options.Domain,
  36. MaxAge: options.MaxAge,
  37. Secure: options.Secure,
  38. HttpOnly: options.HttpOnly,
  39. }
  40. }