Documentation ¶
Index ¶
- Variables
- func TemporarySecretKey() []byte
- type CookieReadWriter
- func (rw CookieReadWriter) Destroy(ctx context.Context, hubOption HubOption) (err error)
- func (rw CookieReadWriter) Read(ctx context.Context, hubOption HubOption) (sessionID string, has bool, err error)
- func (rw CookieReadWriter) Write(ctx context.Context, hubOption HubOption, sessionID string) (err error)
- type DefaultSecurity
- type EmptyHttpReadWirter
- func (EmptyHttpReadWirter) Destroy(ctx context.Context, option HubOption) (err error)
- func (EmptyHttpReadWirter) Read(ctx context.Context, hubOption HubOption) (sessionID string, has bool, err error)
- func (EmptyHttpReadWirter) Write(ctx context.Context, hubOption HubOption, sessionID string) (err error)
- type HeaderReadWriter
- func (rw HeaderReadWriter) Destroy(ctx context.Context, option HubOption) (err error)
- func (rw HeaderReadWriter) Read(ctx context.Context, hubOption HubOption) (sessionID string, has bool, err error)
- func (rw HeaderReadWriter) Write(ctx context.Context, hubOption HubOption, sessionID string) (err error)
- type Hub
- func (hub Hub) GetSessionByCookie(ctx context.Context, writer http.ResponseWriter, request *http.Request) (Session, error)
- func (hub Hub) GetSessionByHeader(ctx context.Context, writer http.ResponseWriter, header http.Header) (Session, error)
- func (hub Hub) GetSessionByReadWriter(ctx context.Context, rw SessionHttpReadWriter) (session Session, err error)
- func (hub Hub) GetSessionBySessionID(ctx context.Context, sessionID string) (session Session, sessionExpired bool, err error)
- func (hub Hub) NewSessionID(ctx context.Context) (sessionID string, err error)
- type HubOption
- type HubOptionCookie
- type HubOptionHeader
- type RedisStore
- func (m RedisStore) Delete(ctx context.Context, storeKey string, field string) (err error)
- func (m RedisStore) Destroy(ctx context.Context, storeKey string) (err error)
- func (m RedisStore) Get(ctx context.Context, storeKey string, field string) (value string, hasValue bool, err error)
- func (m RedisStore) InitSession(ctx context.Context, storeKey string, sessionTTL time.Duration) (err error)
- func (m RedisStore) RenewTTL(ctx context.Context, storeKey string, ttl time.Duration) (err error)
- func (m RedisStore) Set(ctx context.Context, storeKey string, field string, value string) (err error)
- func (m RedisStore) StoreKeyExists(ctx context.Context, storeKey string) (existed bool, err error)
- func (m RedisStore) StoreKeyRemainingTTL(ctx context.Context, storeKey string) (remainingTTL time.Duration, err error)
- type RedisStoreOption
- type Security
- type Session
- func (s Session) Delete(ctx context.Context, field string) (err error)
- func (s Session) Destroy(ctx context.Context) (err error)
- func (s Session) Get(ctx context.Context, field string) (value string, hasValue bool, err error)
- func (s Session) ID() (sessionID string)
- func (s Session) SessionRemainingTTL(ctx context.Context) (ttl time.Duration, err error)
- func (s Session) Set(ctx context.Context, field string, value string) (err error)
- type SessionHttpReadWriter
- type Store
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrInvalidBlockSize indicates hash blocksize <= 0. ErrInvalidBlockSize = xerr.New("invalid blocksize, sess.HubOption{}.SecureKey is wrong or someone forged a incorrect session") // ErrInvalidPKCS7Data indicates bad input to PKCS7 pad or unpad. ErrInvalidPKCS7Data = xerr.New("invalid PKCS7 data (empty or not padded), sess.HubOption{}.SecureKey is wrong or someone forged a incorrect session") // ErrInvalidPKCS7Padding indicates PKCS7 unpad fails to bad input. ErrInvalidPKCS7Padding = xerr.New("invalid padding on input, sess.HubOption{}.SecureKey is wrong or someone forged a incorrect session") )
Functions ¶
func TemporarySecretKey ¶
func TemporarySecretKey() []byte
仅限于演示代码时使用的秘钥生成函数,正式环境请自行生成 长度为 32 的 []byte,并保存在配置文件或配置中心中。
Types ¶
type CookieReadWriter ¶
type CookieReadWriter struct { Writer http.ResponseWriter Request *http.Request }
func (CookieReadWriter) Destroy ¶
func (rw CookieReadWriter) Destroy(ctx context.Context, hubOption HubOption) (err error)
type DefaultSecurity ¶
type DefaultSecurity struct{}
type EmptyHttpReadWirter ¶
type EmptyHttpReadWirter struct{}
func (EmptyHttpReadWirter) Destroy ¶
func (EmptyHttpReadWirter) Destroy(ctx context.Context, option HubOption) (err error)
type HeaderReadWriter ¶
type HeaderReadWriter struct { Writer http.ResponseWriter // 故意将 HeaderReadWriter 设计成需要 Header 而不是 *http.Request // 目的是避免吧 sessHub.GetSessionByHeader() 当做 sessHub.GetSessionByCookie() 使用 Header http.Header }
func (HeaderReadWriter) Destroy ¶
func (rw HeaderReadWriter) Destroy(ctx context.Context, option HubOption) (err error)
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub 利用 Store 生成 sessionID 和查找 Session
func (Hub) GetSessionByCookie ¶
func (Hub) GetSessionByHeader ¶
func (Hub) GetSessionByReadWriter ¶
func (Hub) GetSessionBySessionID ¶
type HubOption ¶
type HubOption struct { // (必填) sessionID 与 storeKey 的加密解密秘钥 设置长度为 32 的 []byte SecureKey []byte // cookie 相关设置 Cookie HubOptionCookie // sesison 过期时间,默认8小时 SessionTTL time.Duration // header 相关设置 Header HubOptionHeader // header 相关设置 // 加密方式,不填则为 goclub/sesion 默认 aes 加密 Security Security // 当sessionID 解码为 storeKey 后在 store 中不存在时触发 // 用于监控系统排查恶意攻击或 sessionID 过期 // ctx 可以用 ctx.WithValue 传递 requestID 便于排查问题 OnStoreKeyDoesNotExist func(ctx context.Context, sessionID string, storeKey string) // 当请求的 sessionID 为空字符串时触发 // 用于监控系统排查问题 // ctx 可以用 ctx.WithValue 传递 requestID 便于排查问题 OnRequestSessionIDIsEmptyString func(ctx context.Context) }
type HubOptionCookie ¶
type HubOptionHeader ¶
type HubOptionHeader struct { // Key 建议设置为 session (若留空则为 session) Key string }
type RedisStore ¶
type RedisStore struct {
// contains filtered or unexported fields
}
func NewRedisStore ¶
func NewRedisStore(option RedisStoreOption) RedisStore
func (RedisStore) Destroy ¶
func (m RedisStore) Destroy(ctx context.Context, storeKey string) (err error)
func (RedisStore) InitSession ¶
func (RedisStore) StoreKeyExists ¶
func (RedisStore) StoreKeyRemainingTTL ¶
type RedisStoreOption ¶
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
func (Session) SessionRemainingTTL ¶
type SessionHttpReadWriter ¶
type Store ¶
type Store interface { InitSession(ctx context.Context, storeKey string, sessionTTL time.Duration) (err error) StoreKeyExists(ctx context.Context, storeKey string) (existed bool, err error) StoreKeyRemainingTTL(ctx context.Context, storeKey string) (remainingTTL time.Duration, err error) RenewTTL(ctx context.Context, storeKey string, ttl time.Duration) (err error) Get(ctx context.Context, storeKey string, field string) (value string, hasValue bool, err error) Set(ctx context.Context, storeKey string, field string, value string) (err error) Delete(ctx context.Context, storeKey string, field string) (err error) Destroy(ctx context.Context, storeKey string) (err error) }
满足 Store 接口的结构体可作为 goclub/sessoin 的数据层 已经封装好的有 sess.NewRedisStore()
Source Files ¶
Click to show internal directories.
Click to hide internal directories.