gtoken

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2025 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CacheModeCache   = 0
	CacheModeRedis   = 1
	CacheModeFile    = 2
	CacheModeFileDat = "gtoken.dat"

	DefaultExpireIn      = 7 * 24 * time.Hour
	DefaultEncryptKey    = "g1t@o3K!e7n"
	DefaultTokenIdLength = 12

	TokenKeyInRequest = "token" // ok for: router, query, body, form, custom

	DefaultLogPrefix = "[GToken]"
)
View Source
const (
	LogLevelDebug   = "debug"
	LogLevelInfo    = "info"
	LogLevelWarning = "warning"
	LogLevelError   = "error"
)

Variables

This section is empty.

Functions

func CheckAuthRequired

func CheckAuthRequired(publicPaths []string, urlPath string, urlMethod string) bool

func GetNanoId

func GetNanoId(length uint8) string

func WriteLog

func WriteLog(ctx context.Context, msg string, loglevel string)

Types

type DefaultResponse

type DefaultResponse struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

type GToken

type GToken struct {
	CacheMode        uint8                                       // 0 cache (default) 1 redis 2 file
	ExpireIn         time.Duration                               // how long a token will be invalid
	AutoRefreshToken bool                                        // whether refresh a token automatically. It is a big risk to use "true" in production.
	SecretKey        []byte                                      // jwt secret key, why use []byte: https://golang-jwt.github.io/jwt/usage/signing_methods/#frequently-asked-questions
	TokenIdLength    uint8                                       // length of NanoId, default 12
	MultiLogin       bool                                        // To be simple, if true, will return the same token while token is still valid
	PublicPaths      []string                                    // Non-auth paths. Support restful formats like "POST:/login".
	DoBeforeAuth     func(r *ghttp.Request) (ok bool)            // Generally, we omit the file requests in this func
	DoAfterAuth      func(r *ghttp.Request, ok bool, data g.Map) // Generally, we add info into context in this func
}

func (*GToken) Init

func (m *GToken) Init(ctx context.Context) bool

Init 初始化配置信息

func (*GToken) NewToken

func (m *GToken) NewToken(ctx context.Context, userKey string, extraData g.Map) (*UserToken, error)

NewToken returns a new token

func (*GToken) ParseRequestToken

func (m *GToken) ParseRequestToken(r *ghttp.Request) string

ParseRequestToken tries to get token from the following path by priority: 1. header.Authorization 2. token

func (*GToken) RemoveToken

func (m *GToken) RemoveToken(ctx context.Context, token string) (ok bool, err error)

RemoveToken deletes Token

func (*GToken) UseMiddleware

func (m *GToken) UseMiddleware(ctx context.Context, group *ghttp.RouterGroup) error

func (*GToken) ValidateToken

func (m *GToken) ValidateToken(ctx context.Context, token string) (*UserToken, error)

ValidateToken returns a valid token or nil

type UserToken

type UserToken struct {
	ID        string      `json:"id"`
	UserKey   string      `json:"userKey"`
	Token     string      `json:"token"`
	CreateAt  *gtime.Time `json:"createAt"`
	RefreshAt *gtime.Time `json:"refreshAt"`
	ExpireAt  *gtime.Time `json:"expireAt"`
	ExtraData g.Map       `json:"extraData"`
}

Jump to

Keyboard shortcuts

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