Documentation
¶
Index ¶
- Variables
- func ClearMiddleware() gin.HandlerFunc
- func Delete(c *gin.Context, key interface{}) (err error)
- func DeleteAllSession(c *gin.Context)
- func DeleteNormalSession(c *gin.Context)
- func DeleteSession(c *gin.Context, sessionName string) (err error)
- func DeleteSessionValue(c *gin.Context, sessionName string, key interface{}) (err error)
- func DeleteTokenSession(c *gin.Context) (err error)
- func GenerateJWTToken(username string, tokenDuration time.Duration) (tokenString string, err error)
- func GetDefaultSessionValue(c *gin.Context, key interface{}) (value interface{}, err error)
- func GetFlashes(c *gin.Context) []interface{}
- func GetInt(c *gin.Context, key interface{}) (value int, err error)
- func GetSessionFlashes(c *gin.Context, sessionName string) []interface{}
- func GetSessionValue(c *gin.Context, sessionName string, key interface{}) (value interface{}, err error)
- func GetString(c *gin.Context, key interface{}) (value string, err error)
- func GetTokenString(c *gin.Context) (tokenString string, err error)
- func NewStore()
- func Set(c *gin.Context, key, value interface{}) (err error)
- func SetFlash(c *gin.Context, flash interface{}) (err error)
- func SetMessage(c *gin.Context, message Message) (err error)
- func SetSessionFlash(c *gin.Context, flash Flash) (err error)
- func SetTokenString(c *gin.Context, tokenString string, seconds int) (err error)
- func ValidateJWTToken(c *gin.Context) (username string, err error)
- type Flash
- type Message
- type Options
Constants ¶
This section is empty.
Variables ¶
var ( //SecretKey to encrypt session //please clear the browser buffer(ctrl+shift+delete) in case you change the key and token name to new one SecretKey = "welcome to Scott Huang's Session and JWT util, please change to your secret accodingly." //JwtTokenName is JWT token name, also is the JWT session name too //Not too long and also do not include blank, such as do not set as "a blank name" JwtTokenName = "jwtTokenSession" //DefaultFlashSessionName to store the flash session //Not too long and also do not include blank, such as do not set as "a blank name" DefaultFlashSessionName = "myDefaultFlashSessionName" //DefaultSessionName to store other session message //Not too long and also do not include blank, such as do not set as "a blank name" DefaultSessionName = "myDefaultSessionName" //DefaultOption to provide default option //Maxage set the session duration by second DefaultOption = &sessions.Options{ Path: "/", MaxAge: 3600 * 1, HttpOnly: true, } )
Functions ¶
func ClearMiddleware ¶
func ClearMiddleware() gin.HandlerFunc
ClearMiddleware clear mem to avoid leak. you should add this middleware at your main gin.router
Please see note from http://www.gorillatoolkit.org/pkg/sessions Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory! ClearHandler actually invoke Clear func
func DeleteAllSession ¶
DeleteAllSession will delete JwtTokenName/DefaultSessionName/DefaultFlashSessionName usually used when user logout
func DeleteNormalSession ¶
DeleteNormalSession will delete DefaultSessionName and DefaultFlashSessionName
func DeleteSession ¶
DeleteSession delete a session instead of Set, the delete will set MaxAge to -1
func DeleteSessionValue ¶
DeleteSessionValue try delete the gived session key value
func DeleteTokenSession ¶
DeleteTokenSession delete before generate JWT token string
func GenerateJWTToken ¶
GenerateJWTToken per gived username and token duration
Claims example as below, and we only use below top 3 claims
"iat": 1416797419, //start "exp": 1448333419, //end "sub": "jrocket@example.com", //username "iss": "Online JWT Builder", "aud": "www.example.com", "GivenName": "Johnny", "Surname": "Rocket", "Email": "jrocket@example.com", "Role": [ "Manager", "Project Administrator" ]
func GetDefaultSessionValue ¶
GetDefaultSessionValue levarage GetSessionValue to get key value
func GetFlashes ¶
GetFlashes return previously flashes from default session
func GetSessionFlashes ¶
GetSessionFlashes return previously flashes per gived session
func GetSessionValue ¶
func GetSessionValue(c *gin.Context, sessionName string, key interface{}) (value interface{}, err error)
GetSessionValue session value according The base get func, other will leverage this func
func GetTokenString ¶
GetTokenString to get tokenString
func NewStore ¶
func NewStore()
NewStore only create one session store globally The main program need call sessions.NewStore() to initialize store User also can set sessions var before call this function to adjust some default parameters
Usage Sample:
//in your main(), setup session after r = gin.Default() sessions.JwtTokenName = "YourJWTTokenName" //string without blank sessions.DefaultSessionName = "YourDefaultSessionName" //string without blank sessions.DefaultFlashSessionName = "YourDefaultFlashSessionName" //string without blank sessions.SecretKey = "Your Secerect Key (*&%(&*%$" //string with any sessions.NewStore() //setup the session store r.Use(sessions.ClearMiddleware()) //important to avoid memory leak //end setup session
func SetMessage ¶
SetMessage will set session into gin.Context per gived SesionMessage It is the basic function for other set func to leverage
Usage Sample:
err := sessions.SetMessage(c, sessions.Message{ Key: sessions.JwtTokenName, Value: tokenString, // SessionName: "", // Options: &sessions.Options{ // Path: "/", // MaxAge: 3600 * 1, //1 hour for session. Btw, the token itself have valid period, so, better set it as same // HttpOnly: true, // }, })
func SetSessionFlash ¶
SetSessionFlash set new flash to givied session
func SetTokenString ¶
SetTokenString into JwtTokenSession
Types ¶
type Flash ¶
type Flash struct { Flash interface{} SessionName string }
Flash type to contain new flash and its session