Merge pull request #4404 from jianzhiyao/session-filter
make session easier to be configured
This commit is contained in:
commit
c603131436
@ -6,4 +6,5 @@
|
|||||||
- Fix `unknown escape sequence` in generated code. [4385](https://github.com/beego/beego/pull/4385)
|
- Fix `unknown escape sequence` in generated code. [4385](https://github.com/beego/beego/pull/4385)
|
||||||
- Using fixed name `commentRouter.go` as generated file name. [4385](https://github.com/beego/beego/pull/4385)
|
- Using fixed name `commentRouter.go` as generated file name. [4385](https://github.com/beego/beego/pull/4385)
|
||||||
- Fix 4383: ORM Adapter produces panic when using orm.RegisterModelWithPrefix. [4386](https://github.com/beego/beego/pull/4386)
|
- Fix 4383: ORM Adapter produces panic when using orm.RegisterModelWithPrefix. [4386](https://github.com/beego/beego/pull/4386)
|
||||||
- Support 4144: Add new api for order by for supporting multiple way to query [4294](https://github.com/beego/beego/pull/4294)
|
- Support 4144: Add new api for order by for supporting multiple way to query [4294](https://github.com/beego/beego/pull/4294)
|
||||||
|
- Support session Filter chain. [4404](https://github.com/beego/beego/pull/4404)
|
||||||
@ -301,6 +301,28 @@ func TestAddFilter(t *testing.T) {
|
|||||||
assert.Equal(t, 1, len(req.setting.FilterChains)-len(r.setting.FilterChains))
|
assert.Equal(t, 1, len(req.setting.FilterChains)-len(r.setting.FilterChains))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFilterChainOrder(t *testing.T) {
|
||||||
|
req := Get("http://beego.me")
|
||||||
|
req.AddFilters(func(next Filter) Filter {
|
||||||
|
return func(ctx context.Context, req *BeegoHTTPRequest) (*http.Response, error) {
|
||||||
|
return NewHttpResponseWithJsonBody("first"), nil
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
req.AddFilters(func(next Filter) Filter {
|
||||||
|
return func(ctx context.Context, req *BeegoHTTPRequest) (*http.Response, error) {
|
||||||
|
return NewHttpResponseWithJsonBody("second"), nil
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
resp, err := req.DoRequestWithCtx(context.Background())
|
||||||
|
assert.Nil(t, err)
|
||||||
|
data := make([]byte, 5)
|
||||||
|
_, _ = resp.Body.Read(data)
|
||||||
|
assert.Equal(t, "first", string(data))
|
||||||
|
}
|
||||||
|
|
||||||
func TestHead(t *testing.T) {
|
func TestHead(t *testing.T) {
|
||||||
req := Head("http://beego.me")
|
req := Head("http://beego.me")
|
||||||
assert.NotNil(t, req)
|
assert.NotNil(t, req)
|
||||||
|
|||||||
2
go.mod
2
go.mod
@ -25,7 +25,7 @@ require (
|
|||||||
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect
|
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect
|
||||||
github.com/gomodule/redigo v2.0.0+incompatible
|
github.com/gomodule/redigo v2.0.0+incompatible
|
||||||
github.com/google/go-cmp v0.5.0 // indirect
|
github.com/google/go-cmp v0.5.0 // indirect
|
||||||
github.com/google/uuid v1.1.1 // indirect
|
github.com/google/uuid v1.1.1
|
||||||
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
|
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
|
||||||
github.com/hashicorp/golang-lru v0.5.4
|
github.com/hashicorp/golang-lru v0.5.4
|
||||||
github.com/ledisdb/ledisdb v0.0.0-20200510135210-d35789ec47e6
|
github.com/ledisdb/ledisdb v0.0.0-20200510135210-d35789ec47e6
|
||||||
|
|||||||
@ -29,6 +29,7 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/beego/beego/v2/server/web/session"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -195,6 +196,22 @@ func (ctx *Context) RenderMethodResult(result interface{}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Session return session store of this context of request
|
||||||
|
func (ctx *Context) Session() (store session.Store, err error) {
|
||||||
|
if ctx.Input != nil {
|
||||||
|
if ctx.Input.CruSession != nil {
|
||||||
|
store = ctx.Input.CruSession
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
err = errors.New(`no valid session store(please initialize session)`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err = errors.New(`no valid input`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Response is a wrapper for the http.ResponseWriter
|
// Response is a wrapper for the http.ResponseWriter
|
||||||
// Started: if true, response was already written to so the other handler will not be executed
|
// Started: if true, response was already written to so the other handler will not be executed
|
||||||
type Response struct {
|
type Response struct {
|
||||||
|
|||||||
@ -15,6 +15,7 @@
|
|||||||
package context
|
package context
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/beego/beego/v2/server/web/session"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
@ -45,3 +46,26 @@ func TestXsrfReset_01(t *testing.T) {
|
|||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContext_Session(t *testing.T) {
|
||||||
|
c := NewContext()
|
||||||
|
if store, err := c.Session(); store != nil || err == nil {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContext_Session1(t *testing.T) {
|
||||||
|
c := Context{}
|
||||||
|
if store, err := c.Session(); store != nil || err == nil {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContext_Session2(t *testing.T) {
|
||||||
|
c := NewContext()
|
||||||
|
c.Input.CruSession = &session.MemSessionStore{}
|
||||||
|
|
||||||
|
if store, err := c.Session(); store == nil || err != nil {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
}
|
||||||
35
server/web/filter/session/filter.go
Normal file
35
server/web/filter/session/filter.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package session
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/beego/beego/v2/core/logs"
|
||||||
|
"github.com/beego/beego/v2/server/web"
|
||||||
|
webContext "github.com/beego/beego/v2/server/web/context"
|
||||||
|
"github.com/beego/beego/v2/server/web/session"
|
||||||
|
)
|
||||||
|
|
||||||
|
//Session maintain session for web service
|
||||||
|
//Session new a session storage and store it into webContext.Context
|
||||||
|
func Session(providerType session.ProviderType, options ...session.ManagerConfigOpt) web.FilterChain {
|
||||||
|
sessionConfig := session.NewManagerConfig(options...)
|
||||||
|
sessionManager, _ := session.NewManager(string(providerType), sessionConfig)
|
||||||
|
go sessionManager.GC()
|
||||||
|
|
||||||
|
return func(next web.FilterFunc) web.FilterFunc {
|
||||||
|
return func(ctx *webContext.Context) {
|
||||||
|
if ctx.Input.CruSession != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if sess, err := sessionManager.SessionStart(ctx.ResponseWriter, ctx.Request); err != nil {
|
||||||
|
logs.Error(`init session error:%s`, err.Error())
|
||||||
|
} else {
|
||||||
|
//release session at the end of request
|
||||||
|
defer sess.SessionRelease(context.Background(), ctx.ResponseWriter)
|
||||||
|
ctx.Input.CruSession = sess
|
||||||
|
}
|
||||||
|
|
||||||
|
next(ctx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
86
server/web/filter/session/filter_test.go
Normal file
86
server/web/filter/session/filter_test.go
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
package session
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/beego/beego/v2/server/web"
|
||||||
|
webContext "github.com/beego/beego/v2/server/web/context"
|
||||||
|
"github.com/beego/beego/v2/server/web/session"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func testRequest(t *testing.T, handler *web.ControllerRegister, path string, method string, code int) {
|
||||||
|
r, _ := http.NewRequest(method, path, nil)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
handler.ServeHTTP(w, r)
|
||||||
|
|
||||||
|
if w.Code != code {
|
||||||
|
t.Errorf("%s, %s: %d, supposed to be %d", path, method, w.Code, code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSession(t *testing.T) {
|
||||||
|
storeKey := uuid.New().String()
|
||||||
|
handler := web.NewControllerRegister()
|
||||||
|
handler.InsertFilterChain(
|
||||||
|
"*",
|
||||||
|
Session(
|
||||||
|
session.ProviderMemory,
|
||||||
|
session.CfgCookieName(`go_session_id`),
|
||||||
|
session.CfgSetCookie(true),
|
||||||
|
session.CfgGcLifeTime(3600),
|
||||||
|
session.CfgMaxLifeTime(3600),
|
||||||
|
session.CfgSecure(false),
|
||||||
|
session.CfgCookieLifeTime(3600),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
handler.InsertFilterChain(
|
||||||
|
"*",
|
||||||
|
func(next web.FilterFunc) web.FilterFunc {
|
||||||
|
return func(ctx *webContext.Context) {
|
||||||
|
if store := ctx.Input.GetData(storeKey); store == nil {
|
||||||
|
t.Error(`store should not be nil`)
|
||||||
|
}
|
||||||
|
next(ctx)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
handler.Any("*", func(ctx *webContext.Context) {
|
||||||
|
ctx.Output.SetStatus(200)
|
||||||
|
})
|
||||||
|
|
||||||
|
testRequest(t, handler, "/dataset1/resource1", "GET", 200)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSession1(t *testing.T) {
|
||||||
|
handler := web.NewControllerRegister()
|
||||||
|
handler.InsertFilterChain(
|
||||||
|
"*",
|
||||||
|
Session(
|
||||||
|
session.ProviderMemory,
|
||||||
|
session.CfgCookieName(`go_session_id`),
|
||||||
|
session.CfgSetCookie(true),
|
||||||
|
session.CfgGcLifeTime(3600),
|
||||||
|
session.CfgMaxLifeTime(3600),
|
||||||
|
session.CfgSecure(false),
|
||||||
|
session.CfgCookieLifeTime(3600),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
handler.InsertFilterChain(
|
||||||
|
"*",
|
||||||
|
func(next web.FilterFunc) web.FilterFunc {
|
||||||
|
return func(ctx *webContext.Context) {
|
||||||
|
if store, err := ctx.Session(); store == nil || err != nil {
|
||||||
|
t.Error(`store should not be nil`)
|
||||||
|
}
|
||||||
|
next(ctx)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
handler.Any("*", func(ctx *webContext.Context) {
|
||||||
|
ctx.Output.SetStatus(200)
|
||||||
|
})
|
||||||
|
|
||||||
|
testRequest(t, handler, "/dataset1/resource1", "GET", 200)
|
||||||
|
}
|
||||||
@ -15,9 +15,12 @@
|
|||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
@ -36,13 +39,46 @@ func TestControllerRegister_InsertFilterChain(t *testing.T) {
|
|||||||
ns := NewNamespace("/chain")
|
ns := NewNamespace("/chain")
|
||||||
|
|
||||||
ns.Get("/*", func(ctx *context.Context) {
|
ns.Get("/*", func(ctx *context.Context) {
|
||||||
ctx.Output.Body([]byte("hello"))
|
_ = ctx.Output.Body([]byte("hello"))
|
||||||
})
|
})
|
||||||
|
|
||||||
r, _ := http.NewRequest("GET", "/chain/user", nil)
|
r, _ := http.NewRequest("GET", "/chain/user", nil)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
BeeApp.Handlers.Init()
|
||||||
BeeApp.Handlers.ServeHTTP(w, r)
|
BeeApp.Handlers.ServeHTTP(w, r)
|
||||||
|
|
||||||
assert.Equal(t, "filter-chain", w.Header().Get("filter"))
|
assert.Equal(t, "filter-chain", w.Header().Get("filter"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestControllerRegister_InsertFilterChain_Order(t *testing.T) {
|
||||||
|
InsertFilterChain("/abc", func(next FilterFunc) FilterFunc {
|
||||||
|
return func(ctx *context.Context) {
|
||||||
|
ctx.Output.Header("first", fmt.Sprintf("%d", time.Now().UnixNano()))
|
||||||
|
time.Sleep(time.Millisecond * 10)
|
||||||
|
next(ctx)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
InsertFilterChain("/abc", func(next FilterFunc) FilterFunc {
|
||||||
|
return func(ctx *context.Context) {
|
||||||
|
ctx.Output.Header("second", fmt.Sprintf("%d", time.Now().UnixNano()))
|
||||||
|
time.Sleep(time.Millisecond * 10)
|
||||||
|
next(ctx)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
r, _ := http.NewRequest("GET", "/abc", nil)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
BeeApp.Handlers.Init()
|
||||||
|
BeeApp.Handlers.ServeHTTP(w, r)
|
||||||
|
first := w.Header().Get("first")
|
||||||
|
second := w.Header().Get("second")
|
||||||
|
|
||||||
|
ft, _ := strconv.ParseInt(first, 10, 64)
|
||||||
|
st, _ := strconv.ParseInt(second, 10, 64)
|
||||||
|
|
||||||
|
assert.True(t, st > ft)
|
||||||
|
}
|
||||||
|
|||||||
@ -139,6 +139,12 @@ func WithRouterSessionOn(sessionOn bool) ControllerOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type filterChainConfig struct {
|
||||||
|
pattern string
|
||||||
|
chain FilterChain
|
||||||
|
opts []FilterOpt
|
||||||
|
}
|
||||||
|
|
||||||
// ControllerRegister containers registered router rules, controller handlers and filters.
|
// ControllerRegister containers registered router rules, controller handlers and filters.
|
||||||
type ControllerRegister struct {
|
type ControllerRegister struct {
|
||||||
routers map[string]*Tree
|
routers map[string]*Tree
|
||||||
@ -151,6 +157,9 @@ type ControllerRegister struct {
|
|||||||
// the filter created by FilterChain
|
// the filter created by FilterChain
|
||||||
chainRoot *FilterRouter
|
chainRoot *FilterRouter
|
||||||
|
|
||||||
|
// keep registered chain and build it when serve http
|
||||||
|
filterChains []filterChainConfig
|
||||||
|
|
||||||
cfg *Config
|
cfg *Config
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,11 +180,23 @@ func NewControllerRegisterWithCfg(cfg *Config) *ControllerRegister {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
|
filterChains: make([]filterChainConfig, 0, 4),
|
||||||
}
|
}
|
||||||
res.chainRoot = newFilterRouter("/*", res.serveHttp, WithCaseSensitive(false))
|
res.chainRoot = newFilterRouter("/*", res.serveHttp, WithCaseSensitive(false))
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Init will be executed when HttpServer start running
|
||||||
|
func (p *ControllerRegister) Init() {
|
||||||
|
for i := len(p.filterChains) - 1; i >= 0 ; i -- {
|
||||||
|
fc := p.filterChains[i]
|
||||||
|
root := p.chainRoot
|
||||||
|
filterFunc := fc.chain(root.filterFunc)
|
||||||
|
p.chainRoot = newFilterRouter(fc.pattern, filterFunc, fc.opts...)
|
||||||
|
p.chainRoot.next = root
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Add controller handler and pattern rules to ControllerRegister.
|
// Add controller handler and pattern rules to ControllerRegister.
|
||||||
// usage:
|
// usage:
|
||||||
// default methods is the same name as method
|
// default methods is the same name as method
|
||||||
@ -513,12 +534,13 @@ func (p *ControllerRegister) InsertFilter(pattern string, pos int, filter Filter
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
func (p *ControllerRegister) InsertFilterChain(pattern string, chain FilterChain, opts ...FilterOpt) {
|
func (p *ControllerRegister) InsertFilterChain(pattern string, chain FilterChain, opts ...FilterOpt) {
|
||||||
root := p.chainRoot
|
|
||||||
filterFunc := chain(root.filterFunc)
|
|
||||||
opts = append(opts, WithCaseSensitive(p.cfg.RouterCaseSensitive))
|
|
||||||
p.chainRoot = newFilterRouter(pattern, filterFunc, opts...)
|
|
||||||
p.chainRoot.next = root
|
|
||||||
|
|
||||||
|
opts = append(opts, WithCaseSensitive(p.cfg.RouterCaseSensitive))
|
||||||
|
p.filterChains = append(p.filterChains, filterChainConfig{
|
||||||
|
pattern: pattern,
|
||||||
|
chain: chain,
|
||||||
|
opts: opts,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// add Filter into
|
// add Filter into
|
||||||
|
|||||||
@ -84,7 +84,9 @@ func (app *HttpServer) Run(addr string, mws ...MiddleWare) {
|
|||||||
|
|
||||||
initBeforeHTTPRun()
|
initBeforeHTTPRun()
|
||||||
|
|
||||||
|
// init...
|
||||||
app.initAddr(addr)
|
app.initAddr(addr)
|
||||||
|
app.Handlers.Init()
|
||||||
|
|
||||||
addr = app.Cfg.Listen.HTTPAddr
|
addr = app.Cfg.Listen.HTTPAddr
|
||||||
|
|
||||||
|
|||||||
@ -15,21 +15,22 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestRedis(t *testing.T) {
|
func TestRedis(t *testing.T) {
|
||||||
sessionConfig := &session.ManagerConfig{
|
|
||||||
CookieName: "gosessionid",
|
|
||||||
EnableSetCookie: true,
|
|
||||||
Gclifetime: 3600,
|
|
||||||
Maxlifetime: 3600,
|
|
||||||
Secure: false,
|
|
||||||
CookieLifeTime: 3600,
|
|
||||||
}
|
|
||||||
|
|
||||||
redisAddr := os.Getenv("REDIS_ADDR")
|
redisAddr := os.Getenv("REDIS_ADDR")
|
||||||
if redisAddr == "" {
|
if redisAddr == "" {
|
||||||
redisAddr = "127.0.0.1:6379"
|
redisAddr = "127.0.0.1:6379"
|
||||||
}
|
}
|
||||||
|
redisConfig := fmt.Sprintf("%s,100,,0,30", redisAddr)
|
||||||
|
|
||||||
|
sessionConfig := session.NewManagerConfig(
|
||||||
|
session.CfgCookieName(`gosessionid`),
|
||||||
|
session.CfgSetCookie(true),
|
||||||
|
session.CfgGcLifeTime(3600),
|
||||||
|
session.CfgMaxLifeTime(3600),
|
||||||
|
session.CfgSecure(false),
|
||||||
|
session.CfgCookieLifeTime(3600),
|
||||||
|
session.CfgProviderConfig(redisConfig),
|
||||||
|
)
|
||||||
|
|
||||||
sessionConfig.ProviderConfig = fmt.Sprintf("%s,100,,0,30", redisAddr)
|
|
||||||
globalSession, err := session.NewManager("redis", sessionConfig)
|
globalSession, err := session.NewManager("redis", sessionConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("could not create manager:", err)
|
t.Fatal("could not create manager:", err)
|
||||||
|
|||||||
@ -13,15 +13,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestRedisSentinel(t *testing.T) {
|
func TestRedisSentinel(t *testing.T) {
|
||||||
sessionConfig := &session.ManagerConfig{
|
sessionConfig := session.NewManagerConfig(
|
||||||
CookieName: "gosessionid",
|
session.CfgCookieName(`gosessionid`),
|
||||||
EnableSetCookie: true,
|
session.CfgSetCookie(true),
|
||||||
Gclifetime: 3600,
|
session.CfgGcLifeTime(3600),
|
||||||
Maxlifetime: 3600,
|
session.CfgMaxLifeTime(3600),
|
||||||
Secure: false,
|
session.CfgSecure(false),
|
||||||
CookieLifeTime: 3600,
|
session.CfgCookieLifeTime(3600),
|
||||||
ProviderConfig: "127.0.0.1:6379,100,,0,master",
|
session.CfgProviderConfig("127.0.0.1:6379,100,,0,master"),
|
||||||
}
|
)
|
||||||
globalSessions, e := session.NewManager("redis_sentinel", sessionConfig)
|
globalSessions, e := session.NewManager("redis_sentinel", sessionConfig)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
t.Log(e)
|
t.Log(e)
|
||||||
|
|||||||
@ -91,25 +91,6 @@ func GetProvider(name string) (Provider, error) {
|
|||||||
return provider, nil
|
return provider, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ManagerConfig define the session config
|
|
||||||
type ManagerConfig struct {
|
|
||||||
CookieName string `json:"cookieName"`
|
|
||||||
EnableSetCookie bool `json:"enableSetCookie,omitempty"`
|
|
||||||
Gclifetime int64 `json:"gclifetime"`
|
|
||||||
Maxlifetime int64 `json:"maxLifetime"`
|
|
||||||
DisableHTTPOnly bool `json:"disableHTTPOnly"`
|
|
||||||
Secure bool `json:"secure"`
|
|
||||||
CookieLifeTime int `json:"cookieLifeTime"`
|
|
||||||
ProviderConfig string `json:"providerConfig"`
|
|
||||||
Domain string `json:"domain"`
|
|
||||||
SessionIDLength int64 `json:"sessionIDLength"`
|
|
||||||
EnableSidInHTTPHeader bool `json:"EnableSidInHTTPHeader"`
|
|
||||||
SessionNameInHTTPHeader string `json:"SessionNameInHTTPHeader"`
|
|
||||||
EnableSidInURLQuery bool `json:"EnableSidInURLQuery"`
|
|
||||||
SessionIDPrefix string `json:"sessionIDPrefix"`
|
|
||||||
CookieSameSite http.SameSite `json:"cookieSameSite"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Manager contains Provider and its configuration.
|
// Manager contains Provider and its configuration.
|
||||||
type Manager struct {
|
type Manager struct {
|
||||||
provider Provider
|
provider Provider
|
||||||
|
|||||||
143
server/web/session/session_config.go
Normal file
143
server/web/session/session_config.go
Normal file
@ -0,0 +1,143 @@
|
|||||||
|
package session
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
// ManagerConfig define the session config
|
||||||
|
type ManagerConfig struct {
|
||||||
|
CookieName string `json:"cookieName"`
|
||||||
|
EnableSetCookie bool `json:"enableSetCookie,omitempty"`
|
||||||
|
Gclifetime int64 `json:"gclifetime"`
|
||||||
|
Maxlifetime int64 `json:"maxLifetime"`
|
||||||
|
DisableHTTPOnly bool `json:"disableHTTPOnly"`
|
||||||
|
Secure bool `json:"secure"`
|
||||||
|
CookieLifeTime int `json:"cookieLifeTime"`
|
||||||
|
ProviderConfig string `json:"providerConfig"`
|
||||||
|
Domain string `json:"domain"`
|
||||||
|
SessionIDLength int64 `json:"sessionIDLength"`
|
||||||
|
EnableSidInHTTPHeader bool `json:"EnableSidInHTTPHeader"`
|
||||||
|
SessionNameInHTTPHeader string `json:"SessionNameInHTTPHeader"`
|
||||||
|
EnableSidInURLQuery bool `json:"EnableSidInURLQuery"`
|
||||||
|
SessionIDPrefix string `json:"sessionIDPrefix"`
|
||||||
|
CookieSameSite http.SameSite `json:"cookieSameSite"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ManagerConfig) Opts(opts ...ManagerConfigOpt) {
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type ManagerConfigOpt func(config *ManagerConfig)
|
||||||
|
|
||||||
|
func NewManagerConfig(opts ...ManagerConfigOpt) *ManagerConfig {
|
||||||
|
config := &ManagerConfig{}
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(config)
|
||||||
|
}
|
||||||
|
return config
|
||||||
|
}
|
||||||
|
|
||||||
|
// CfgCookieName set key of session id
|
||||||
|
func CfgCookieName(cookieName string) ManagerConfigOpt {
|
||||||
|
return func(config *ManagerConfig) {
|
||||||
|
config.CookieName = cookieName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// CfgCookieName set len of session id
|
||||||
|
func CfgSessionIdLength(len int64) ManagerConfigOpt {
|
||||||
|
return func(config *ManagerConfig) {
|
||||||
|
config.SessionIDLength = len
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// CfgSessionIdPrefix set prefix of session id
|
||||||
|
func CfgSessionIdPrefix(prefix string) ManagerConfigOpt {
|
||||||
|
return func(config *ManagerConfig) {
|
||||||
|
config.SessionIDPrefix = prefix
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//CfgSetCookie whether set `Set-Cookie` header in HTTP response
|
||||||
|
func CfgSetCookie(enable bool) ManagerConfigOpt {
|
||||||
|
return func(config *ManagerConfig) {
|
||||||
|
config.EnableSetCookie = enable
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//CfgGcLifeTime set session gc lift time
|
||||||
|
func CfgGcLifeTime(lifeTime int64) ManagerConfigOpt {
|
||||||
|
return func(config *ManagerConfig) {
|
||||||
|
config.Gclifetime = lifeTime
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//CfgMaxLifeTime set session lift time
|
||||||
|
func CfgMaxLifeTime(lifeTime int64) ManagerConfigOpt {
|
||||||
|
return func(config *ManagerConfig) {
|
||||||
|
config.Maxlifetime = lifeTime
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//CfgGcLifeTime set session lift time
|
||||||
|
func CfgCookieLifeTime(lifeTime int) ManagerConfigOpt {
|
||||||
|
return func(config *ManagerConfig) {
|
||||||
|
config.CookieLifeTime = lifeTime
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//CfgProviderConfig configure session provider
|
||||||
|
func CfgProviderConfig(providerConfig string) ManagerConfigOpt {
|
||||||
|
return func(config *ManagerConfig) {
|
||||||
|
config.ProviderConfig = providerConfig
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//CfgDomain set cookie domain
|
||||||
|
func CfgDomain(domain string) ManagerConfigOpt {
|
||||||
|
return func(config *ManagerConfig) {
|
||||||
|
config.Domain = domain
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//CfgSessionIdInHTTPHeader enable session id in http header
|
||||||
|
func CfgSessionIdInHTTPHeader(enable bool) ManagerConfigOpt {
|
||||||
|
return func(config *ManagerConfig) {
|
||||||
|
config.EnableSidInHTTPHeader = enable
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//CfgSetSessionNameInHTTPHeader set key of session id in http header
|
||||||
|
func CfgSetSessionNameInHTTPHeader(name string) ManagerConfigOpt {
|
||||||
|
return func(config *ManagerConfig) {
|
||||||
|
config.SessionNameInHTTPHeader = name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//EnableSidInURLQuery enable session id in query string
|
||||||
|
func CfgEnableSidInURLQuery(enable bool) ManagerConfigOpt {
|
||||||
|
return func(config *ManagerConfig) {
|
||||||
|
config.EnableSidInURLQuery = enable
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//DisableHTTPOnly set HTTPOnly for http.Cookie
|
||||||
|
func CfgHTTPOnly(HTTPOnly bool) ManagerConfigOpt {
|
||||||
|
return func(config *ManagerConfig) {
|
||||||
|
config.DisableHTTPOnly = !HTTPOnly
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//CfgSecure set Secure for http.Cookie
|
||||||
|
func CfgSecure(Enable bool) ManagerConfigOpt {
|
||||||
|
return func(config *ManagerConfig) {
|
||||||
|
config.Secure = Enable
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//CfgSameSite set http.SameSite
|
||||||
|
func CfgSameSite(sameSite http.SameSite) ManagerConfigOpt {
|
||||||
|
return func(config *ManagerConfig) {
|
||||||
|
config.CookieSameSite = sameSite
|
||||||
|
}
|
||||||
|
}
|
||||||
222
server/web/session/session_config_test.go
Normal file
222
server/web/session/session_config_test.go
Normal file
@ -0,0 +1,222 @@
|
|||||||
|
package session
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCfgCookieLifeTime(t *testing.T) {
|
||||||
|
value := 8754
|
||||||
|
c := NewManagerConfig(
|
||||||
|
CfgCookieLifeTime(value),
|
||||||
|
)
|
||||||
|
|
||||||
|
if c.CookieLifeTime != value {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCfgDomain(t *testing.T) {
|
||||||
|
value := `http://domain.com`
|
||||||
|
c := NewManagerConfig(
|
||||||
|
CfgDomain(value),
|
||||||
|
)
|
||||||
|
|
||||||
|
if c.Domain != value {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCfgSameSite(t *testing.T) {
|
||||||
|
value := http.SameSiteLaxMode
|
||||||
|
c := NewManagerConfig(
|
||||||
|
CfgSameSite(value),
|
||||||
|
)
|
||||||
|
|
||||||
|
if c.CookieSameSite != value {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCfgSecure(t *testing.T) {
|
||||||
|
c := NewManagerConfig(
|
||||||
|
CfgSecure(true),
|
||||||
|
)
|
||||||
|
|
||||||
|
if c.Secure != true {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCfgSecure1(t *testing.T) {
|
||||||
|
c := NewManagerConfig(
|
||||||
|
CfgSecure(false),
|
||||||
|
)
|
||||||
|
|
||||||
|
if c.Secure != false {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCfgSessionIdPrefix(t *testing.T) {
|
||||||
|
value := `sodiausodkljalsd`
|
||||||
|
c := NewManagerConfig(
|
||||||
|
CfgSessionIdPrefix(value),
|
||||||
|
)
|
||||||
|
|
||||||
|
if c.SessionIDPrefix != value {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCfgSetSessionNameInHTTPHeader(t *testing.T) {
|
||||||
|
value := `sodiausodkljalsd`
|
||||||
|
c := NewManagerConfig(
|
||||||
|
CfgSetSessionNameInHTTPHeader(value),
|
||||||
|
)
|
||||||
|
|
||||||
|
if c.SessionNameInHTTPHeader != value {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCfgCookieName(t *testing.T) {
|
||||||
|
value := `sodiausodkljalsd`
|
||||||
|
c := NewManagerConfig(
|
||||||
|
CfgCookieName(value),
|
||||||
|
)
|
||||||
|
|
||||||
|
if c.CookieName != value {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCfgEnableSidInURLQuery(t *testing.T) {
|
||||||
|
c := NewManagerConfig(
|
||||||
|
CfgEnableSidInURLQuery(true),
|
||||||
|
)
|
||||||
|
|
||||||
|
if c.EnableSidInURLQuery != true {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCfgGcLifeTime(t *testing.T) {
|
||||||
|
value := int64(5454)
|
||||||
|
c := NewManagerConfig(
|
||||||
|
CfgGcLifeTime(value),
|
||||||
|
)
|
||||||
|
|
||||||
|
if c.Gclifetime != value {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCfgHTTPOnly(t *testing.T) {
|
||||||
|
c := NewManagerConfig(
|
||||||
|
CfgHTTPOnly(true),
|
||||||
|
)
|
||||||
|
|
||||||
|
if c.DisableHTTPOnly != false {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCfgHTTPOnly2(t *testing.T) {
|
||||||
|
c := NewManagerConfig(
|
||||||
|
CfgHTTPOnly(false),
|
||||||
|
)
|
||||||
|
|
||||||
|
if c.DisableHTTPOnly != true {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCfgMaxLifeTime(t *testing.T) {
|
||||||
|
value := int64(5454)
|
||||||
|
c := NewManagerConfig(
|
||||||
|
CfgMaxLifeTime(value),
|
||||||
|
)
|
||||||
|
|
||||||
|
if c.Maxlifetime != value {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCfgProviderConfig(t *testing.T) {
|
||||||
|
value := `asodiuasldkj12i39809as`
|
||||||
|
c := NewManagerConfig(
|
||||||
|
CfgProviderConfig(value),
|
||||||
|
)
|
||||||
|
|
||||||
|
if c.ProviderConfig != value {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCfgSessionIdInHTTPHeader(t *testing.T) {
|
||||||
|
c := NewManagerConfig(
|
||||||
|
CfgSessionIdInHTTPHeader(true),
|
||||||
|
)
|
||||||
|
|
||||||
|
if c.EnableSidInHTTPHeader != true {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCfgSessionIdInHTTPHeader1(t *testing.T) {
|
||||||
|
c := NewManagerConfig(
|
||||||
|
CfgSessionIdInHTTPHeader(false),
|
||||||
|
)
|
||||||
|
|
||||||
|
if c.EnableSidInHTTPHeader != false {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCfgSessionIdLength(t *testing.T) {
|
||||||
|
value := int64(100)
|
||||||
|
c := NewManagerConfig(
|
||||||
|
CfgSessionIdLength(value),
|
||||||
|
)
|
||||||
|
|
||||||
|
if c.SessionIDLength != value {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCfgSetCookie(t *testing.T) {
|
||||||
|
c := NewManagerConfig(
|
||||||
|
CfgSetCookie(true),
|
||||||
|
)
|
||||||
|
|
||||||
|
if c.EnableSetCookie != true {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCfgSetCookie1(t *testing.T) {
|
||||||
|
c := NewManagerConfig(
|
||||||
|
CfgSetCookie(false),
|
||||||
|
)
|
||||||
|
|
||||||
|
if c.EnableSetCookie != false {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewManagerConfig(t *testing.T) {
|
||||||
|
c := NewManagerConfig()
|
||||||
|
if c == nil {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestManagerConfig_Opts(t *testing.T) {
|
||||||
|
c := NewManagerConfig()
|
||||||
|
c.Opts(CfgSetCookie(true))
|
||||||
|
|
||||||
|
if c.EnableSetCookie != true {
|
||||||
|
t.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
18
server/web/session/session_provider_type.go
Normal file
18
server/web/session/session_provider_type.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package session
|
||||||
|
|
||||||
|
type ProviderType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
ProviderCookie ProviderType = `cookie`
|
||||||
|
ProviderFile ProviderType = `file`
|
||||||
|
ProviderMemory ProviderType = `memory`
|
||||||
|
ProviderCouchbase ProviderType = `couchbase`
|
||||||
|
ProviderLedis ProviderType = `ledis`
|
||||||
|
ProviderMemcache ProviderType = `memcache`
|
||||||
|
ProviderMysql ProviderType = `mysql`
|
||||||
|
ProviderPostgresql ProviderType = `postgresql`
|
||||||
|
ProviderRedis ProviderType = `redis`
|
||||||
|
ProviderRedisCluster ProviderType = `redis_cluster`
|
||||||
|
ProviderRedisSentinel ProviderType = `redis_sentinel`
|
||||||
|
ProviderSsdb ProviderType = `ssdb`
|
||||||
|
)
|
||||||
Loading…
x
Reference in New Issue
Block a user