token_auth

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2024 License: MIT Imports: 11 Imported by: 0

README

鉴权中间件

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnauthorized        = httputils.NewUnauthorizedError(-1, "未登录")
	ErrTokenInvalid        = httputils.NewUnauthorizedError(-2, "token失效")
	ErrTokenExpired        = httputils.NewUnauthorizedError(-3, "token过期")
	ErrRefreshTokenInvalid = httputils.NewUnauthorizedError(-4, "refresh_token失效")
	ErrRefreshTokenExpired = httputils.NewUnauthorizedError(-5, "refresh_token过期")
)

Functions

This section is empty.

Types

type AccessToken

type AccessToken struct {
	AccessToken  string            `json:"access_token"`
	RefreshToken string            `json:"refresh_token"`
	KvMap        map[string]string `json:"kv_map"`
	ExpireAt     int64             `json:"expire_at"`
	RefreshAt    int64             `json:"refresh_at"`
}

type Option

type Option func(*Options)

func WithGetAccessToken

func WithGetAccessToken(getToken func(ctx *gin.Context) (string, error)) Option

func WithHandlerError

func WithHandlerError(handlerError func(ctx *gin.Context, err error) error) Option

func WithLock

func WithLock(lock sync.Locker) Option

func WithSetContext

func WithSetContext(setContext func(ctx context.Context, accessToken *AccessToken) (context.Context, error)) Option

func WithStorage

func WithStorage(storage Storage) Option

func WithTokenExpired

func WithTokenExpired(tokenExpired int64, refreshExpired int64) Option

type Options

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

type Storage

type Storage interface {
	Save(ctx context.Context, accessToken *AccessToken) error
	FindByAccessToken(ctx context.Context, accessToken string) (*AccessToken, error)
	FindByRefreshToken(ctx context.Context, refreshToken string) (*AccessToken, error)
	Delete(ctx context.Context, accessToken *AccessToken) error
}

func NewCacheStorage

func NewCacheStorage(cache cache.Cache, prefix string) Storage

type TokenAuth

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

func NewTokenAuth

func NewTokenAuth(option ...Option) *TokenAuth

func (*TokenAuth) Delete

func (t *TokenAuth) Delete(ctx context.Context, accessToken *AccessToken) error

func (*TokenAuth) Generate

func (t *TokenAuth) Generate(ctx context.Context) *AccessToken

func (*TokenAuth) Get

func (t *TokenAuth) Get(ctx context.Context) *AccessToken

func (*TokenAuth) Middleware

func (t *TokenAuth) Middleware(force bool) gin.HandlerFunc

Middleware 中间件 force 是否强制要求鉴权

func (*TokenAuth) Refresh

func (t *TokenAuth) Refresh(ctx context.Context, refreshToken string) (*AccessToken, error)

func (*TokenAuth) Save

func (t *TokenAuth) Save(ctx context.Context, accessToken *AccessToken) error

Jump to

Keyboard shortcuts

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