fix #247
Showing
1 changed file
with
39 additions
and
62 deletions
| ... | @@ -51,6 +51,8 @@ type Manager struct { | ... | @@ -51,6 +51,8 @@ type Manager struct { |
| 51 | maxlifetime int64 | 51 | maxlifetime int64 |
| 52 | hashfunc string //support md5 & sha1 | 52 | hashfunc string //support md5 & sha1 |
| 53 | hashkey string | 53 | hashkey string |
| 54 | maxage int | ||
| 55 | secure bool | ||
| 54 | options []interface{} | 56 | options []interface{} |
| 55 | } | 57 | } |
| 56 | 58 | ||
| ... | @@ -65,6 +67,10 @@ func NewManager(provideName, cookieName string, maxlifetime int64, savePath stri | ... | @@ -65,6 +67,10 @@ func NewManager(provideName, cookieName string, maxlifetime int64, savePath stri |
| 65 | return nil, fmt.Errorf("session: unknown provide %q (forgotten import?)", provideName) | 67 | return nil, fmt.Errorf("session: unknown provide %q (forgotten import?)", provideName) |
| 66 | } | 68 | } |
| 67 | provider.SessionInit(maxlifetime, savePath) | 69 | provider.SessionInit(maxlifetime, savePath) |
| 70 | secure := false | ||
| 71 | if len(options) > 0 { | ||
| 72 | secure = options[0].(bool) | ||
| 73 | } | ||
| 68 | hashfunc := "sha1" | 74 | hashfunc := "sha1" |
| 69 | if len(options) > 1 { | 75 | if len(options) > 1 { |
| 70 | hashfunc = options[1].(string) | 76 | hashfunc = options[1].(string) |
| ... | @@ -73,12 +79,37 @@ func NewManager(provideName, cookieName string, maxlifetime int64, savePath stri | ... | @@ -73,12 +79,37 @@ func NewManager(provideName, cookieName string, maxlifetime int64, savePath stri |
| 73 | if len(options) > 2 { | 79 | if len(options) > 2 { |
| 74 | hashkey = options[2].(string) | 80 | hashkey = options[2].(string) |
| 75 | } | 81 | } |
| 82 | maxage := -1 | ||
| 83 | if len(options) > 3 { | ||
| 84 | switch options[3].(type) { | ||
| 85 | case int: | ||
| 86 | if options[3].(int) > 0 { | ||
| 87 | maxage = options[3].(int) | ||
| 88 | } else if options[3].(int) < 0 { | ||
| 89 | maxage = 0 | ||
| 90 | } | ||
| 91 | case int64: | ||
| 92 | if options[3].(int64) > 0 { | ||
| 93 | maxage = int(options[3].(int64)) | ||
| 94 | } else if options[3].(int64) < 0 { | ||
| 95 | maxage = 0 | ||
| 96 | } | ||
| 97 | case int32: | ||
| 98 | if options[3].(int32) > 0 { | ||
| 99 | maxage = int(options[3].(int32)) | ||
| 100 | } else if options[3].(int32) < 0 { | ||
| 101 | maxage = 0 | ||
| 102 | } | ||
| 103 | } | ||
| 104 | } | ||
| 76 | return &Manager{ | 105 | return &Manager{ |
| 77 | provider: provider, | 106 | provider: provider, |
| 78 | cookieName: cookieName, | 107 | cookieName: cookieName, |
| 79 | maxlifetime: maxlifetime, | 108 | maxlifetime: maxlifetime, |
| 80 | hashfunc: hashfunc, | 109 | hashfunc: hashfunc, |
| 81 | hashkey: hashkey, | 110 | hashkey: hashkey, |
| 111 | maxage: maxage, | ||
| 112 | secure: secure, | ||
| 82 | options: options, | 113 | options: options, |
| 83 | }, nil | 114 | }, nil |
| 84 | } | 115 | } |
| ... | @@ -86,43 +117,16 @@ func NewManager(provideName, cookieName string, maxlifetime int64, savePath stri | ... | @@ -86,43 +117,16 @@ func NewManager(provideName, cookieName string, maxlifetime int64, savePath stri |
| 86 | //get Session | 117 | //get Session |
| 87 | func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (session SessionStore) { | 118 | func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (session SessionStore) { |
| 88 | cookie, err := r.Cookie(manager.cookieName) | 119 | cookie, err := r.Cookie(manager.cookieName) |
| 89 | maxage := -1 | ||
| 90 | if len(manager.options) > 3 { | ||
| 91 | switch manager.options[3].(type) { | ||
| 92 | case int: | ||
| 93 | if manager.options[3].(int) > 0 { | ||
| 94 | maxage = manager.options[3].(int) | ||
| 95 | } else if manager.options[3].(int) < 0 { | ||
| 96 | maxage = 0 | ||
| 97 | } | ||
| 98 | case int64: | ||
| 99 | if manager.options[3].(int64) > 0 { | ||
| 100 | maxage = int(manager.options[3].(int64)) | ||
| 101 | } else if manager.options[3].(int64) < 0 { | ||
| 102 | maxage = 0 | ||
| 103 | } | ||
| 104 | case int32: | ||
| 105 | if manager.options[3].(int32) > 0 { | ||
| 106 | maxage = int(manager.options[3].(int32)) | ||
| 107 | } else if manager.options[3].(int32) < 0 { | ||
| 108 | maxage = 0 | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | if err != nil || cookie.Value == "" { | 120 | if err != nil || cookie.Value == "" { |
| 113 | sid := manager.sessionId(r) | 121 | sid := manager.sessionId(r) |
| 114 | session, _ = manager.provider.SessionRead(sid) | 122 | session, _ = manager.provider.SessionRead(sid) |
| 115 | secure := false | ||
| 116 | if len(manager.options) > 0 { | ||
| 117 | secure = manager.options[0].(bool) | ||
| 118 | } | ||
| 119 | cookie = &http.Cookie{Name: manager.cookieName, | 123 | cookie = &http.Cookie{Name: manager.cookieName, |
| 120 | Value: url.QueryEscape(sid), | 124 | Value: url.QueryEscape(sid), |
| 121 | Path: "/", | 125 | Path: "/", |
| 122 | HttpOnly: true, | 126 | HttpOnly: true, |
| 123 | Secure: secure} | 127 | Secure: manager.secure} |
| 124 | if maxage >= 0 { | 128 | if manager.maxage >= 0 { |
| 125 | cookie.MaxAge = maxage | 129 | cookie.MaxAge = manager.maxage |
| 126 | } | 130 | } |
| 127 | //cookie.Expires = time.Now().Add(time.Duration(manager.maxlifetime) * time.Second) | 131 | //cookie.Expires = time.Now().Add(time.Duration(manager.maxlifetime) * time.Second) |
| 128 | http.SetCookie(w, cookie) | 132 | http.SetCookie(w, cookie) |
| ... | @@ -131,8 +135,8 @@ func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (se | ... | @@ -131,8 +135,8 @@ func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (se |
| 131 | //cookie.Expires = time.Now().Add(time.Duration(manager.maxlifetime) * time.Second) | 135 | //cookie.Expires = time.Now().Add(time.Duration(manager.maxlifetime) * time.Second) |
| 132 | cookie.HttpOnly = true | 136 | cookie.HttpOnly = true |
| 133 | cookie.Path = "/" | 137 | cookie.Path = "/" |
| 134 | if maxage >= 0 { | 138 | if manager.maxage >= 0 { |
| 135 | cookie.MaxAge = maxage | 139 | cookie.MaxAge = manager.maxage |
| 136 | } | 140 | } |
| 137 | http.SetCookie(w, cookie) | 141 | http.SetCookie(w, cookie) |
| 138 | sid, _ := url.QueryUnescape(cookie.Value) | 142 | sid, _ := url.QueryUnescape(cookie.Value) |
| ... | @@ -165,15 +169,11 @@ func (manager *Manager) SessionRegenerateId(w http.ResponseWriter, r *http.Reque | ... | @@ -165,15 +169,11 @@ func (manager *Manager) SessionRegenerateId(w http.ResponseWriter, r *http.Reque |
| 165 | if err != nil && cookie.Value == "" { | 169 | if err != nil && cookie.Value == "" { |
| 166 | //delete old cookie | 170 | //delete old cookie |
| 167 | session, _ = manager.provider.SessionRead(sid) | 171 | session, _ = manager.provider.SessionRead(sid) |
| 168 | secure := false | ||
| 169 | if len(manager.options) > 0 { | ||
| 170 | secure = manager.options[0].(bool) | ||
| 171 | } | ||
| 172 | cookie = &http.Cookie{Name: manager.cookieName, | 172 | cookie = &http.Cookie{Name: manager.cookieName, |
| 173 | Value: url.QueryEscape(sid), | 173 | Value: url.QueryEscape(sid), |
| 174 | Path: "/", | 174 | Path: "/", |
| 175 | HttpOnly: true, | 175 | HttpOnly: true, |
| 176 | Secure: secure, | 176 | Secure: manager.secure, |
| 177 | } | 177 | } |
| 178 | } else { | 178 | } else { |
| 179 | oldsid, _ := url.QueryUnescape(cookie.Value) | 179 | oldsid, _ := url.QueryUnescape(cookie.Value) |
| ... | @@ -182,31 +182,8 @@ func (manager *Manager) SessionRegenerateId(w http.ResponseWriter, r *http.Reque | ... | @@ -182,31 +182,8 @@ func (manager *Manager) SessionRegenerateId(w http.ResponseWriter, r *http.Reque |
| 182 | cookie.HttpOnly = true | 182 | cookie.HttpOnly = true |
| 183 | cookie.Path = "/" | 183 | cookie.Path = "/" |
| 184 | } | 184 | } |
| 185 | maxage := -1 | 185 | if manager.maxage >= 0 { |
| 186 | if len(manager.options) > 3 { | 186 | cookie.MaxAge = manager.maxage |
| 187 | switch manager.options[3].(type) { | ||
| 188 | case int: | ||
| 189 | if manager.options[3].(int) > 0 { | ||
| 190 | maxage = manager.options[3].(int) | ||
| 191 | } else if manager.options[3].(int) < 0 { | ||
| 192 | maxage = 0 | ||
| 193 | } | ||
| 194 | case int64: | ||
| 195 | if manager.options[3].(int64) > 0 { | ||
| 196 | maxage = int(manager.options[3].(int64)) | ||
| 197 | } else if manager.options[3].(int64) < 0 { | ||
| 198 | maxage = 0 | ||
| 199 | } | ||
| 200 | case int32: | ||
| 201 | if manager.options[3].(int32) > 0 { | ||
| 202 | maxage = int(manager.options[3].(int32)) | ||
| 203 | } else if manager.options[3].(int32) < 0 { | ||
| 204 | maxage = 0 | ||
| 205 | } | ||
| 206 | } | ||
| 207 | } | ||
| 208 | if maxage >= 0 { | ||
| 209 | cookie.MaxAge = maxage | ||
| 210 | } | 187 | } |
| 211 | http.SetCookie(w, cookie) | 188 | http.SetCookie(w, cookie) |
| 212 | r.AddCookie(cookie) | 189 | r.AddCookie(cookie) | ... | ... |
-
Please register or sign in to post a comment