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
func (ctx *Context) RemoveCookie(name string) {
cookie := fasthttp.AcquireCookie()
cookie.SetKey(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)
ctx.RequestCtx.Request.Header.DelCookie(name)
ctx.RequestCtx.Response.Header.DelClientCookie(name)
}
// GetFlash get a flash message by it's key

View File

@ -26,7 +26,6 @@ type (
config *config.Sessions
provider IProvider
mu sync.Mutex
compiledCookie string
}
)
@ -47,7 +46,7 @@ func newManager(c config.Sessions) (*Manager, error) {
manager := &Manager{}
manager.config = &c
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
}
@ -83,19 +82,20 @@ func (m *Manager) Start(ctx context.IContext) store.IStore {
m.mu.Lock()
var store store.IStore
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
sid := m.generateSessionID()
store, _ = m.provider.Init(sid)
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
cookie.SetKey(m.compiledCookie)
cookie.SetKey(m.config.Cookie)
cookie.SetValue(base64.URLEncoding.EncodeToString([]byte(sid)))
cookie.SetPath("/")
if !m.config.DisableSubdomainPersistance {
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 {
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
}
}
// finally set the .localhost.com (for(1-level) || .mysubdomain.localhost.com (for 2-level subdomain allow)
cookie.SetDomain("." + requestDomain) // . to allow persistance
}
}
cookie.SetHTTPOnly(true)
cookie.SetExpire(m.config.Expires)
requestCtx.Response.Header.SetCookie(cookie)