Skip to content

Commit

Permalink
Update examples and fix ParseAndVerify (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Apr 19, 2020
1 parent 49472df commit 33c9d86
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 18 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
24 changes: 18 additions & 6 deletions example_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand All @@ -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()))
Expand All @@ -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()))
Expand Down
41 changes: 36 additions & 5 deletions example_parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 6 additions & 7 deletions example_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand Down

0 comments on commit 33c9d86

Please sign in to comment.