Documentation
¶
Index ¶
- Constants
- func CheckAuthRequired(publicPaths []string, urlPath string, urlMethod string) bool
- func ParseRequestToken(r *ghttp.Request) string
- func WriteLog(ctx context.Context, msg string, logLevel string)
- type DefaultResponse
- type GToken
- func (m *GToken) Init(ctx context.Context) bool
- func (m *GToken) NewToken(ctx context.Context, userID string, extraData g.Map) (token string, tokenInfo *TokenInfo, err error)
- func (m *GToken) RemoveToken(ctx context.Context, token string) (ok bool, err error)
- func (m *GToken) UseMiddleware(ctx context.Context, group *ghttp.RouterGroup) error
- func (m *GToken) ValidateToken(ctx context.Context, token string) (*TokenInfo, error)
- type TokenInfo
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 )
View Source
const ( LogLevelDebug = "debug" LogLevelInfo = "info" LogLevelWarning = "warning" LogLevelError = "error" )
Variables ¶
This section is empty.
Functions ¶
func CheckAuthRequired ¶
func ParseRequestToken ¶ added in v1.1.0
ParseRequestToken tries to get token from the following path by priority: 1. header.Authorization 2. token
Types ¶
type DefaultResponse ¶
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) 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 ¶
RemoveToken deletes Token
func (*GToken) UseMiddleware ¶
Click to show internal directories.
Click to hide internal directories.