gtoken

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

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

	DefaultExpireIn      = 7 * 24 * time.Hour
	DefaultSecretKey     = "g1t@o3K!e7n"
	DefaultTokenIDLength = 12

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

	DefaultLogPrefix = "[GToken]"

	DefaultPrefixToken = "jwt:"
	DefaultPrefixUser  = "user:"

	PrefixBearer = "Bearer "

	DefaultCodeOK           = 0
	DefaultCodeUnauthorized = 401
)
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 ParseRequestToken added in v1.1.0

func ParseRequestToken(r *ghttp.Request) string

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

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
	SingleSession    bool                                        // if true, only one token can be kept, so the old one will be deleted
	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
	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

func (*GToken) NewToken

func (m *GToken) NewToken(ctx context.Context, userID string, extraData g.Map) (token string, tokenInfo *TokenInfo, err error)

NewToken returns a new 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) (*TokenInfo, error)

ValidateToken returns token info. If AutoRefreshToken is true, refresh token ttl.

type TokenInfo added in v1.1.0

type TokenInfo struct {
	UserID    string      `json:"userID"`
	TokenID   string      `json:"tokenID"`
	ExtraData g.Map       `json:"extraData"`
	ExpireAt  *gtime.Time `json:"ExpireAt"`
	RefreshAt *gtime.Time `json:"RefreshAt"`
}

Jump to

Keyboard shortcuts

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