From 9c00ec7ce73b11dff249367736095a3608f2c442 Mon Sep 17 00:00:00 2001 From: Dave Grijalva Date: Tue, 18 Aug 2015 13:28:52 -0700 Subject: [PATCH] more/simpler examples --- example_test.go | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/example_test.go b/example_test.go index 9bc8d4b..4321f14 100644 --- a/example_test.go +++ b/example_test.go @@ -32,15 +32,42 @@ func ExampleNew() { //Output: foo:bar exp:3600 } -func ExampleNewWithClaims(mySigningKey []byte) (string, error) { +func ExampleNewWithClaims() { + mySigningKey := []byte("AllYourBase") + // Create the Claims claims := jwt.StandardClaims{ ExpiresAt: 15000, Issuer: "test", } - token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims) - return token.SignedString(mySigningKey) + token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) + ss, err := token.SignedString(mySigningKey) + fmt.Printf("%v %v", ss, err) + //Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MDAwLCJpc3MiOiJ0ZXN0In0.QsODzZu3lUZMVdhbO76u3Jv02iYCvEHcYVUI1kOWEU0 +} + +func ExampleNewWithClaims_customType() { + mySigningKey := []byte("AllYourBase") + + type MyCustomClaims struct { + Foo string `json:"foo"` + jwt.StandardClaims + } + + // Create the Claims + claims := MyCustomClaims{ + "bar", + jwt.StandardClaims{ + ExpiresAt: 15000, + Issuer: "test", + }, + } + + token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) + ss, err := token.SignedString(mySigningKey) + fmt.Printf("%v %v", ss, err) + //Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJleHAiOjE1MDAwLCJpc3MiOiJ0ZXN0In0.HE7fK0xOQwFEr4WDgRWj4teRPZ6i3GLwD5YCm6Pwu_c } func ExampleParse_errorChecking(myToken string, myLookupKey func(interface{}) (interface{}, error)) {