1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-23 03:10:56 +00:00

Merge pull request #2096 from Maxgis/feature_session

session.NewManager second params shoube be a object not a string
This commit is contained in:
astaxie 2016-08-16 23:51:35 +08:00 committed by GitHub
commit 7813cb5783
5 changed files with 43 additions and 33 deletions

View File

@ -45,26 +45,24 @@ func registerSession() error {
if BConfig.WebConfig.Session.SessionOn { if BConfig.WebConfig.Session.SessionOn {
var err error var err error
sessionConfig := AppConfig.String("sessionConfig") sessionConfig := AppConfig.String("sessionConfig")
conf := new(session.ManagerConfig)
if sessionConfig == "" { if sessionConfig == "" {
conf := map[string]interface{}{ conf.CookieName = BConfig.WebConfig.Session.SessionName
"cookieName": BConfig.WebConfig.Session.SessionName, conf.EnableSetCookie = BConfig.WebConfig.Session.SessionAutoSetCookie
"gclifetime": BConfig.WebConfig.Session.SessionGCMaxLifetime, conf.Gclifetime = BConfig.WebConfig.Session.SessionGCMaxLifetime
"providerConfig": filepath.ToSlash(BConfig.WebConfig.Session.SessionProviderConfig), conf.Secure = BConfig.Listen.EnableHTTPS
"secure": BConfig.Listen.EnableHTTPS, conf.CookieLifeTime = BConfig.WebConfig.Session.SessionCookieLifeTime
"enableSetCookie": BConfig.WebConfig.Session.SessionAutoSetCookie, conf.ProviderConfig = filepath.ToSlash(BConfig.WebConfig.Session.SessionProviderConfig)
"domain": BConfig.WebConfig.Session.SessionDomain, conf.Domain = BConfig.WebConfig.Session.SessionDomain
"cookieLifeTime": BConfig.WebConfig.Session.SessionCookieLifeTime, conf.EnableSidInHttpHeader = BConfig.WebConfig.Session.EnableSidInHttpHeader
"enableSidInHttpHeader": BConfig.WebConfig.Session.EnableSidInHttpHeader, conf.SessionNameInHttpHeader = BConfig.WebConfig.Session.SessionNameInHttpHeader
"sessionNameInHttpHeader": BConfig.WebConfig.Session.SessionNameInHttpHeader, conf.EnableSidInUrlQuery = BConfig.WebConfig.Session.EnableSidInUrlQuery
"enableSidInUrlQuery": BConfig.WebConfig.Session.EnableSidInUrlQuery, } else {
} if err = json.Unmarshal([]byte(sessionConfig), conf); err != nil {
confBytes, err := json.Marshal(conf)
if err != nil {
return err return err
} }
sessionConfig = string(confBytes)
} }
if GlobalSessions, err = session.NewManager(BConfig.WebConfig.Session.SessionProvider, sessionConfig); err != nil { if GlobalSessions, err = session.NewManager(BConfig.WebConfig.Session.SessionProvider, conf); err != nil {
return err return err
} }
go GlobalSessions.GC() go GlobalSessions.GC()

View File

@ -15,6 +15,7 @@
package session package session
import ( import (
"encoding/json"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"strings" "strings"
@ -23,7 +24,11 @@ import (
func TestCookie(t *testing.T) { func TestCookie(t *testing.T) {
config := `{"cookieName":"gosessionid","enableSetCookie":false,"gclifetime":3600,"ProviderConfig":"{\"cookieName\":\"gosessionid\",\"securityKey\":\"beegocookiehashkey\"}"}` config := `{"cookieName":"gosessionid","enableSetCookie":false,"gclifetime":3600,"ProviderConfig":"{\"cookieName\":\"gosessionid\",\"securityKey\":\"beegocookiehashkey\"}"}`
globalSessions, err := NewManager("cookie", config) conf := new(ManagerConfig)
if err := json.Unmarshal([]byte(config), conf); err != nil {
t.Fatal("json decode error", err)
}
globalSessions, err := NewManager("cookie", conf)
if err != nil { if err != nil {
t.Fatal("init cookie session err", err) t.Fatal("init cookie session err", err)
} }
@ -56,7 +61,11 @@ func TestCookie(t *testing.T) {
func TestDestorySessionCookie(t *testing.T) { func TestDestorySessionCookie(t *testing.T) {
config := `{"cookieName":"gosessionid","enableSetCookie":true,"gclifetime":3600,"ProviderConfig":"{\"cookieName\":\"gosessionid\",\"securityKey\":\"beegocookiehashkey\"}"}` config := `{"cookieName":"gosessionid","enableSetCookie":true,"gclifetime":3600,"ProviderConfig":"{\"cookieName\":\"gosessionid\",\"securityKey\":\"beegocookiehashkey\"}"}`
globalSessions, err := NewManager("cookie", config) conf := new(ManagerConfig)
if err := json.Unmarshal([]byte(config), conf); err != nil {
t.Fatal("json decode error", err)
}
globalSessions, err := NewManager("cookie", conf)
if err != nil { if err != nil {
t.Fatal("init cookie session err", err) t.Fatal("init cookie session err", err)
} }

View File

@ -15,6 +15,7 @@
package session package session
import ( import (
"encoding/json"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"strings" "strings"
@ -22,7 +23,12 @@ import (
) )
func TestMem(t *testing.T) { func TestMem(t *testing.T) {
globalSessions, _ := NewManager("memory", `{"cookieName":"gosessionid","gclifetime":10}`) config := `{"cookieName":"gosessionid","gclifetime":10, "enableSetCookie":true}`
conf := new(ManagerConfig)
if err := json.Unmarshal([]byte(config), conf); err != nil {
t.Fatal("json decode error", err)
}
globalSessions, _ := NewManager("memory", conf)
go globalSessions.GC() go globalSessions.GC()
r, _ := http.NewRequest("GET", "/", nil) r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()

View File

@ -89,7 +89,7 @@ func TestCookieEncodeDecode(t *testing.T) {
func TestParseConfig(t *testing.T) { func TestParseConfig(t *testing.T) {
s := `{"cookieName":"gosessionid","gclifetime":3600}` s := `{"cookieName":"gosessionid","gclifetime":3600}`
cf := new(managerConfig) cf := new(ManagerConfig)
cf.EnableSetCookie = true cf.EnableSetCookie = true
err := json.Unmarshal([]byte(s), cf) err := json.Unmarshal([]byte(s), cf)
if err != nil { if err != nil {
@ -103,7 +103,7 @@ func TestParseConfig(t *testing.T) {
} }
cc := `{"cookieName":"gosessionid","enableSetCookie":false,"gclifetime":3600,"ProviderConfig":"{\"cookieName\":\"gosessionid\",\"securityKey\":\"beegocookiehashkey\"}"}` cc := `{"cookieName":"gosessionid","enableSetCookie":false,"gclifetime":3600,"ProviderConfig":"{\"cookieName\":\"gosessionid\",\"securityKey\":\"beegocookiehashkey\"}"}`
cf2 := new(managerConfig) cf2 := new(ManagerConfig)
cf2.EnableSetCookie = true cf2.EnableSetCookie = true
err = json.Unmarshal([]byte(cc), cf2) err = json.Unmarshal([]byte(cc), cf2)
if err != nil { if err != nil {

View File

@ -30,7 +30,6 @@ package session
import ( import (
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -82,7 +81,7 @@ func Register(name string, provide Provider) {
provides[name] = provide provides[name] = provide
} }
type managerConfig struct { type ManagerConfig struct {
CookieName string `json:"cookieName"` CookieName string `json:"cookieName"`
EnableSetCookie bool `json:"enableSetCookie,omitempty"` EnableSetCookie bool `json:"enableSetCookie,omitempty"`
Gclifetime int64 `json:"gclifetime"` Gclifetime int64 `json:"gclifetime"`
@ -100,7 +99,7 @@ type managerConfig struct {
// Manager contains Provider and its configuration. // Manager contains Provider and its configuration.
type Manager struct { type Manager struct {
provider Provider provider Provider
config *managerConfig config *ManagerConfig
} }
// NewManager Create new Manager with provider name and json config string. // NewManager Create new Manager with provider name and json config string.
@ -115,17 +114,12 @@ type Manager struct {
// 2. hashfunc default sha1 // 2. hashfunc default sha1
// 3. hashkey default beegosessionkey // 3. hashkey default beegosessionkey
// 4. maxage default is none // 4. maxage default is none
func NewManager(provideName, config string) (*Manager, error) { func NewManager(provideName string, cf *ManagerConfig) (*Manager, error) {
provider, ok := provides[provideName] provider, ok := provides[provideName]
if !ok { if !ok {
return nil, fmt.Errorf("session: unknown provide %q (forgotten import?)", provideName) return nil, fmt.Errorf("session: unknown provide %q (forgotten import?)", provideName)
} }
cf := new(managerConfig)
cf.EnableSetCookie = true
err := json.Unmarshal([]byte(config), cf)
if err != nil {
return nil, err
}
if cf.Maxlifetime == 0 { if cf.Maxlifetime == 0 {
cf.Maxlifetime = cf.Gclifetime cf.Maxlifetime = cf.Gclifetime
} }
@ -142,7 +136,7 @@ func NewManager(provideName, config string) (*Manager, error) {
} }
} }
err = provider.SessionInit(cf.Maxlifetime, cf.ProviderConfig) err := provider.SessionInit(cf.Maxlifetime, cf.ProviderConfig)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -166,7 +160,7 @@ func NewManager(provideName, config string) (*Manager, error) {
// otherwise return an valid session id. // otherwise return an valid session id.
func (manager *Manager) getSid(r *http.Request) (string, error) { func (manager *Manager) getSid(r *http.Request) (string, error) {
cookie, errs := r.Cookie(manager.config.CookieName) cookie, errs := r.Cookie(manager.config.CookieName)
if errs != nil || cookie.Value == "" || cookie.MaxAge < 0 { if errs != nil || cookie.Value == "" {
var sid string var sid string
if manager.config.EnableSidInUrlQuery { if manager.config.EnableSidInUrlQuery {
errs := r.ParseForm() errs := r.ParseForm()
@ -211,6 +205,9 @@ func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (se
} }
session, err = manager.provider.SessionRead(sid) session, err = manager.provider.SessionRead(sid)
if err != nil {
return nil, errs
}
cookie := &http.Cookie{ cookie := &http.Cookie{
Name: manager.config.CookieName, Name: manager.config.CookieName,
Value: url.QueryEscape(sid), Value: url.QueryEscape(sid),