Documentation
¶
Overview ¶
Package safesession 实现安全登录会话。
这里的session表示保持登录的session。
Index ¶
- Variables
- type Control
- func (c *Control) Check(clientIP, userAgent string, s *Session) (bool, error)
- func (c *Control) CheckLogined(clientIP, userAgent string, cookie *http.Cookie) (bool, error, Session)
- func (c *Control) NewSession(clientIP, userAgent, UserName string) Session
- func (c *Control) SetSession(se *Session, w http.ResponseWriter)
- type DB
- type IPInfo
- type Session
Constants ¶
This section is empty.
Variables ¶
View Source
var LoginExpired = errors.New("登录已过期,请重新登录")
View Source
var RegionErr = errors.New("IP属地在两次登录时不在同一个地区,请重新登录")
View Source
var Test = false
Test 为true将在创建 Session 时不获取ip属地。
Functions ¶
This section is empty.
Types ¶
type Control ¶
type Control struct {
// contains filtered or unexported fields
}
Control 管理所有 Session。
零值无效,必须使用 NewControl 初始化。
func NewControl ¶
func (*Control) CheckLogined ¶
func (c *Control) CheckLogined(clientIP, userAgent string, cookie *http.Cookie) (bool, error, Session)
CheckLogined 检查是否已经登录。 从多个goroutine调用是安全的。 如果err!=nil,调用者应该删除cookie(响应MaxAge<0)。
func (*Control) NewSession ¶
NewSession 创建一个 Session ,保证ID不重复。 从多个goroutine调用是安全的。
func (*Control) SetSession ¶
func (c *Control) SetSession(se *Session, w http.ResponseWriter)
SetSession 设置已创建的登录会话。 只能在https时使用。 只要每次调用的w不同,从多个goroutine调用是安全的。
type DB ¶
type DB struct {
// Store 存储验证 [Session] 本身有效的必要信息到数据库,
// 返回false表示ID重复。
Store func(ID string, CreateTime time.Time) bool
// Delete 从数据库删除 [Session] 。
Delete func(ID string)
// Exist 查询是否有指定的 [Session] 。
Exist func(ID string) bool
// Valid 验证 [Session] 表示的用户登录状态有效。
Valid func(UserName string, SessionID string) error
}
DB 包含需要的数据库操作。
从多个goroutine调用里面的字段方法应该是安全的。
type IPInfo ¶
type IPInfo struct {
// Country 是ip属地。
// 正确命名应该是Region,为了向后兼容,所以不修改。
Country string `json:"country"`
}
IPInfo 是ip信息。
type Session ¶
type Session struct {
// ID 对每个登录会话是唯一的。
ID string `gorm:"primaryKey;type:char(64)"`
// CreateTime 是创建登录会话的时间。
CreateTime time.Time
// Ip 是创建登录会话时的ip信息。
Ip IPInfo `json:"-" gorm:"-:all"`
// CSRF_TOKEN 用来防范跨站请求伪造攻击。
CSRF_TOKEN string `json:"-" gorm:"-:all"`
// 下列字段是创建登录会话时的客户端设备信息,
// 和ip信息以及CSRF_TOKEN一起保存在客户浏览器,不在服务器保存。
Os, OsVersion string `json:"-" gorm:"-:all"`
Name string `json:"-" gorm:"-:all"`
Device string `json:"-" gorm:"-:all"`
Broswer string `json:"-" gorm:"-:all"`
}
Session 表示一个登录会话。
Click to show internal directories.
Click to hide internal directories.