config.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package sessions
  2. import (
  3. "time"
  4. "github.com/kataras/iris/context"
  5. "github.com/google/uuid"
  6. )
  7. const (
  8. // DefaultCookieName the secret cookie's name for sessions
  9. DefaultCookieName = "irissessionid"
  10. )
  11. type (
  12. // Config is the configuration for sessions. Please read it before using sessions.
  13. Config struct {
  14. // Cookie string, the session's client cookie name, for example: "mysessionid"
  15. //
  16. // Defaults to "irissessionid".
  17. Cookie string
  18. // CookieSecureTLS set to true if server is running over TLS
  19. // and you need the session's cookie "Secure" field to be set true.
  20. // Defaults to false.
  21. CookieSecureTLS bool
  22. // AllowReclaim will allow to
  23. // Destroy and Start a session in the same request handler.
  24. // All it does is that it removes the cookie for both `Request` and `ResponseWriter` while `Destroy`
  25. // or add a new cookie to `Request` while `Start`.
  26. //
  27. // Defaults to false.
  28. AllowReclaim bool
  29. // Encoding should encodes and decodes
  30. // authenticated and optionally encrypted cookie values.
  31. //
  32. // Defaults to nil.
  33. Encoding context.SecureCookie
  34. // Expires the duration of which the cookie must expires (created_time.Add(Expires)).
  35. // If you want to delete the cookie when the browser closes, set it to -1.
  36. //
  37. // 0 means no expire, (24 years)
  38. // -1 means when browser closes
  39. // > 0 is the time.Duration which the session cookies should expire.
  40. //
  41. // Defaults to infinitive/unlimited life duration(0).
  42. Expires time.Duration
  43. // SessionIDGenerator can be set to a function which
  44. // return a unique session id.
  45. // By default we will use a uuid impl package to generate
  46. // that, but developers can change that with simple assignment.
  47. SessionIDGenerator func(ctx *context.Context) string
  48. // DisableSubdomainPersistence set it to true in order dissallow your subdomains to have access to the session cookie
  49. //
  50. // Defaults to false.
  51. DisableSubdomainPersistence bool
  52. }
  53. )
  54. // Validate corrects missing fields configuration fields and returns the right configuration
  55. func (c Config) Validate() Config {
  56. if c.Cookie == "" {
  57. c.Cookie = DefaultCookieName
  58. }
  59. if c.SessionIDGenerator == nil {
  60. c.SessionIDGenerator = func(*context.Context) string {
  61. id, _ := uuid.NewRandom()
  62. return id.String()
  63. }
  64. }
  65. return c
  66. }