From db4251f9ddf0d4e470dc19d71169923fd10c0305 Mon Sep 17 00:00:00 2001 From: Dave Grijalva Date: Sun, 28 Dec 2014 12:24:54 -0800 Subject: [PATCH] added examples --- example_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 example_test.go diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..fcc34fa --- /dev/null +++ b/example_test.go @@ -0,0 +1,30 @@ +package jwt_test + +import ( + "fmt" + "github.com/dgrijalva/jwt-go" + "time" +) + +func ExampleParse(myToken string, myLookupKey func(interface{}) (interface{}, error)) { + token, err := jwt.Parse(myToken, func(token *jwt.Token) (interface{}, error) { + return myLookupKey(token.Header["kid"]) + }) + + if err == nil && token.Valid { + fmt.Println("Your token is valid. I like your style.") + } else { + fmt.Println("This token is terrible! I cannot accept this.") + } +} + +func ExampleNew(mySigningKey string) (string, error) { + // Create the token + token := jwt.New(jwt.SigningMethodHS256) + // Set some claims + token.Claims["foo"] = "bar" + token.Claims["exp"] = time.Now().Add(time.Hour * 72).Unix() + // Sign and get the complete encoded token as a string + tokenString, err := token.SignedString(mySigningKey) + return tokenString, err +}