config.go 2.8 KB

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