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 {
if
BConfig
.
WebConfig
.
Session
.
SessionOn
{
var
err
error
sessionConfig
:=
AppConfig
.
String
(
"sessionConfig"
)
conf
:=
new
(
session
.
ManagerConfig
)
if
sessionConfig
==
""
{
conf
:=
map
[
string
]
interface
{}{
"cookieName"
:
BConfig
.
WebConfig
.
Session
.
SessionName
,
"gclifetime"
:
BConfig
.
WebConfig
.
Session
.
SessionGCMaxLifetime
,
"providerConfig"
:
filepath
.
ToSlash
(
BConfig
.
WebConfig
.
Session
.
SessionProviderConfig
),
"secure"
:
BConfig
.
Listen
.
EnableHTTPS
,
"enableSetCookie"
:
BConfig
.
WebConfig
.
Session
.
SessionAutoSetCookie
,
"domain"
:
BConfig
.
WebConfig
.
Session
.
SessionDomain
,
"cookieLifeTime"
:
BConfig
.
WebConfig
.
Session
.
SessionCookieLifeTime
,
"enableSidInHttpHeader"
:
BConfig
.
WebConfig
.
Session
.
EnableSidInHttpHeader
,
"sessionNameInHttpHeader"
:
BConfig
.
WebConfig
.
Session
.
SessionNameInHttpHeader
,
"enableSidInUrlQuery"
:
BConfig
.
WebConfig
.
Session
.
EnableSidInUrlQuery
,
}
confBytes
,
err
:=
json
.
Marshal
(
conf
)
if
err
!=
nil
{
conf
.
CookieName
=
BConfig
.
WebConfig
.
Session
.
SessionName
conf
.
EnableSetCookie
=
BConfig
.
WebConfig
.
Session
.
SessionAutoSetCookie
conf
.
Gclifetime
=
BConfig
.
WebConfig
.
Session
.
SessionGCMaxLifetime
conf
.
Secure
=
BConfig
.
Listen
.
EnableHTTPS
conf
.
CookieLifeTime
=
BConfig
.
WebConfig
.
Session
.
SessionCookieLifeTime
conf
.
ProviderConfig
=
filepath
.
ToSlash
(
BConfig
.
WebConfig
.
Session
.
SessionProviderConfig
)
conf
.
Domain
=
BConfig
.
WebConfig
.
Session
.
SessionDomain
conf
.
EnableSidInHttpHeader
=
BConfig
.
WebConfig
.
Session
.
EnableSidInHttpHeader
conf
.
SessionNameInHttpHeader
=
BConfig
.
WebConfig
.
Session
.
SessionNameInHttpHeader
conf
.
EnableSidInUrlQuery
=
BConfig
.
WebConfig
.
Session
.
EnableSidInUrlQuery
}
else
{
if
err
=
json
.
Unmarshal
([]
byte
(
sessionConfig
),
conf
);
err
!=
nil
{
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
}
go
GlobalSessions
.
GC
()
...
...
session/sess_cookie_test.go
View file @
ef0d3d80
...
...
@@ -15,6 +15,7 @@
package
session
import
(
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
...
...
@@ -23,7 +24,11 @@ import (
func
TestCookie
(
t
*
testing
.
T
)
{
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
{
t
.
Fatal
(
"init cookie session err"
,
err
)
}
...
...
@@ -56,7 +61,11 @@ func TestCookie(t *testing.T) {
func
TestDestorySessionCookie
(
t
*
testing
.
T
)
{
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
{
t
.
Fatal
(
"init cookie session err"
,
err
)
}
...
...
session/sess_mem_test.go
View file @
ef0d3d80
...
...
@@ -15,6 +15,7 @@
package
session
import
(
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
...
...
@@ -22,7 +23,12 @@ import (
)
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
()
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/"
,
nil
)
w
:=
httptest
.
NewRecorder
()
...
...
session/sess_test.go
View file @
ef0d3d80
...
...
@@ -89,7 +89,7 @@ func TestCookieEncodeDecode(t *testing.T) {
func
TestParseConfig
(
t
*
testing
.
T
)
{
s
:=
`{"cookieName":"gosessionid","gclifetime":3600}`
cf
:=
new
(
m
anagerConfig
)
cf
:=
new
(
M
anagerConfig
)
cf
.
EnableSetCookie
=
true
err
:=
json
.
Unmarshal
([]
byte
(
s
),
cf
)
if
err
!=
nil
{
...
...
@@ -103,7 +103,7 @@ func TestParseConfig(t *testing.T) {
}
cc
:=
`{"cookieName":"gosessionid","enableSetCookie":false,"gclifetime":3600,"ProviderConfig":"{\"cookieName\":\"gosessionid\",\"securityKey\":\"beegocookiehashkey\"}"}`
cf2
:=
new
(
m
anagerConfig
)
cf2
:=
new
(
M
anagerConfig
)
cf2
.
EnableSetCookie
=
true
err
=
json
.
Unmarshal
([]
byte
(
cc
),
cf2
)
if
err
!=
nil
{
...
...
session/session.go
View file @
ef0d3d80
...
...
@@ -30,7 +30,6 @@ package session
import
(
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
...
...
@@ -82,7 +81,7 @@ func Register(name string, provide Provider) {
provides
[
name
]
=
provide
}
type
m
anagerConfig
struct
{
type
M
anagerConfig
struct
{
CookieName
string
`json:"cookieName"`
EnableSetCookie
bool
`json:"enableSetCookie,omitempty"`
Gclifetime
int64
`json:"gclifetime"`
...
...
@@ -100,7 +99,7 @@ type managerConfig struct {
// Manager contains Provider and its configuration.
type
Manager
struct
{
provider
Provider
config
*
m
anagerConfig
config
*
M
anagerConfig
}
// NewManager Create new Manager with provider name and json config string.
...
...
@@ -115,17 +114,12 @@ type Manager struct {
// 2. hashfunc default sha1
// 3. hashkey default beegosessionkey
// 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
]
if
!
ok
{
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
{
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
{
return
nil
,
err
}
...
...
@@ -166,7 +160,7 @@ func NewManager(provideName, config string) (*Manager, error) {
// otherwise return an valid session id.
func
(
manager
*
Manager
)
getSid
(
r
*
http
.
Request
)
(
string
,
error
)
{
cookie
,
errs
:=
r
.
Cookie
(
manager
.
config
.
CookieName
)
if
errs
!=
nil
||
cookie
.
Value
==
""
||
cookie
.
MaxAge
<
0
{
if
errs
!=
nil
||
cookie
.
Value
==
""
{
var
sid
string
if
manager
.
config
.
EnableSidInUrlQuery
{
errs
:=
r
.
ParseForm
()
...
...
@@ -211,6 +205,9 @@ func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (se
}
session
,
err
=
manager
.
provider
.
SessionRead
(
sid
)
if
err
!=
nil
{
return
nil
,
errs
}
cookie
:=
&
http
.
Cookie
{
Name
:
manager
.
config
.
CookieName
,
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