config.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package sessions
  2. import (
  3. "time"
  4. "github.com/iris-contrib/go.uuid"
  5. )
  6. const (
  7. // DefaultCookieName the secret cookie's name for sessions
  8. DefaultCookieName = "irissessionid"
  9. )
  10. // Encoding is the Cookie Encoder/Decoder interface, which can be passed as configuration field
  11. // alternatively to the `Encode` and `Decode` fields.
  12. type Encoding interface {
  13. // Encode the cookie value if not nil.
  14. // Should accept as first argument the cookie name (config.Name)
  15. // as second argument the server's generated session id.
  16. // Should return the new session id, if error the session id setted to empty which is invalid.
  17. //
  18. // Note: Errors are not printed, so you have to know what you're doing,
  19. // and remember: if you use AES it only supports key sizes of 16, 24 or 32 bytes.
  20. // You either need to provide exactly that amount or you derive the key from what you type in.
  21. //
  22. // Defaults to nil
  23. Encode(cookieName string, value interface{}) (string, error)
  24. // Decode the cookie value if not nil.
  25. // Should accept as first argument the cookie name (config.Name)
  26. // as second second accepts the client's cookie value (the encoded session id).
  27. // Should return an error if decode operation failed.
  28. //
  29. // Note: Errors are not printed, so you have to know what you're doing,
  30. // and remember: if you use AES it only supports key sizes of 16, 24 or 32 bytes.
  31. // You either need to provide exactly that amount or you derive the key from what you type in.
  32. //
  33. // Defaults to nil
  34. Decode(cookieName string, cookieValue string, v interface{}) error
  35. }
  36. type (
  37. // Config is the configuration for sessions. Please read it before using sessions.
  38. Config struct {
  39. // Cookie string, the session's client cookie name, for example: "mysessionid"
  40. //
  41. // Defaults to "irissessionid".
  42. Cookie string
  43. // CookieSecureTLS set to true if server is running over TLS
  44. // and you need the session's cookie "Secure" field to be setted true.
  45. //
  46. // Note: The user should fill the Decode configuation field in order for this to work.
  47. // Recommendation: You don't need this to be setted to true, just fill the Encode and Decode fields
  48. // with a third-party library like secure cookie, example is provided at the _examples folder.
  49. //
  50. // Defaults to false.
  51. CookieSecureTLS bool
  52. // AllowReclaim will allow to
  53. // Destroy and Start a session in the same request handler.
  54. // All it does is that it removes the cookie for both `Request` and `ResponseWriter` while `Destroy`
  55. // or add a new cookie to `Request` while `Start`.
  56. //
  57. // Defaults to false.
  58. AllowReclaim bool
  59. // Encode the cookie value if not nil.
  60. // Should accept as first argument the cookie name (config.Cookie)
  61. // as second argument the server's generated session id.
  62. // Should return the new session id, if error the session id setted to empty which is invalid.
  63. //
  64. // Note: Errors are not printed, so you have to know what you're doing,
  65. // and remember: if you use AES it only supports key sizes of 16, 24 or 32 bytes.
  66. // You either need to provide exactly that amount or you derive the key from what you type in.
  67. //
  68. // Defaults to nil.
  69. Encode func(cookieName string, value interface{}) (string, error)
  70. // Decode the cookie value if not nil.
  71. // Should accept as first argument the cookie name (config.Cookie)
  72. // as second second accepts the client's cookie value (the encoded session id).
  73. // Should return an error if decode operation failed.
  74. //
  75. // Note: Errors are not printed, so you have to know what you're doing,
  76. // and remember: if you use AES it only supports key sizes of 16, 24 or 32 bytes.
  77. // You either need to provide exactly that amount or you derive the key from what you type in.
  78. //
  79. // Defaults to nil.
  80. Decode func(cookieName string, cookieValue string, v interface{}) error
  81. // Encoding same as Encode and Decode but receives a single instance which
  82. // completes the "CookieEncoder" interface, `Encode` and `Decode` functions.
  83. //
  84. // Defaults to nil.
  85. Encoding Encoding
  86. // Expires the duration of which the cookie must expires (created_time.Add(Expires)).
  87. // If you want to delete the cookie when the browser closes, set it to -1.
  88. //
  89. // 0 means no expire, (24 years)
  90. // -1 means when browser closes
  91. // > 0 is the time.Duration which the session cookies should expire.
  92. //
  93. // Defaults to infinitive/unlimited life duration(0).
  94. Expires time.Duration
  95. // SessionIDGenerator should returns a random session id.
  96. // By default we will use a uuid impl package to generate
  97. // that, but developers can change that with simple assignment.
  98. SessionIDGenerator func() string
  99. // DisableSubdomainPersistence set it to true in order dissallow your subdomains to have access to the session cookie
  100. //
  101. // Defaults to false.
  102. DisableSubdomainPersistence bool
  103. }
  104. )
  105. // Validate corrects missing fields configuration fields and returns the right configuration
  106. func (c Config) Validate() Config {
  107. if c.Cookie == "" {
  108. c.Cookie = DefaultCookieName
  109. }
  110. if c.SessionIDGenerator == nil {
  111. c.SessionIDGenerator = func() string {
  112. id, _ := uuid.NewV4()
  113. return id.String()
  114. }
  115. }
  116. if c.Encoding != nil {
  117. c.Encode = c.Encoding.Encode
  118. c.Decode = c.Encoding.Decode
  119. }
  120. return c
  121. }