1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-29 16:41:29 +00:00
Beego/session/session.go

242 lines
6.3 KiB
Go
Raw Normal View History

package session
import (
2013-09-26 10:07:00 +00:00
"crypto/hmac"
"crypto/md5"
"crypto/rand"
2013-09-26 10:07:00 +00:00
"crypto/sha1"
"encoding/hex"
"fmt"
"io"
"net/http"
"net/url"
"time"
)
type SessionStore interface {
Set(key, value interface{}) error //set session value
Get(key interface{}) interface{} //get session value
Delete(key interface{}) error //delete session value
SessionID() string //back current sessionID
2013-11-05 13:59:35 +00:00
SessionRelease() // release the resource & save data to provider
2013-09-26 10:07:00 +00:00
Flush() error //delete all data
}
type Provider interface {
SessionInit(maxlifetime int64, savePath string) error
SessionRead(sid string) (SessionStore, error)
2013-11-05 14:23:48 +00:00
SessionExist(sid string) bool
2013-09-26 10:07:00 +00:00
SessionRegenerate(oldsid, sid string) (SessionStore, error)
SessionDestroy(sid string) error
2013-11-01 16:16:10 +00:00
SessionAll() int //get all active session
SessionGC()
}
var provides = make(map[string]Provider)
// Register makes a session provide available by the provided name.
// If Register is called twice with the same name or if driver is nil,
// it panics.
func Register(name string, provide Provider) {
if provide == nil {
panic("session: Register provide is nil")
}
if _, dup := provides[name]; dup {
panic("session: Register called twice for provider " + name)
}
provides[name] = provide
}
type Manager struct {
cookieName string //private cookiename
provider Provider
maxlifetime int64
2013-09-26 10:07:00 +00:00
hashfunc string //support md5 & sha1
hashkey string
2013-10-28 15:09:45 +00:00
maxage int //cookielifetime
2013-10-28 13:44:07 +00:00
secure bool
2013-09-25 14:54:07 +00:00
options []interface{}
}
2013-09-26 10:07:00 +00:00
//options
//1. is https default false
//2. hashfunc default sha1
//3. hashkey default beegosessionkey
//4. maxage default is none
2013-09-25 14:54:07 +00:00
func NewManager(provideName, cookieName string, maxlifetime int64, savePath string, options ...interface{}) (*Manager, error) {
provider, ok := provides[provideName]
if !ok {
return nil, fmt.Errorf("session: unknown provide %q (forgotten import?)", provideName)
}
provider.SessionInit(maxlifetime, savePath)
2013-10-28 13:44:07 +00:00
secure := false
if len(options) > 0 {
secure = options[0].(bool)
}
2013-09-26 10:07:00 +00:00
hashfunc := "sha1"
if len(options) > 1 {
hashfunc = options[1].(string)
}
hashkey := "beegosessionkey"
if len(options) > 2 {
hashkey = options[2].(string)
}
2013-10-28 13:44:07 +00:00
maxage := -1
if len(options) > 3 {
switch options[3].(type) {
case int:
if options[3].(int) > 0 {
maxage = options[3].(int)
} else if options[3].(int) < 0 {
maxage = 0
}
case int64:
if options[3].(int64) > 0 {
maxage = int(options[3].(int64))
} else if options[3].(int64) < 0 {
maxage = 0
}
case int32:
if options[3].(int32) > 0 {
maxage = int(options[3].(int32))
} else if options[3].(int32) < 0 {
maxage = 0
}
}
}
2013-09-26 10:07:00 +00:00
return &Manager{
provider: provider,
cookieName: cookieName,
maxlifetime: maxlifetime,
hashfunc: hashfunc,
hashkey: hashkey,
2013-10-28 13:44:07 +00:00
maxage: maxage,
secure: secure,
2013-09-26 10:07:00 +00:00
options: options,
}, nil
}
//get Session
func (manager *Manager) SessionStart(w http.ResponseWriter, r *http.Request) (session SessionStore) {
cookie, err := r.Cookie(manager.cookieName)
if err != nil || cookie.Value == "" {
2013-09-26 10:07:00 +00:00
sid := manager.sessionId(r)
session, _ = manager.provider.SessionRead(sid)
2013-09-26 10:07:00 +00:00
cookie = &http.Cookie{Name: manager.cookieName,
2013-04-08 04:50:11 +00:00
Value: url.QueryEscape(sid),
Path: "/",
HttpOnly: true,
2013-10-28 13:44:07 +00:00
Secure: manager.secure}
if manager.maxage >= 0 {
cookie.MaxAge = manager.maxage
2013-09-26 10:07:00 +00:00
}
http.SetCookie(w, cookie)
r.AddCookie(cookie)
} else {
sid, _ := url.QueryUnescape(cookie.Value)
2013-11-05 14:23:48 +00:00
if manager.provider.SessionExist(sid) {
session, _ = manager.provider.SessionRead(sid)
} else {
sid = manager.sessionId(r)
session, _ = manager.provider.SessionRead(sid)
cookie = &http.Cookie{Name: manager.cookieName,
Value: url.QueryEscape(sid),
Path: "/",
HttpOnly: true,
Secure: manager.secure}
if manager.maxage >= 0 {
cookie.MaxAge = manager.maxage
}
http.SetCookie(w, cookie)
r.AddCookie(cookie)
}
}
return
}
//Destroy sessionid
func (manager *Manager) SessionDestroy(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie(manager.cookieName)
if err != nil || cookie.Value == "" {
return
} else {
manager.provider.SessionDestroy(cookie.Value)
expiration := time.Now()
cookie := http.Cookie{Name: manager.cookieName, Path: "/", HttpOnly: true, Expires: expiration, MaxAge: -1}
http.SetCookie(w, &cookie)
}
}
2013-10-28 15:25:30 +00:00
func (manager *Manager) GetProvider(sid string) (sessions SessionStore, err error) {
sessions, err = manager.provider.SessionRead(sid)
return
}
func (manager *Manager) GC() {
manager.provider.SessionGC()
time.AfterFunc(time.Duration(manager.maxlifetime)*time.Second, func() { manager.GC() })
}
2013-09-26 10:07:00 +00:00
func (manager *Manager) SessionRegenerateId(w http.ResponseWriter, r *http.Request) (session SessionStore) {
sid := manager.sessionId(r)
cookie, err := r.Cookie(manager.cookieName)
if err != nil && cookie.Value == "" {
//delete old cookie
session, _ = manager.provider.SessionRead(sid)
cookie = &http.Cookie{Name: manager.cookieName,
Value: url.QueryEscape(sid),
Path: "/",
HttpOnly: true,
2013-10-28 13:44:07 +00:00
Secure: manager.secure,
2013-09-26 10:07:00 +00:00
}
} else {
oldsid, _ := url.QueryUnescape(cookie.Value)
session, _ = manager.provider.SessionRegenerate(oldsid, sid)
cookie.Value = url.QueryEscape(sid)
cookie.HttpOnly = true
cookie.Path = "/"
}
2013-10-28 13:44:07 +00:00
if manager.maxage >= 0 {
cookie.MaxAge = manager.maxage
2013-09-26 10:07:00 +00:00
}
http.SetCookie(w, cookie)
r.AddCookie(cookie)
return
}
2013-11-01 16:16:10 +00:00
func (manager *Manager) GetActiveSession() int {
return manager.provider.SessionAll()
}
2013-11-05 13:59:35 +00:00
func (manager *Manager) SetHashFunc(hasfunc, hashkey string) {
manager.hashfunc = hasfunc
manager.hashkey = hashkey
}
2013-09-26 10:07:00 +00:00
2013-11-05 13:59:35 +00:00
func (manager *Manager) SetSecure(secure bool) {
manager.secure = secure
}
//remote_addr cruunixnano randdata
2013-09-26 10:07:00 +00:00
func (manager *Manager) sessionId(r *http.Request) (sid string) {
2013-10-21 14:06:30 +00:00
bs := make([]byte, 24)
if _, err := io.ReadFull(rand.Reader, bs); err != nil {
return ""
}
2013-09-26 10:07:00 +00:00
sig := fmt.Sprintf("%s%d%s", r.RemoteAddr, time.Now().UnixNano(), bs)
if manager.hashfunc == "md5" {
h := md5.New()
2013-10-21 14:06:30 +00:00
h.Write([]byte(sig))
sid = hex.EncodeToString(h.Sum(nil))
2013-09-26 10:07:00 +00:00
} else if manager.hashfunc == "sha1" {
h := hmac.New(sha1.New, []byte(manager.hashkey))
fmt.Fprintf(h, "%s", sig)
2013-10-21 14:06:30 +00:00
sid = hex.EncodeToString(h.Sum(nil))
2013-09-26 10:07:00 +00:00
} else {
h := hmac.New(sha1.New, []byte(manager.hashkey))
fmt.Fprintf(h, "%s", sig)
2013-10-21 14:06:30 +00:00
sid = hex.EncodeToString(h.Sum(nil))
2013-09-26 10:07:00 +00:00
}
return
}