Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
ef0d3d80
Commit
ef0d3d80
authored
Aug 13, 2016
by
maxin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
set session.managerconfig public
parent
ce6f1987
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
43 additions
and
33 deletions
+43
-33
hooks.go
hooks.go
+14
-16
sess_cookie_test.go
session/sess_cookie_test.go
+11
-2
sess_mem_test.go
session/sess_mem_test.go
+7
-1
sess_test.go
session/sess_test.go
+2
-2
session.go
session/session.go
+9
-12
No files found.
hooks.go
View file @
ef0d3d80
...
@@ -45,26 +45,24 @@ func registerSession() error {
...
@@ -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
()
...
...
session/sess_cookie_test.go
View file @
ef0d3d80
...
@@ -15,6 +15,7 @@
...
@@ -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 (
...
@@ -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) {
...
@@ -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
)
}
}
...
...
session/sess_mem_test.go
View file @
ef0d3d80
...
@@ -15,6 +15,7 @@
...
@@ -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 (
...
@@ -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
()
...
...
session/sess_test.go
View file @
ef0d3d80
...
@@ -89,7 +89,7 @@ func TestCookieEncodeDecode(t *testing.T) {
...
@@ -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
(
m
anagerConfig
)
cf
:=
new
(
M
anagerConfig
)
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) {
...
@@ -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
(
m
anagerConfig
)
cf2
:=
new
(
M
anagerConfig
)
cf2
.
EnableSetCookie
=
true
cf2
.
EnableSetCookie
=
true
err
=
json
.
Unmarshal
([]
byte
(
cc
),
cf2
)
err
=
json
.
Unmarshal
([]
byte
(
cc
),
cf2
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
session/session.go
View file @
ef0d3d80
...
@@ -30,7 +30,6 @@ package session
...
@@ -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) {
...
@@ -82,7 +81,7 @@ func Register(name string, provide Provider) {
provides
[
name
]
=
provide
provides
[
name
]
=
provide
}
}
type
m
anagerConfig
struct
{
type
M
anagerConfig
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 {
...
@@ -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
*
m
anagerConfig
config
*
M
anagerConfig
}
}
// 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 {
...
@@ -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
strin
g
)
(
*
Manager
,
error
)
{
func
NewManager
(
provideName
string
,
cf
*
ManagerConfi
g
)
(
*
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) {
...
@@ -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) {
...
@@ -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
...
@@ -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
),
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment