Documentation ¶
Index ¶
- func Marshal(userID string, userName string, roleID uint64, expiredAt time.Time, ...) (string, error)
- func TokenHandler(secretKey []byte, cookieName string, contextBasicMethodKey string, ...) (http.HandlerFunc, error)
- func Unmarshal(data string, secretKey []byte) (userID string, userName string, roleID uint64, expiredAt time.Time, err error)
- type Token
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Marshal ¶
func Marshal(userID string, userName string, roleID uint64, expiredAt time.Time, secretKey []byte) (string, error)
Marshal creates a token string from the user id, user name, role id, and expiration time. The token string is encrypted with the secret key and encoded in base64.
func TokenHandler ¶
func TokenHandler( secretKey []byte, cookieName string, contextBasicMethodKey string, contextBearerMethodKey string, nextFunc http.HandlerFunc, ) (http.HandlerFunc, error)
TokenHandler is a middleware that extracts the user ID from the request and adds it to the request context. The token is extracted from the cookie and the header.
- secretKey: the secret key used to crypt and decrypt the token.
- cookieName: the name of the cookie that contains the token.
- contextBasicMethodKey: the key used to store the basic method token in the context.
- contextBearerMethodKey: the key used to store the bearer method token in the context.
- nextFunc: the next handler in the chain.
IMPORTANT: does not return an error if the user ID is not found.
func Unmarshal ¶
func Unmarshal(data string, secretKey []byte) (userID string, userName string, roleID uint64, expiredAt time.Time, err error)
Unmarshal extracts the user id, user name, role id, and expiration time from the token string. The token string is decoded from base64 and decrypted with the secret key.
Types ¶
Click to show internal directories.
Click to hide internal directories.