Documentation
¶
Index ¶
- Constants
- func CheckAuthRequired(publicPaths []string, urlPath string, urlMethod string) bool
- func GetNanoId(length uint8) 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, userKey string, extraData g.Map) (*UserToken, error)
- func (m *GToken) ParseRequestToken(r *ghttp.Request) string
- 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) (*UserToken, error)
- type UserToken
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 ¶
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
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) ParseRequestToken ¶
ParseRequestToken tries to get token from the following path by priority: 1. header.Authorization 2. token
func (*GToken) RemoveToken ¶
RemoveToken deletes Token
func (*GToken) UseMiddleware ¶
Click to show internal directories.
Click to hide internal directories.