From 33c9d86524cdaa497f1e4a0d23340419ca866060 Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Sun, 19 Apr 2020 12:22:53 +0200 Subject: [PATCH] Update examples and fix ParseAndVerify (#31) --- README.md | 2 ++ example_build_test.go | 24 +++++++++++++++++------ example_parse_test.go | 41 +++++++++++++++++++++++++++++++++++----- example_validate_test.go | 13 ++++++------- 4 files changed, 62 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 461f02a..5b24edb 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,8 @@ token, _ := builder.Build(claims) raw := token.Raw() // JWT signed token ``` +Also see examples: [build](https://github.com/cristalhq/jwt/blob/master/example_build_test.go), [parse](https://github.com/cristalhq/jwt/blob/master/example_parse_test.go), [validate](https://github.com/cristalhq/jwt/blob/master/example_validate_test.go). + ## Documentation See [these docs](https://godoc.org/github.com/cristalhq/jwt). diff --git a/example_build_test.go b/example_build_test.go index 449b2d9..06d1231 100644 --- a/example_build_test.go +++ b/example_build_test.go @@ -8,14 +8,18 @@ import ( ) func Example_BuildSimple() { - signer, _ := jwt.NewHS256([]byte(`secret`)) + key := []byte(`secret`) + signer, _ := jwt.NewHS256(key) builder := jwt.NewTokenBuilder(signer) claims := &jwt.StandardClaims{ Audience: []string{"admin"}, ID: "random-unique-string", } - token, _ := builder.Build(claims) + token, errBuild := builder.Build(claims) + if errBuild != nil { + panic(errBuild) + } fmt.Printf("Algorithm %v\n", token.Header().Algorithm) fmt.Printf("Type %v\n", token.Header().Type) @@ -43,7 +47,8 @@ func (u *userClaims) MarshalBinary() ([]byte, error) { } func Example_BuildUserClaims() { - signer, _ := jwt.NewHS256([]byte(`secret`)) + key := []byte(`secret`) + signer, _ := jwt.NewHS256(key) builder := jwt.NewTokenBuilder(signer) claims := &userClaims{ @@ -54,7 +59,10 @@ func Example_BuildUserClaims() { IsAdministrator: true, Email: "foo@bar.baz", } - token, _ := builder.Build(claims) + token, errBuild := builder.Build(claims) + if errBuild != nil { + panic(errBuild) + } fmt.Printf("Claims %v\n", string(token.RawClaims())) fmt.Printf("Payload %v\n", string(token.Payload())) @@ -73,14 +81,18 @@ func (d *dummyClaims) MarshalBinary() ([]byte, error) { } func Example_DummyClaims() { - signer, _ := jwt.NewHS256([]byte(`secret`)) + key := []byte(`secret`) + signer, _ := jwt.NewHS256(key) builder := jwt.NewTokenBuilder(signer) claims := dummyClaims(map[string]interface{}{ "aUdIeNcE": "@everyone", "well": "well-well-well", }) - token, _ := builder.Build(&claims) + token, errBuild := builder.Build(&claims) + if errBuild != nil { + panic(errBuild) + } fmt.Printf("Claims %v\n", string(token.RawClaims())) fmt.Printf("Payload %v\n", string(token.Payload())) diff --git a/example_parse_test.go b/example_parse_test.go index b770fd1..6ea63b2 100644 --- a/example_parse_test.go +++ b/example_parse_test.go @@ -6,13 +6,44 @@ import ( "github.com/cristalhq/jwt" ) -func Example_ParseSimple() { +func Example_Parse() { t := `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJhZG1pbiIsImp0aSI6InJhbmRvbS11bmlxdWUtc3RyaW5nIn0.dv9-XpY9P8ypm1uWQwB6eKvq3jeyodLA7brhjsf4JVs` - token, err := jwt.Parse([]byte(t)) - if err != nil { - fmt.Printf("parse err: %q", err) - return + token, errParse := jwt.Parse([]byte(t)) + if errParse != nil { + panic(errParse) + } + + fmt.Printf("Algorithm %v\n", token.Header().Algorithm) + fmt.Printf("Type %v\n", token.Header().Type) + fmt.Printf("Claims %v\n", string(token.RawClaims())) + fmt.Printf("Payload %v\n", string(token.Payload())) + fmt.Printf("Token %v\n", string(token.Raw())) + + // key := []byte(`secret`) + // signer, _ := jwt.NewHS256(key) + // errVerify := signer.Verify(token.Payload(), token.Signature()) + // if errVerify != nil { + // panic(errVerify) + // } + + // Output: + // Algorithm HS256 + // Type JWT + // Claims {"aud":"admin","jti":"random-unique-string"} + // Payload eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJhZG1pbiIsImp0aSI6InJhbmRvbS11bmlxdWUtc3RyaW5nIn0 + // Token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJhZG1pbiIsImp0aSI6InJhbmRvbS11bmlxdWUtc3RyaW5nIn0.dv9-XpY9P8ypm1uWQwB6eKvq3jeyodLA7brhjsf4JVs +} + +func Example_ParseAndVerify() { + key := []byte(`secret`) + signer, _ := jwt.NewHS256(key) + + t := `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJhZG1pbiIsImp0aSI6InJhbmRvbS11bmlxdWUtc3RyaW5nIn0.dv9-XpY9P8ypm1uWQwB6eKvq3jeyodLA7brhjsf4JVs` + + token, errParse := jwt.ParseAndVerify([]byte(t), signer) + if errParse != nil { + panic(errParse) } fmt.Printf("Algorithm %v\n", token.Header().Algorithm) diff --git a/example_validate_test.go b/example_validate_test.go index 3fcba3c..3751787 100644 --- a/example_validate_test.go +++ b/example_validate_test.go @@ -10,10 +10,9 @@ import ( func Example_Validate() { t := `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJhZG1pbiIsImp0aSI6InJhbmRvbS11bmlxdWUtc3RyaW5nIn0.dv9-XpY9P8ypm1uWQwB6eKvq3jeyodLA7brhjsf4JVs` - token, err := jwt.Parse([]byte(t)) - if err != nil { - fmt.Printf("parse err: %q", err) - return + token, errParse := jwt.Parse([]byte(t)) + if errParse != nil { + panic(errParse) } fmt.Printf("Algorithm %v\n", token.Header().Algorithm) @@ -30,9 +29,9 @@ func Example_Validate() { jwt.IDChecker("random-unique-string"), ) - err = validator.Validate(claims) - if err != nil { - fmt.Printf("token is invalid: %#v", err) + errValidate := validator.Validate(claims) + if errValidate != nil { + panic(errValidate) } // Output: