Skip to content

Commit f9c07c4

Browse files
committed
update session
1 parent 0d1872e commit f9c07c4

File tree

7 files changed

+79
-21
lines changed

7 files changed

+79
-21
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ gogs
55
*.db
66
*.log
77
custom/
8+
data/
89
.vendor/
910
.idea/
1011
*.iml

conf/app.ini

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,33 @@ INTERVAL = 60
7272
; memcache: "127.0.0.1:11211"
7373
HOST =
7474

75+
[session]
76+
; Either "memory", "file", "redis" or "mysql", default is "memory"
77+
PROVIDER = file
78+
; provider config
79+
; memory: not have any config yet
80+
; file: session file path
81+
; e.g. tmp/sessions
82+
; redis: config like redis server addr,poolSize,password
83+
; e.g. 127.0.0.1:6379,100,astaxie
84+
; mysql: go-sql-driver/mysql dsn config string
85+
; e.g. root:password@/session_table
86+
PROVIDER_CONFIG = data/sessions
87+
; session cookie name
88+
COOKIE_NAME = i_like_gogits
89+
; if you use session in https only, default is false
90+
COOKIE_SECURE = false
91+
; enable set cookie, default is true
92+
ENABLE_SET_COOKIE = true
93+
; session gc time interval, default is 86400
94+
GC_INTERVAL_TIME = 86400
95+
; session life time, default is 86400
96+
SESSION_LIFE_TIME = 86400
97+
; session id hash func, default is sha1
98+
SESSION_ID_HASHFUNC = sha1
99+
; session hash key, default is use random string
100+
SESSION_ID_HASHKEY =
101+
75102
[picture]
76103
; The place to picture data, either "server" or "qiniu", default is "server"
77104
SERVICE = server

modules/auth/user.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99
"reflect"
1010

1111
"github.com/codegangsta/martini"
12-
"github.com/martini-contrib/sessions"
12+
13+
"github.com/gogits/session"
1314

1415
"github.com/gogits/binding"
1516

@@ -19,7 +20,7 @@ import (
1920
)
2021

2122
// SignedInId returns the id of signed in user.
22-
func SignedInId(session sessions.Session) int64 {
23+
func SignedInId(session session.SessionStore) int64 {
2324
userId := session.Get("userId")
2425
if userId == nil {
2526
return 0
@@ -34,7 +35,7 @@ func SignedInId(session sessions.Session) int64 {
3435
}
3536

3637
// SignedInName returns the name of signed in user.
37-
func SignedInName(session sessions.Session) string {
38+
func SignedInName(session session.SessionStore) string {
3839
userName := session.Get("userName")
3940
if userName == nil {
4041
return ""
@@ -46,7 +47,7 @@ func SignedInName(session sessions.Session) string {
4647
}
4748

4849
// SignedInUser returns the user object of signed user.
49-
func SignedInUser(session sessions.Session) *models.User {
50+
func SignedInUser(session session.SessionStore) *models.User {
5051
id := SignedInId(session)
5152
if id <= 0 {
5253
return nil
@@ -61,7 +62,7 @@ func SignedInUser(session sessions.Session) *models.User {
6162
}
6263

6364
// IsSignedIn check if any user has signed in.
64-
func IsSignedIn(session sessions.Session) bool {
65+
func IsSignedIn(session session.SessionStore) bool {
6566
return SignedInId(session) > 0
6667
}
6768

modules/base/conf.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/Unknwon/goconfig"
1717

1818
"github.com/gogits/cache"
19+
"github.com/gogits/session"
1920

2021
"github.com/gogits/gogs/modules/log"
2122
)
@@ -49,6 +50,10 @@ var (
4950

5051
LogMode string
5152
LogConfig string
53+
54+
SessionProvider string
55+
SessionConfig *session.Config
56+
SessionManager *session.Manager
5257
)
5358

5459
var Service struct {
@@ -164,6 +169,30 @@ func newCacheService() {
164169
log.Info("Cache Service Enabled")
165170
}
166171

172+
func newSessionService() {
173+
SessionProvider = Cfg.MustValue("session", "PROVIDER", "memory")
174+
175+
SessionConfig = new(session.Config)
176+
SessionConfig.ProviderConfig = Cfg.MustValue("session", "PROVIDER_CONFIG")
177+
SessionConfig.CookieName = Cfg.MustValue("session", "COOKIE_NAME", "i_like_gogits")
178+
SessionConfig.CookieSecure = Cfg.MustBool("session", "COOKIE_SECURE")
179+
SessionConfig.EnableSetCookie = Cfg.MustBool("session", "ENABLE_SET_COOKIE", true)
180+
SessionConfig.GcIntervalTime = Cfg.MustInt64("session", "GC_INTERVAL_TIME", 86400)
181+
SessionConfig.SessionLifeTime = Cfg.MustInt64("session", "SESSION_LIFE_TIME", 86400)
182+
SessionConfig.SessionIDHashFunc = Cfg.MustValue("session", "SESSION_ID_HASHFUNC", "sha1")
183+
SessionConfig.SessionIDHashKey = Cfg.MustValue("session", "SESSION_ID_HASHKEY")
184+
185+
var err error
186+
SessionManager, err = session.NewManager(SessionProvider, *SessionConfig)
187+
if err != nil {
188+
fmt.Printf("Init session system failed, provider: %s, %v\n",
189+
SessionProvider, err)
190+
os.Exit(2)
191+
}
192+
193+
log.Info("Session Service Enabled")
194+
}
195+
167196
func newMailService() {
168197
// Check mailer setting.
169198
if Cfg.MustBool("mailer", "ENABLED") {
@@ -234,6 +263,7 @@ func NewServices() {
234263
newService()
235264
newLogService()
236265
newCacheService()
266+
newSessionService()
237267
newMailService()
238268
newRegisterMailService()
239269
}

modules/middleware/context.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
"time"
1111

1212
"github.com/codegangsta/martini"
13-
"github.com/martini-contrib/sessions"
1413

1514
"github.com/gogits/cache"
15+
"github.com/gogits/session"
1616

1717
"github.com/gogits/gogs/models"
1818
"github.com/gogits/gogs/modules/auth"
@@ -27,7 +27,7 @@ type Context struct {
2727
p martini.Params
2828
Req *http.Request
2929
Res http.ResponseWriter
30-
Session sessions.Session
30+
Session session.SessionStore
3131
Cache cache.Cache
3232
User *models.User
3333
IsSigned bool
@@ -92,21 +92,25 @@ func (ctx *Context) Handle(status int, title string, err error) {
9292

9393
// InitContext initializes a classic context for a request.
9494
func InitContext() martini.Handler {
95-
return func(res http.ResponseWriter, r *http.Request, c martini.Context,
96-
session sessions.Session, rd *Render) {
95+
return func(res http.ResponseWriter, r *http.Request, c martini.Context, rd *Render) {
9796

9897
ctx := &Context{
9998
c: c,
10099
// p: p,
101-
Req: r,
102-
Res: res,
103-
Session: session,
104-
Cache: base.Cache,
105-
Render: rd,
100+
Req: r,
101+
Res: res,
102+
Cache: base.Cache,
103+
Render: rd,
106104
}
107105

106+
// start session
107+
ctx.Session = base.SessionManager.SessionStart(res, r)
108+
defer func() {
109+
ctx.Session.SessionRelease(res)
110+
}()
111+
108112
// Get user from session if logined.
109-
user := auth.SignedInUser(session)
113+
user := auth.SignedInUser(ctx.Session)
110114
ctx.User = user
111115
ctx.IsSigned = user != nil
112116

routers/user/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func SignIn(ctx *middleware.Context, form auth.LogInForm) {
8888

8989
user, err := models.LoginUserPlain(form.UserName, form.Password)
9090
if err != nil {
91-
if err.Error() == models.ErrUserNotExist.Error() {
91+
if err == models.ErrUserNotExist {
9292
ctx.RenderWithErr("Username or password is not correct", "user/signin", &form)
9393
return
9494
}

web.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
"github.com/codegangsta/cli"
1414
"github.com/codegangsta/martini"
15-
"github.com/martini-contrib/sessions"
1615

1716
"github.com/gogits/binding"
1817

@@ -81,10 +80,6 @@ func runWeb(*cli.Context) {
8180
// Middlewares.
8281
m.Use(middleware.Renderer(middleware.RenderOptions{Funcs: []template.FuncMap{base.TemplateFuncs}}))
8382

84-
// TODO: should use other store because cookie store is not secure.
85-
store := sessions.NewCookieStore([]byte("secret123"))
86-
m.Use(sessions.Sessions("my_session", store))
87-
8883
m.Use(middleware.InitContext())
8984

9085
reqSignIn := middleware.SignInRequire(true)

0 commit comments

Comments
 (0)