Documentation
¶
Index ¶
- Constants
- Variables
- func AuthMW() gin.HandlerFunc
- func FlagSet() *pflag.FlagSet
- func HasRole(c *gin.Context, role string) bool
- func HasRoles(c *gin.Context, roles ...string) bool
- func Init(name string, store Store, data ...Data) gin.HandlerFunc
- func New() string
- func ReleaseSession(s *Session)
- func RoleMW(roles ...string) gin.HandlerFunc
- type Config
- type Data
- type DataMap
- type DataStringSlice
- type DefaultData
- func (d *DefaultData) Account() string
- func (d *DefaultData) Clear() Data
- func (d *DefaultData) Delete(key string) Data
- func (d *DefaultData) Get(key string) any
- func (d *DefaultData) ID() uint64
- func (d *DefaultData) Items() DataMap
- func (d *DefaultData) New() string
- func (d *DefaultData) Roles() []string
- func (d *DefaultData) Set(key string, val any) Data
- func (d *DefaultData) SetAccount(v string) Data
- func (d *DefaultData) SetID(v uint64) Data
- func (d *DefaultData) SetRoles(v []string) Data
- func (d *DefaultData) SetState(v uint16) Data
- func (d *DefaultData) SetToken(v string) Data
- func (d *DefaultData) SetValues(k string, v any) Data
- func (d *DefaultData) State() uint16
- func (d *DefaultData) Token() string
- type FsData
- type FsStore
- type MemStore
- type RedisStore
- type Session
- func (s *Session) Account() string
- func (s *Session) Clear() error
- func (s *Session) Data() Data
- func (s *Session) Delete(key string) error
- func (s *Session) Get(key string) any
- func (s *Session) HasRole(role string) bool
- func (s *Session) ID() uint64
- func (s *Session) MarshalJSON() ([]byte, error)
- func (s *Session) Roles() []string
- func (s *Session) Save(lifetime ...time.Duration) error
- func (s *Session) Set(key string, val any)
- func (s *Session) SetAccount(val string)
- func (s *Session) SetHttpOnly(v bool)
- func (s *Session) SetID(val uint64)
- func (s *Session) SetMaxAge(v int)
- func (s *Session) SetRoles(roles []string)
- func (s *Session) SetSecure(v bool)
- func (s *Session) SetState(val uint16)
- func (s *Session) SetValues(key string, val any)
- func (s *Session) State() uint16
- func (s *Session) Token() string
- func (s *Session) UnmarshalJSON(data []byte) error
- type ShardedMemStore
- type Store
Constants ¶
View Source
const ( DefaultKey = "github.com/mulan-ext/auth" TokenKey = "github.com/mulan-ext/auth/token" CtxKeyID = "id" CtxKeyAccount = "account" CtxKeyState = "state" CtxKeyRoles = "roles" CtxKeyIsAdmin = "is_admin" RoleAdmin = "admin" )
View Source
const ( // DefaultMaxAge 默认过期时间: 7天 DefaultMaxAge = 7 * 24 * 60 * 60 // DefaultKeyPrefix 默认key前缀 DefaultKeyPrefix = "ginx:auth:token:" )
View Source
const ( // DefaultFilePrefix 默认文件前缀 DefaultFilePrefix = "ginx_auth_token_" // DefaultFileMode 默认文件权限 DefaultFileMode = 0644 // DefaultDirMode 默认目录权限 DefaultDirMode = 0755 )
Variables ¶
View Source
var ( // ErrTokenNotFound token不存在 ErrTokenNotFound = errors.New("token not found") // ErrTokenExpired token已过期 ErrTokenExpired = errors.New("token expired") )
Functions ¶
func AuthMW ¶
func AuthMW() gin.HandlerFunc
func RoleMW ¶
func RoleMW(roles ...string) gin.HandlerFunc
Types ¶
type Config ¶
type Data ¶
type Data interface {
Token() string
ID() uint64
Account() string
State() uint16
Roles() []string
Items() DataMap
Get(string) any
New() string
Set(string, any) Data
SetToken(string) Data
SetID(uint64) Data
SetAccount(string) Data
SetState(uint16) Data
SetValues(string, any) Data
SetRoles([]string) Data
Delete(string) Data
Clear() Data
}
type DataMap ¶
func (DataMap) MarshalBinary ¶
func (*DataMap) UnmarshalBinary ¶
func (*DataMap) UnmarshalJSON ¶
func (*DataMap) UnmarshalText ¶
type DataStringSlice ¶
type DataStringSlice []string
func (DataStringSlice) MarshalBinary ¶
func (d DataStringSlice) MarshalBinary() ([]byte, error)
func (*DataStringSlice) UnmarshalBinary ¶
func (d *DataStringSlice) UnmarshalBinary(buf []byte) error
func (*DataStringSlice) UnmarshalJSON ¶
func (d *DataStringSlice) UnmarshalJSON(buf []byte) error
func (*DataStringSlice) UnmarshalText ¶
func (d *DataStringSlice) UnmarshalText(buf []byte) error
type DefaultData ¶
type DefaultData struct {
Items_ DataMap `json:"items" redis:"items"`
Token_ string `json:"token" redis:"token"`
Account_ string `json:"account" redis:"account"`
Roles_ DataStringSlice `json:"roles" redis:"roles"`
ID_ uint64 `json:"id" redis:"id"`
State_ uint16 `json:"state" redis:"state"`
// contains filtered or unexported fields
}
func (*DefaultData) Account ¶
func (d *DefaultData) Account() string
func (*DefaultData) Clear ¶
func (d *DefaultData) Clear() Data
func (*DefaultData) Delete ¶
func (d *DefaultData) Delete(key string) Data
func (*DefaultData) Get ¶
func (d *DefaultData) Get(key string) any
func (*DefaultData) ID ¶
func (d *DefaultData) ID() uint64
func (*DefaultData) Items ¶
func (d *DefaultData) Items() DataMap
func (*DefaultData) New ¶
func (d *DefaultData) New() string
func (*DefaultData) Roles ¶
func (d *DefaultData) Roles() []string
func (*DefaultData) SetAccount ¶
func (d *DefaultData) SetAccount(v string) Data
func (*DefaultData) SetID ¶
func (d *DefaultData) SetID(v uint64) Data
func (*DefaultData) SetRoles ¶
func (d *DefaultData) SetRoles(v []string) Data
func (*DefaultData) SetState ¶
func (d *DefaultData) SetState(v uint16) Data
func (*DefaultData) SetToken ¶
func (d *DefaultData) SetToken(v string) Data
func (*DefaultData) State ¶
func (d *DefaultData) State() uint16
func (*DefaultData) Token ¶
func (d *DefaultData) Token() string
type MemStore ¶
type MemStore struct {
// contains filtered or unexported fields
}
func NewMemStore ¶
type RedisStore ¶
type RedisStore struct {
// contains filtered or unexported fields
}
func NewRedisStore ¶
func NewRedisStore(client redis.UniversalClient, maxAge ...int) (*RedisStore, error)
type Session ¶
type Session struct {
IsNil bool
// contains filtered or unexported fields
}
func NewSession ¶
func NewSessionWithPool ¶
NewSessionWithPool 使用对象池创建Session(可选)
func (*Session) MarshalJSON ¶
func (*Session) SetAccount ¶
func (*Session) SetHttpOnly ¶ added in v0.1.3
func (*Session) UnmarshalJSON ¶
type ShardedMemStore ¶
type ShardedMemStore struct {
// contains filtered or unexported fields
}
ShardedMemStore 分片内存存储,用于提升并发写性能
func NewShardedMemStore ¶
func NewShardedMemStore(shardCount int, maxAge ...int) *ShardedMemStore
NewShardedMemStore 创建分片内存存储 shardCount 必须是2的幂(4, 8, 16, 32...)
func (*ShardedMemStore) Clear ¶
func (s *ShardedMemStore) Clear(ctx context.Context, token string) error
Clear 清除token
Click to show internal directories.
Click to hide internal directories.