token

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Index

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 FlagSet

func FlagSet() *pflag.FlagSet

func HasRole

func HasRole(c *gin.Context, role string) bool

HasRole 检查用户是否拥有指定角色

func HasRoles

func HasRoles(c *gin.Context, roles ...string) bool

HasRoles 检查用户是否拥有所有指定角色

func Init

func Init(name string, store Store, data ...Data) gin.HandlerFunc

Init 初始化Session中间件

func New

func New() string

func ReleaseSession

func ReleaseSession(s *Session)

ReleaseSession 释放Session到池中(可选,需要手动调用)

func RoleMW

func RoleMW(roles ...string) gin.HandlerFunc

Types

type Config

type Config struct {
	Name   string `json:"name" yaml:"name"`
	Secret string `json:"secret" yaml:"secret"`
}

func (*Config) FlagSet

func (c *Config) FlagSet() *pflag.FlagSet

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

type DataMap map[string]any

func (DataMap) MarshalBinary

func (d DataMap) MarshalBinary() ([]byte, error)

func (*DataMap) UnmarshalBinary

func (d *DataMap) UnmarshalBinary(buf []byte) error

func (*DataMap) UnmarshalJSON

func (d *DataMap) UnmarshalJSON(buf []byte) error

func (*DataMap) UnmarshalText

func (d *DataMap) UnmarshalText(buf []byte) error

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

func (d *DefaultData) Set(key string, val any) Data

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

func (d *DefaultData) SetValues(k string, v any) Data

func (*DefaultData) State

func (d *DefaultData) State() uint16

func (*DefaultData) Token

func (d *DefaultData) Token() string

type FsData added in v0.1.3

type FsData struct {
	Data   Data      `json:"data"`
	Expire time.Time `json:"expire"`
}

type FsStore

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

func NewFsStore

func NewFsStore(dir string, maxAge ...int) (*FsStore, error)

func (*FsStore) Clear

func (s *FsStore) Clear(ctx context.Context, token string) error

func (*FsStore) Get

func (s *FsStore) Get(ctx context.Context, token string) (Data, error)

func (*FsStore) Save

func (s *FsStore) Save(ctx context.Context, v Data, lifetime ...time.Duration) error

type MemStore

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

func NewMemStore

func NewMemStore(maxAge ...int) *MemStore

func (*MemStore) Clear

func (s *MemStore) Clear(ctx context.Context, token string) error

func (*MemStore) Get

func (s *MemStore) Get(ctx context.Context, token string) (Data, error)

func (*MemStore) Save

func (s *MemStore) Save(ctx context.Context, v Data, lifetime ...time.Duration) error

type RedisStore

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

func NewRedisStore

func NewRedisStore(client redis.UniversalClient, maxAge ...int) (*RedisStore, error)

func (*RedisStore) Clear

func (s *RedisStore) Clear(ctx context.Context, token string) error

func (*RedisStore) Get

func (s *RedisStore) Get(ctx context.Context, token string) (Data, error)

func (*RedisStore) Save

func (s *RedisStore) Save(ctx context.Context, v Data, lifetime ...time.Duration) error

type Session

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

func Default

func Default(c *gin.Context) *Session

Default 获取当前请求的Session

func NewSession

func NewSession(ctx context.Context, store Store, data Data, maxAge ...int) *Session

func NewSessionWithPool

func NewSessionWithPool(ctx context.Context, store Store, data Data, maxAge ...int) *Session

NewSessionWithPool 使用对象池创建Session(可选)

func (*Session) Account

func (s *Session) Account() string

func (*Session) Clear

func (s *Session) Clear() error

Clear 清空session并生成新token

func (*Session) Data

func (s *Session) Data() Data

Data 获取session数据,首次调用时从store加载

func (*Session) Delete

func (s *Session) Delete(key string) error

Delete 删除指定key

func (*Session) Get

func (s *Session) Get(key string) any

func (*Session) HasRole

func (s *Session) HasRole(role string) bool

func (*Session) ID

func (s *Session) ID() uint64

func (*Session) MarshalJSON

func (s *Session) MarshalJSON() ([]byte, error)

func (*Session) Roles

func (s *Session) Roles() []string

func (*Session) Save

func (s *Session) Save(lifetime ...time.Duration) error

Save 保存session数据

func (*Session) Set

func (s *Session) Set(key string, val any)

func (*Session) SetAccount

func (s *Session) SetAccount(val string)

func (*Session) SetHttpOnly added in v0.1.3

func (s *Session) SetHttpOnly(v bool)

func (*Session) SetID

func (s *Session) SetID(val uint64)

func (*Session) SetMaxAge added in v0.1.3

func (s *Session) SetMaxAge(v int)

func (*Session) SetRoles

func (s *Session) SetRoles(roles []string)

func (*Session) SetSecure added in v0.1.3

func (s *Session) SetSecure(v bool)

func (*Session) SetState

func (s *Session) SetState(val uint16)

func (*Session) SetValues

func (s *Session) SetValues(key string, val any)

func (*Session) State

func (s *Session) State() uint16

func (*Session) Token

func (s *Session) Token() string

func (*Session) UnmarshalJSON

func (s *Session) UnmarshalJSON(data []byte) error

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

func (*ShardedMemStore) Get

func (s *ShardedMemStore) Get(ctx context.Context, token string) (Data, error)

Get 获取token数据

func (*ShardedMemStore) Save

func (s *ShardedMemStore) Save(ctx context.Context, v Data, lifetime ...time.Duration) error

Save 保存token数据

type Store

type Store interface {
	Clear(ctx context.Context, v string) error
	Get(ctx context.Context, v string) (Data, error)
	Save(ctx context.Context, v Data, lifetime ...time.Duration) error
}

Jump to

Keyboard shortcuts

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