sess_cookie.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package session
  15. import (
  16. "crypto/aes"
  17. "crypto/cipher"
  18. "encoding/json"
  19. "net/http"
  20. "net/url"
  21. "sync"
  22. )
  23. var cookiepder = &CookieProvider{}
  24. // CookieSessionStore Cookie SessionStore
  25. type CookieSessionStore struct {
  26. sid string
  27. values map[interface{}]interface{} // session data
  28. lock sync.RWMutex
  29. }
  30. // Set value to cookie session.
  31. // the value are encoded as gob with hash block string.
  32. func (st *CookieSessionStore) Set(key, value interface{}) error {
  33. st.lock.Lock()
  34. defer st.lock.Unlock()
  35. st.values[key] = value
  36. return nil
  37. }
  38. // Get value from cookie session
  39. func (st *CookieSessionStore) Get(key interface{}) interface{} {
  40. st.lock.RLock()
  41. defer st.lock.RUnlock()
  42. if v, ok := st.values[key]; ok {
  43. return v
  44. }
  45. return nil
  46. }
  47. // Delete value in cookie session
  48. func (st *CookieSessionStore) Delete(key interface{}) error {
  49. st.lock.Lock()
  50. defer st.lock.Unlock()
  51. delete(st.values, key)
  52. return nil
  53. }
  54. // Flush Clean all values in cookie session
  55. func (st *CookieSessionStore) Flush() error {
  56. st.lock.Lock()
  57. defer st.lock.Unlock()
  58. st.values = make(map[interface{}]interface{})
  59. return nil
  60. }
  61. // SessionID Return id of this cookie session
  62. func (st *CookieSessionStore) SessionID() string {
  63. return st.sid
  64. }
  65. // SessionRelease Write cookie session to http response cookie
  66. func (st *CookieSessionStore) SessionRelease(w http.ResponseWriter) {
  67. str, err := encodeCookie(cookiepder.block,
  68. cookiepder.config.SecurityKey,
  69. cookiepder.config.SecurityName,
  70. st.values)
  71. if err != nil {
  72. return
  73. }
  74. cookie := &http.Cookie{Name: cookiepder.config.CookieName,
  75. Value: url.QueryEscape(str),
  76. Path: "/",
  77. HttpOnly: true,
  78. Secure: cookiepder.config.Secure,
  79. MaxAge: cookiepder.config.Maxage}
  80. http.SetCookie(w, cookie)
  81. return
  82. }
  83. type cookieConfig struct {
  84. SecurityKey string `json:"securityKey"`
  85. BlockKey string `json:"blockKey"`
  86. SecurityName string `json:"securityName"`
  87. CookieName string `json:"cookieName"`
  88. Secure bool `json:"secure"`
  89. Maxage int `json:"maxage"`
  90. }
  91. // CookieProvider Cookie session provider
  92. type CookieProvider struct {
  93. maxlifetime int64
  94. config *cookieConfig
  95. block cipher.Block
  96. }
  97. // SessionInit Init cookie session provider with max lifetime and config json.
  98. // maxlifetime is ignored.
  99. // json config:
  100. // securityKey - hash string
  101. // blockKey - gob encode hash string. it's saved as aes crypto.
  102. // securityName - recognized name in encoded cookie string
  103. // cookieName - cookie name
  104. // maxage - cookie max life time.
  105. func (pder *CookieProvider) SessionInit(maxlifetime int64, config string) error {
  106. pder.config = &cookieConfig{}
  107. err := json.Unmarshal([]byte(config), pder.config)
  108. if err != nil {
  109. return err
  110. }
  111. if pder.config.BlockKey == "" {
  112. pder.config.BlockKey = string(generateRandomKey(16))
  113. }
  114. if pder.config.SecurityName == "" {
  115. pder.config.SecurityName = string(generateRandomKey(20))
  116. }
  117. pder.block, err = aes.NewCipher([]byte(pder.config.BlockKey))
  118. if err != nil {
  119. return err
  120. }
  121. pder.maxlifetime = maxlifetime
  122. return nil
  123. }
  124. // SessionRead Get SessionStore in cooke.
  125. // decode cooke string to map and put into SessionStore with sid.
  126. func (pder *CookieProvider) SessionRead(sid string) (Store, error) {
  127. maps, _ := decodeCookie(pder.block,
  128. pder.config.SecurityKey,
  129. pder.config.SecurityName,
  130. sid, pder.maxlifetime)
  131. if maps == nil {
  132. maps = make(map[interface{}]interface{})
  133. }
  134. rs := &CookieSessionStore{sid: sid, values: maps}
  135. return rs, nil
  136. }
  137. // SessionExist Cookie session is always existed
  138. func (pder *CookieProvider) SessionExist(sid string) bool {
  139. return true
  140. }
  141. // SessionRegenerate Implement method, no used.
  142. func (pder *CookieProvider) SessionRegenerate(oldsid, sid string) (Store, error) {
  143. return nil, nil
  144. }
  145. // SessionDestroy Implement method, no used.
  146. func (pder *CookieProvider) SessionDestroy(sid string) error {
  147. return nil
  148. }
  149. // SessionGC Implement method, no used.
  150. func (pder *CookieProvider) SessionGC() {
  151. return
  152. }
  153. // SessionAll Implement method, return 0.
  154. func (pder *CookieProvider) SessionAll() int {
  155. return 0
  156. }
  157. // SessionUpdate Implement method, no used.
  158. func (pder *CookieProvider) SessionUpdate(sid string) error {
  159. return nil
  160. }
  161. func init() {
  162. Register("cookie", cookiepder)
  163. }