sess

package module
v0.0.0-...-272e75f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2023 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

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)

func (CookieReadWriter) Read

func (rw CookieReadWriter) Read(ctx context.Context, hubOption HubOption) (sessionID string, has bool, err error)

func (CookieReadWriter) Write

func (rw CookieReadWriter) Write(ctx context.Context, hubOption HubOption, sessionID string) (err error)

type DefaultSecurity

type DefaultSecurity struct{}

func (DefaultSecurity) Decrypt

func (DefaultSecurity) Decrypt(sessionID []byte, securityKey []byte) (storeKey []byte, err error)

func (DefaultSecurity) Encrypt

func (DefaultSecurity) Encrypt(storeKey []byte, securityKey []byte) (sessionID []byte, err error)

type EmptyHttpReadWirter

type EmptyHttpReadWirter struct{}

func (EmptyHttpReadWirter) Destroy

func (EmptyHttpReadWirter) Destroy(ctx context.Context, option HubOption) (err error)

func (EmptyHttpReadWirter) Read

func (EmptyHttpReadWirter) Read(ctx context.Context, hubOption HubOption) (sessionID string, has bool, err error)

func (EmptyHttpReadWirter) Write

func (EmptyHttpReadWirter) Write(ctx context.Context, hubOption HubOption, sessionID string) (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)

func (HeaderReadWriter) Read

func (rw HeaderReadWriter) Read(ctx context.Context, hubOption HubOption) (sessionID string, has bool, err error)

func (HeaderReadWriter) Write

func (rw HeaderReadWriter) Write(ctx context.Context, hubOption HubOption, sessionID string) (err error)

type Hub

type Hub struct {
	// contains filtered or unexported fields
}

Hub 利用 Store 生成 sessionID 和查找 Session

func NewHub

func NewHub(store Store, option HubOption) (hub *Hub, err error)

func (Hub) GetSessionByCookie

func (hub Hub) GetSessionByCookie(ctx context.Context, writer http.ResponseWriter, request *http.Request) (Session, error)

func (Hub) GetSessionByHeader

func (hub Hub) GetSessionByHeader(ctx context.Context, writer http.ResponseWriter, header http.Header) (Session, error)

func (Hub) GetSessionByReadWriter

func (hub Hub) GetSessionByReadWriter(ctx context.Context, rw SessionHttpReadWriter) (session Session, err error)

func (Hub) GetSessionBySessionID

func (hub Hub) GetSessionBySessionID(ctx context.Context, sessionID string) (session Session, sessionExpired bool, err error)

func (Hub) NewSessionID

func (hub Hub) NewSessionID(ctx context.Context) (sessionID string, err error)

NewSessionID 微信小程序和 app 场景下可能在登录成功时可能需要手动创建 SessionID 所以提供 NewSessionID 发放

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 HubOptionCookie struct {
	// Name 默认为session_id, 建议设置为 项目名 + "_session_id"
	Name string
	// Path 默认为 "/"
	Path   string
	Domain string
	// Path 默认为 int(HubOption{}.SessionTTL.Seconds())
	MaxAge int
	Secure bool
}

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) Delete

func (m RedisStore) Delete(ctx context.Context, storeKey string, field string) (err error)

func (RedisStore) Destroy

func (m RedisStore) Destroy(ctx context.Context, storeKey string) (err error)

func (RedisStore) Get

func (m RedisStore) Get(ctx context.Context, storeKey string, field string) (value string, hasValue bool, err error)

func (RedisStore) InitSession

func (m RedisStore) InitSession(ctx context.Context, storeKey string, sessionTTL time.Duration) (err error)

func (RedisStore) RenewTTL

func (m RedisStore) RenewTTL(ctx context.Context, storeKey string, ttl time.Duration) (err error)

func (RedisStore) Set

func (m RedisStore) Set(ctx context.Context, storeKey string, field string, value string) (err error)

func (RedisStore) StoreKeyExists

func (m RedisStore) StoreKeyExists(ctx context.Context, storeKey string) (existed bool, err error)

func (RedisStore) StoreKeyRemainingTTL

func (m RedisStore) StoreKeyRemainingTTL(ctx context.Context, storeKey string) (remainingTTL time.Duration, err error)

type RedisStoreOption

type RedisStoreOption struct {
	Client         red.Connecter
	StoreKeyPrefix string
}

type Security

type Security interface {
	Encrypt(storeKey []byte, securityKey []byte) (sessionID []byte, err error)
	Decrypt(sessionID []byte, securityKey []byte) (storeKey []byte, err error)
}

type Session

type Session struct {
	// contains filtered or unexported fields
}

func (Session) Delete

func (s Session) Delete(ctx context.Context, field string) (err error)

func (Session) Destroy

func (s Session) Destroy(ctx context.Context) (err error)

func (Session) Get

func (s Session) Get(ctx context.Context, field string) (value string, hasValue bool, err error)

func (Session) ID

func (s Session) ID() (sessionID string)

func (Session) SessionRemainingTTL

func (s Session) SessionRemainingTTL(ctx context.Context) (ttl time.Duration, err error)

func (Session) Set

func (s Session) Set(ctx context.Context, field string, value string) (err error)

type SessionHttpReadWriter

type SessionHttpReadWriter interface {
	Read(ctx context.Context, hubOption HubOption) (sessionID string, has bool, err error)
	Write(ctx context.Context, hubOption HubOption, sessionID string) (err error)
	Destroy(ctx context.Context, option HubOption) (err error)
}

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()

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL