Documentation ¶
Index ¶
- Constants
- Variables
- func GetGUID() (valueGUID string)
- func NewRedisSessionSentinelModeConfig(redisHost string, redisDB int, redisPass string, masterName string) *kv.MyRedisConf
- func NewRedisSessionSingleModeConfig(redisHost string, redisDB int, redisPass string) *kv.MyRedisConf
- func SI(s string) (i int64)
- type GetUserInfoFunc
- type JwtData
- type JwtManage
- type RedisSession
- func (s *RedisSession) AddUser(userId string, userInfoValidTimes int64) (user *User, exist bool, err error)
- func (s *RedisSession) CheckToken(token string) (user *User, exist bool, err error)
- func (s *RedisSession) CheckTokenOrUpdateUser(token string, userInfoValidTimes int64) (user *User, exist bool, err error)
- func (s *RedisSession) ConfigDefaultExpireTime(second int64) TokenManage
- func (s *RedisSession) ConfigGetUserInfoFunc(fn GetUserInfoFunc) TokenManage
- func (s *RedisSession) ConfigTokenKeyPrefix(tokenKey string) TokenManage
- func (s *RedisSession) ConfigUserKeyPrefix(userKey string) TokenManage
- func (s *RedisSession) DeleteToken(token string) (err error)
- func (s *RedisSession) DeleteUser(userId string) (err error)
- func (s *RedisSession) DeleteUserToken(userId string) (err error)
- func (s *RedisSession) ListUserToken(userId string) ([]string, error)
- func (s *RedisSession) RefreshToken(token string, tokenValidTimes int64) (err error)
- func (s *RedisSession) RefreshUser(ids []string, userInfoValidTimes int64) (err error)
- func (s *RedisSession) SetSingleMode() TokenManage
- func (s *RedisSession) SetToken(useId string, tokenValidTimes int64) (token string, err error)
- type TokenManage
- func NewRedisSession(redisConf *kv.MyRedisConf) (TokenManage, error)
- func NewRedisSessionAll(redisConf *kv.MyRedisConf, tokenKey, userKey string, expireTime int64, ...) (TokenManage, error)
- func NewRedisSessionSimple(redisHost string, redisDB int, redisPass string) (TokenManage, error)
- func NewRedisSessionWithPool(pool *redis.Pool) (TokenManage, error)
- type User
Constants ¶
const ( // JwtPayloadClientVersionName Todo client jwtPayload version field JwtPayloadClientVersionName = "clientVersion" // JwtPayloadClientVersion client jwtPayload version value JwtPayloadClientVersion = 1.0 )
Variables ¶
var ( // TokenMapKeyExpireTime list all token, hash key expire TokenMapKeyExpireTime int64 = 3600 * 24 * 30 )
Functions ¶
func NewRedisSessionSentinelModeConfig ¶
func NewRedisSessionSentinelModeConfig(redisHost string, redisDB int, redisPass string, masterName string) *kv.MyRedisConf
NewRedisSessionSentinelModeConfig redis sentinel mode config redisHost is sentinel address, not redis address redisPass is redis password
func NewRedisSessionSingleModeConfig ¶
func NewRedisSessionSingleModeConfig(redisHost string, redisDB int, redisPass string) *kv.MyRedisConf
NewRedisSessionSingleModeConfig redis single mode config
Types ¶
type GetUserInfoFunc ¶
GetUserInfoFunc func get user info from where
type JwtData ¶ added in v1.0.13
type JwtData struct { // 用户ID UserId string `json:"user_id,omitempty"` // 创建时间,毫秒 CreateMSTime int64 `json:"create_ms_time,omitempty"` // 辅助 CreateString string `json:"create_string,omitempty"` // 过期时间,毫秒 ExpiryMSTime int64 `json:"expiry_ms_time,omitempty"` // 辅助 ExpiryString string `json:"expiry_string,omitempty"` // 是否已过期 IsExpire bool `json:"is_expire,omitempty"` // 服务端数据库令牌对ID,可忽视 ServerTokenHandle string `json:"server_token_handle,omitempty"` // 客户端Payload数据,返回给客户端的数据,客户端解密可看 ClientPayload map[string]interface{} `json:"client_payload,omitempty"` // 服务端Session数据,保存在服务器端的数据,客户端不可看 ServerSessionData map[string]interface{} `json:"server_session_data,omitempty"` }
JwtData 数据
type JwtManage ¶ added in v1.0.13
type JwtManage interface { // Config 配置令牌有效时间 Config(refreshTokenExpireTime time.Duration, accessTokenExpireTime time.Duration) // CreateNewLogInToken 创建令牌对,可传入客户端或服务端数据 CreateNewLogInToken(userId string, clientJwtPayload map[string]interface{}, serverSessionData map[string]interface{}) (accessToken, refreshToken string, err error) // GetSessionInfoByAccessToken 获取某令牌对信息,先走JWT无状态解码数据,force则强制查询服务端 GetSessionInfoByAccessToken(accessToken string, force bool) (jwtData JwtData, err error) // GetAllSessionsForUser 获取某用户所有令牌对信息 GetAllSessionsForUser(userId string) (jwtDataList []JwtData, err error) // RevokeLogInTokenByUserId 撤销某用户所有令牌对 RevokeLogInTokenByUserId(userId string) error // RevokeLogInTokenByAccessToken 撤销用户某令牌对 RevokeLogInTokenByAccessToken(accessToken string) (revoke bool, err error) // RefreshLogInToken 刷新令牌对 RefreshLogInToken(refreshToken string) (newAccessToken string, newRefreshToken string, err error) // GetSignPublicKey 获取服务端签名key,客户端使用这个来进行解密,可以是空 GetSignPublicKey() (publicKey string, err error) // ParseAccessToken 解析和验证JWT信息,客户端方法 ParseAccessToken(accessToken string, publicKey string) (jwtData JwtData, err error) }
JwtManage Todo 另外一种实现的 Session
type RedisSession ¶
type RedisSession struct {
// contains filtered or unexported fields
}
RedisSession session by redis
func (*RedisSession) AddUser ¶
func (s *RedisSession) AddUser(userId string, userInfoValidTimes int64) (user *User, exist bool, err error)
AddUser Add the user info to cache,expire after some second
func (*RedisSession) CheckToken ¶
func (s *RedisSession) CheckToken(token string) (user *User, exist bool, err error)
func (*RedisSession) CheckTokenOrUpdateUser ¶
func (s *RedisSession) CheckTokenOrUpdateUser(token string, userInfoValidTimes int64) (user *User, exist bool, err error)
CheckTokenOrUpdateUser Check the token, when cache database exist return user info directly, others hit the persistent database and save newest user in cache database then return. such redis check, not check load from mysql. you can check user info by that token, if s.getUserFunc == nil do nothing
func (*RedisSession) ConfigDefaultExpireTime ¶
func (s *RedisSession) ConfigDefaultExpireTime(second int64) TokenManage
ConfigDefaultExpireTime config by chain
func (*RedisSession) ConfigGetUserInfoFunc ¶
func (s *RedisSession) ConfigGetUserInfoFunc(fn GetUserInfoFunc) TokenManage
ConfigGetUserInfoFunc config by chain
func (*RedisSession) ConfigTokenKeyPrefix ¶
func (s *RedisSession) ConfigTokenKeyPrefix(tokenKey string) TokenManage
ConfigTokenKeyPrefix config by chain
func (*RedisSession) ConfigUserKeyPrefix ¶
func (s *RedisSession) ConfigUserKeyPrefix(userKey string) TokenManage
ConfigUserKeyPrefix config by chain
func (*RedisSession) DeleteToken ¶
func (s *RedisSession) DeleteToken(token string) (err error)
DeleteToken Delete token when you do action such logout
func (*RedisSession) DeleteUser ¶
func (s *RedisSession) DeleteUser(userId string) (err error)
DeleteUser Delete user info in cache
func (*RedisSession) DeleteUserToken ¶
func (s *RedisSession) DeleteUserToken(userId string) (err error)
DeleteUserToken Delete all token of this user
func (*RedisSession) ListUserToken ¶
func (s *RedisSession) ListUserToken(userId string) ([]string, error)
ListUserToken List all token in one user
func (*RedisSession) RefreshToken ¶
func (s *RedisSession) RefreshToken(token string, tokenValidTimes int64) (err error)
RefreshToken Refresh token,token expire will be again after some second
func (*RedisSession) RefreshUser ¶
func (s *RedisSession) RefreshUser(ids []string, userInfoValidTimes int64) (err error)
RefreshUser Refresh cache of user info batch
func (*RedisSession) SetSingleMode ¶
func (s *RedisSession) SetSingleMode() TokenManage
SetSingleMode set single mode, new token will destroy other token
type TokenManage ¶
type TokenManage interface { SetToken(userId string, tokenValidTimes int64) (token string, err error) // Set token, expire after some second RefreshToken(token string, tokenValidTimes int64) error // Refresh token,token expire will be again after some second DeleteToken(token string) error // Delete token when you do action such logout CheckToken(token string) (user *User, exist bool, err error) // Check the token, but not refresh user info cache CheckTokenOrUpdateUser(token string, userInfoValidTimes int64) (user *User, exist bool, err error) // Check the token, when cache database exist return user info directly, others hit the persistent database and save newest user in cache database then return. such redis check, not check load from mysql. ListUserToken(userId string) ([]string, error) // List all token of one user DeleteUserToken(userId string) error // Delete all token of this user RefreshUser(userId []string, userInfoValidTimes int64) error // Refresh cache of user info batch DeleteUser(userId string) error // Delete user info in cache AddUser(userId string, userInfoValidTimes int64) (user *User, exist bool, err error) // Add the user info to cache,expire after some second ConfigTokenKeyPrefix(tokenKey string) TokenManage // Config chain, just cache key prefix ConfigUserKeyPrefix(userKey string) TokenManage // Config chain, just cache key prefix ConfigDefaultExpireTime(second int64) TokenManage // Config chain, token expire after second ConfigGetUserInfoFunc(fn GetUserInfoFunc) TokenManage // Config chain, when cache not found user info, will load from this func SetSingleMode() TokenManage // Can set single mode, before one new token gen, will destroy other token }
TokenManage token manage token will be put in cache database such redis and user info relate with that token will cache too
func NewRedisSession ¶
func NewRedisSession(redisConf *kv.MyRedisConf) (TokenManage, error)
NewRedisSession new a redis session with redisConf config
func NewRedisSessionAll ¶
func NewRedisSessionAll(redisConf *kv.MyRedisConf, tokenKey, userKey string, expireTime int64, getUserInfoFunc GetUserInfoFunc) (TokenManage, error)
NewRedisSessionAll new a redis session, config all define prefix of token and user key
func NewRedisSessionSimple ¶ added in v1.0.13
func NewRedisSessionSimple(redisHost string, redisDB int, redisPass string) (TokenManage, error)
NewRedisSessionSimple new a redis session with simple config
func NewRedisSessionWithPool ¶
func NewRedisSessionWithPool(pool *redis.Pool) (TokenManage, error)
NewRedisSessionWithPool new a redis session by redis pool
type User ¶
type User struct { Id string `json:"id"` // unique mark TokenRemainLiveTime int64 `json:"-"` // token remain live time in cache TokenExpireTime int64 `json:"-"` // when token expire Token string `json:"-"` // this token Detail interface{} `json:"detail"` // can diy your real user info by config ConfigGetUserInfoFunc() }
User core user info, it's Id will be the primary key store in cache database such redis