

The jwt.MaxAge is a helper which sets the and for you.Įxample Code to manually set all claims using a standard map: now := time.Now() Returns the encoded token, ready to be sent and stored to the client. The last variadic argument is a type of SignOption ( MaxAge function and Claims struct are both valid sign options), can be used to merge custom claims with the standard ones. Note that the claims can be any Go type, including custom struct, map and raw byte.

The JWT claims is the payload part and it depends on your application's requirements, there you can set custom fields (and expiration) that you can extract to another request of the same authorized client later on. The second argument is the private key (or shared key, when symmetric algorithm was chosen) will be used to create the signature. The first argument is the signing algorithm to create the signature part. Token, err := jwt.Sign(jwt.HS256, sharedkey, userClaims, jwt.MaxAge(15 *time.Minute)) Signing a Token is done through the Sign package-level function. Signing and Verifying a token is an extremely easy process. The package contains comments on each one of its exported functions, structures and variables, therefore, for a more detailed technical documentation please refer to godocs. VerifiedToken, err := jwt.Verify(jwt.HS256, sharedKey, token) Verify and extract claims from a token: Token, err := jwt.Sign(jwt.HS256, sharedKey, m圜laims, jwt.MaxAge(15*time.Minute)) Generate a token which expires at 15 minutes from now: Decode the custom claims with the VerifiedToken.Claims method. Verify the token with the Verify method, returns a VerifiedToken value. Optionally set an expiration, if "exp" is missing from the payload use the jwt.MaxAge helper. Sign and generate a token with the Sign method, returns the token in compact form. Import as import "/kataras/jwt" and use it as jwt.XXX. The only requirement is the Go Programming Language. This package was designed with security, performance and simplicity in mind, it protects your tokens from critical vulnerabilities that you may find in other libraries. Fast and simple JWT implementation written in Go.
