Last active
January 24, 2019 08:21
-
-
Save lya79/1988a49fc004d6d0a7540670e84c7102 to your computer and use it in GitHub Desktop.
jwt-go sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "time" | |
| jwt "github.com/dgrijalva/jwt-go" | |
| ) | |
| /* | |
| go get -u github.com/dgrijalva/jwt-go | |
| */ | |
| var hmacSampleSecret = []byte("vvsdvsdv1231k4j3krjfsd") | |
| func main() { | |
| tokenStr, err := creating() | |
| if err != nil { | |
| fmt.Println("create token, err:", err) | |
| return | |
| } | |
| for i := 0; i < 10; i++ { | |
| time.Sleep(1 * time.Second) | |
| fmt.Println("") | |
| fmt.Println("sleep ", (i + 1), "sec") | |
| validating(tokenStr) | |
| } | |
| } | |
| func createExpires(sec int64) int64 { | |
| return time.Now().Add(time.Second * time.Duration(sec)).Unix() | |
| } | |
| func isExpired(t int64) bool { | |
| return time.Now().Unix()-t >= 0 | |
| } | |
| func creating() (string, error) { | |
| // Create a new token object, specifying signing method and the claims | |
| // you would like it to contain. | |
| token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ | |
| "username": "test123", | |
| "expires": createExpires(5), | |
| }) | |
| // Sign and get the complete encoded token as a string using the secret | |
| tokenString, err := token.SignedString(hmacSampleSecret) | |
| fmt.Println(tokenString, err) | |
| return tokenString, err | |
| } | |
| func validating(tokenString string) { | |
| // Parse takes the token string and a function for looking up the key. The latter is especially | |
| // useful if you use multiple keys for your application. The standard is to use 'kid' in the | |
| // head of the token to identify which key to use, but the parsed token (head and claims) is provided | |
| // to the callback, providing flexibility. | |
| token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { | |
| // Don't forget to validate the alg is what you expect: | |
| if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { | |
| return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"]) | |
| } | |
| // hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key") | |
| return hmacSampleSecret, nil | |
| }) | |
| if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid { | |
| fmt.Println("success parse,") | |
| // fmt.Println("username:", claims["username"]) | |
| // fmt.Println("expires:", claims["expires"]) | |
| // fmt.Println("nbf:", claims["nbf"]) | |
| if claims["username"] != nil { | |
| username, ok := claims["username"].(string) | |
| fmt.Println("username:", username, ", ok:", ok) | |
| } else { | |
| fmt.Println("username nil") | |
| } | |
| if claims["expires"] != nil { | |
| expires, ok := claims["expires"].(float64) | |
| fmt.Println("expires:", expires, ", ok:", ok) | |
| if ok { | |
| fmt.Println("isExpired:", isExpired(int64(expires))) | |
| } | |
| } else { | |
| fmt.Println("expires nil") | |
| } | |
| if claims["nbf"] != nil { | |
| nbf, ok := claims["nbf"].(string) | |
| fmt.Println("nbf:", nbf, ", ok:", ok) | |
| } else { | |
| fmt.Println("nbf nil") | |
| } | |
| } else { | |
| fmt.Println("fail parse,", err) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment