This commit is contained in:
Makis Maropoulos 2016-06-30 14:26:42 +03:00
parent 12d24f715d
commit 781abb00ea
2 changed files with 34 additions and 38 deletions

View File

@ -713,15 +713,8 @@ func (ctx *Context) SetCookieKV(key, value string) {
// RemoveCookie deletes a cookie by it's name/key // RemoveCookie deletes a cookie by it's name/key
func (ctx *Context) RemoveCookie(name string) { func (ctx *Context) RemoveCookie(name string) {
cookie := fasthttp.AcquireCookie() ctx.RequestCtx.Request.Header.DelCookie(name)
cookie.SetKey(name) ctx.RequestCtx.Response.Header.DelClientCookie(name)
cookie.SetValue("")
cookie.SetPath("/")
cookie.SetHTTPOnly(true)
exp := time.Now().Add(-time.Duration(1) * time.Minute) //RFC says 1 second, but make sure 1 minute because we are using fasthttp
cookie.SetExpire(exp)
ctx.Response.Header.SetCookie(cookie)
fasthttp.ReleaseCookie(cookie)
} }
// GetFlash get a flash message by it's key // GetFlash get a flash message by it's key

View File

@ -26,7 +26,6 @@ type (
config *config.Sessions config *config.Sessions
provider IProvider provider IProvider
mu sync.Mutex mu sync.Mutex
compiledCookie string
} }
) )
@ -47,7 +46,7 @@ func newManager(c config.Sessions) (*Manager, error) {
manager := &Manager{} manager := &Manager{}
manager.config = &c manager.config = &c
manager.provider = provider manager.provider = provider
manager.compiledCookie = base64.URLEncoding.EncodeToString([]byte(c.Cookie)) manager.config.Cookie = base64.URLEncoding.EncodeToString([]byte(c.Cookie)) // change the cookie's name/key to a more safe
return manager, nil return manager, nil
} }
@ -83,19 +82,20 @@ func (m *Manager) Start(ctx context.IContext) store.IStore {
m.mu.Lock() m.mu.Lock()
var store store.IStore var store store.IStore
requestCtx := ctx.GetRequestCtx() requestCtx := ctx.GetRequestCtx()
cookieValue := string(requestCtx.Request.Header.Cookie(m.compiledCookie)) cookieValue := string(requestCtx.Request.Header.Cookie(m.config.Cookie))
if cookieValue == "" { // cookie doesn't exists, let's generate a session and add set a cookie if cookieValue == "" { // cookie doesn't exists, let's generate a session and add set a cookie
sid := m.generateSessionID() sid := m.generateSessionID()
store, _ = m.provider.Init(sid) store, _ = m.provider.Init(sid)
cookie := fasthttp.AcquireCookie() cookie := fasthttp.AcquireCookie()
// The RFC makes no mention of encoding url value, so here I think to encode both sessionid key and the value using the safe(to put and to use as cookie) url-encoding // The RFC makes no mention of encoding url value, so here I think to encode both sessionid key and the value using the safe(to put and to use as cookie) url-encoding
cookie.SetKey(m.compiledCookie) cookie.SetKey(m.config.Cookie)
cookie.SetValue(base64.URLEncoding.EncodeToString([]byte(sid))) cookie.SetValue(base64.URLEncoding.EncodeToString([]byte(sid)))
cookie.SetPath("/") cookie.SetPath("/")
if !m.config.DisableSubdomainPersistance { if !m.config.DisableSubdomainPersistance {
requestDomain := ctx.HostString() requestDomain := ctx.HostString()
// there is a problem with .localhost setted as the domain, so we check that first
if strings.Count(requestDomain, ".") > 0 {
if portIdx := strings.IndexByte(requestDomain, ':'); portIdx > 0 { if portIdx := strings.IndexByte(requestDomain, ':'); portIdx > 0 {
requestDomain = requestDomain[0:portIdx] requestDomain = requestDomain[0:portIdx]
} }
@ -117,8 +117,11 @@ func (m *Manager) Start(ctx context.IContext) store.IStore {
requestDomain = strings.Replace(requestDomain, requestDomain[0:subdomainSuff], s, 1) // set to localhost.com || mysubdomain.localhost.com requestDomain = strings.Replace(requestDomain, requestDomain[0:subdomainSuff], s, 1) // set to localhost.com || mysubdomain.localhost.com
} }
} }
// finally set the .localhost.com (for(1-level) || .mysubdomain.localhost.com (for 2-level subdomain allow)
cookie.SetDomain("." + requestDomain) // . to allow persistance cookie.SetDomain("." + requestDomain) // . to allow persistance
} }
}
cookie.SetHTTPOnly(true) cookie.SetHTTPOnly(true)
cookie.SetExpire(m.config.Expires) cookie.SetExpire(m.config.Expires)
requestCtx.Response.Header.SetCookie(cookie) requestCtx.Response.Header.SetCookie(cookie)