session.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package session
  2. import (
  3. "math/rand"
  4. "time"
  5. ms "github.com/go-macaron/session"
  6. _ "github.com/go-macaron/session/memcache"
  7. _ "github.com/go-macaron/session/mysql"
  8. _ "github.com/go-macaron/session/postgres"
  9. _ "github.com/go-macaron/session/redis"
  10. "github.com/grafana/grafana/pkg/log"
  11. "gopkg.in/macaron.v1"
  12. )
  13. const (
  14. SESS_KEY_USERID = "uid"
  15. SESS_KEY_OAUTH_STATE = "state"
  16. SESS_KEY_APIKEY = "apikey_id" // used for render requests with api keys
  17. SESS_KEY_LASTLDAPSYNC = "last_ldap_sync"
  18. )
  19. var sessionManager *ms.Manager
  20. var sessionOptions *ms.Options
  21. var StartSessionGC func()
  22. var GetSessionCount func() int
  23. var sessionLogger = log.New("session")
  24. func init() {
  25. StartSessionGC = func() {
  26. sessionManager.GC()
  27. sessionLogger.Debug("Session GC")
  28. time.AfterFunc(time.Duration(sessionOptions.Gclifetime)*time.Second, StartSessionGC)
  29. }
  30. GetSessionCount = func() int {
  31. return sessionManager.Count()
  32. }
  33. }
  34. func Init(options *ms.Options) {
  35. var err error
  36. sessionOptions = prepareOptions(options)
  37. sessionManager, err = ms.NewManager(options.Provider, *options)
  38. if err != nil {
  39. panic(err)
  40. }
  41. // start GC threads after some random seconds
  42. rndSeconds := 10 + rand.Int63n(180)
  43. time.AfterFunc(time.Duration(rndSeconds)*time.Second, StartSessionGC)
  44. }
  45. func prepareOptions(opt *ms.Options) *ms.Options {
  46. if len(opt.Provider) == 0 {
  47. opt.Provider = "memory"
  48. }
  49. if len(opt.ProviderConfig) == 0 {
  50. opt.ProviderConfig = "data/sessions"
  51. }
  52. if len(opt.CookieName) == 0 {
  53. opt.CookieName = "grafana_sess"
  54. }
  55. if len(opt.CookiePath) == 0 {
  56. opt.CookiePath = "/"
  57. }
  58. if opt.Gclifetime == 0 {
  59. opt.Gclifetime = 3600
  60. }
  61. if opt.Maxlifetime == 0 {
  62. opt.Maxlifetime = opt.Gclifetime
  63. }
  64. if opt.IDLength == 0 {
  65. opt.IDLength = 16
  66. }
  67. return opt
  68. }
  69. func GetSession() SessionStore {
  70. return &SessionWrapper{manager: sessionManager}
  71. }
  72. type SessionStore interface {
  73. // Set sets value to given key in session.
  74. Set(interface{}, interface{}) error
  75. // Get gets value by given key in session.
  76. Get(interface{}) interface{}
  77. // Delete deletes a key from session.
  78. Delete(interface{}) interface{}
  79. // ID returns current session ID.
  80. ID() string
  81. // Release releases session resource and save data to provider.
  82. Release() error
  83. // Destory deletes a session.
  84. Destory(*macaron.Context) error
  85. // init
  86. Start(*macaron.Context) error
  87. // RegenerateId regenerates the session id
  88. RegenerateId(*macaron.Context) error
  89. }
  90. type SessionWrapper struct {
  91. session ms.RawStore
  92. manager *ms.Manager
  93. }
  94. func (s *SessionWrapper) Start(c *macaron.Context) error {
  95. // See https://github.com/grafana/grafana/issues/11155 for details on why
  96. // a recover and retry is needed
  97. defer func() error {
  98. if err := recover(); err != nil {
  99. var retryErr error
  100. s.session, retryErr = s.manager.Start(c)
  101. return retryErr
  102. }
  103. return nil
  104. }()
  105. var err error
  106. s.session, err = s.manager.Start(c)
  107. return err
  108. }
  109. func (s *SessionWrapper) RegenerateId(c *macaron.Context) error {
  110. var err error
  111. s.session, err = s.manager.RegenerateId(c)
  112. return err
  113. }
  114. func (s *SessionWrapper) Set(k interface{}, v interface{}) error {
  115. if s.session != nil {
  116. return s.session.Set(k, v)
  117. }
  118. return nil
  119. }
  120. func (s *SessionWrapper) Get(k interface{}) interface{} {
  121. if s.session != nil {
  122. return s.session.Get(k)
  123. }
  124. return nil
  125. }
  126. func (s *SessionWrapper) Delete(k interface{}) interface{} {
  127. if s.session != nil {
  128. return s.session.Delete(k)
  129. }
  130. return nil
  131. }
  132. func (s *SessionWrapper) ID() string {
  133. if s.session != nil {
  134. return s.session.ID()
  135. }
  136. return ""
  137. }
  138. func (s *SessionWrapper) Release() error {
  139. if s.session != nil {
  140. return s.session.Release()
  141. }
  142. return nil
  143. }
  144. func (s *SessionWrapper) Destory(c *macaron.Context) error {
  145. if s.session != nil {
  146. if err := s.manager.Destory(c); err != nil {
  147. return err
  148. }
  149. s.session = nil
  150. }
  151. return nil
  152. }