d2eece9a by astaxie

session: #620 make the session never read empty

1 parent c3a23b28
...@@ -20,6 +20,8 @@ import ( ...@@ -20,6 +20,8 @@ import (
20 "io" 20 "io"
21 "strconv" 21 "strconv"
22 "time" 22 "time"
23
24 "github.com/astaxie/beego/utils"
23 ) 25 )
24 26
25 func init() { 27 func init() {
...@@ -60,8 +62,8 @@ func DecodeGob(encoded []byte) (map[interface{}]interface{}, error) { ...@@ -60,8 +62,8 @@ func DecodeGob(encoded []byte) (map[interface{}]interface{}, error) {
60 // generateRandomKey creates a random key with the given strength. 62 // generateRandomKey creates a random key with the given strength.
61 func generateRandomKey(strength int) []byte { 63 func generateRandomKey(strength int) []byte {
62 k := make([]byte, strength) 64 k := make([]byte, strength)
63 if _, err := io.ReadFull(rand.Reader, k); err != nil { 65 if n, err := io.ReadFull(rand.Reader, k); n != strength || err != nil {
64 return nil 66 return utils.RandomCreateBytes(strength)
65 } 67 }
66 return k 68 return k
67 } 69 }
......
...@@ -18,6 +18,8 @@ import ( ...@@ -18,6 +18,8 @@ import (
18 "net/http" 18 "net/http"
19 "net/url" 19 "net/url"
20 "time" 20 "time"
21
22 "github.com/astaxie/beego/utils"
21 ) 23 )
22 24
23 // SessionStore contains all data for one session process with specific id. 25 // SessionStore contains all data for one session process with specific id.
...@@ -237,9 +239,9 @@ func (manager *Manager) SetSecure(secure bool) { ...@@ -237,9 +239,9 @@ func (manager *Manager) SetSecure(secure bool) {
237 239
238 // generate session id with rand string, unix nano time, remote addr by hash function. 240 // generate session id with rand string, unix nano time, remote addr by hash function.
239 func (manager *Manager) sessionId(r *http.Request) (sid string) { 241 func (manager *Manager) sessionId(r *http.Request) (sid string) {
240 bs := make([]byte, 24) 242 bs := make([]byte, 32)
241 if _, err := io.ReadFull(rand.Reader, bs); err != nil { 243 if n, err := io.ReadFull(rand.Reader, bs); n != 32 || err != nil {
242 return "" 244 bs = utils.RandomCreateBytes(32)
243 } 245 }
244 sig := fmt.Sprintf("%s%d%s", r.RemoteAddr, time.Now().UnixNano(), bs) 246 sig := fmt.Sprintf("%s%d%s", r.RemoteAddr, time.Now().UnixNano(), bs)
245 if manager.config.SessionIDHashFunc == "md5" { 247 if manager.config.SessionIDHashFunc == "md5" {
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!