Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autodoc updates #968

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions docs/01-jwt.md
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,57 @@ source: [examples/jwt_flatten_audience_example_test.go](https://github.com/lestr
github.com/lestrrat-go/jwx is focused on usability / stable API. If you are worried about performance while processing JWTs, the best path is just to use a plain struct after handling JWS yourself:

<!-- INCLUDE(examples/jwt_raw_struct_example_test.go) -->
```go
package examples

import (
"encoding/json"
"fmt"
"os"

"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jws"
"github.com/lestrrat-go/jwx/v2/jwt"
)

func ExampleJWTPlainStruct() {
t1, err := jwt.NewBuilder().
Issuer("https://github.com/lestrrat-go/jwx/v2/examples").
Subject("raw_struct").
Claim("private", "foobar").
Build()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to build JWT: %s\n", err)
}

key := []byte("secret")
signed, err := jwt.Sign(t1, jwt.WithKey(jwa.HS256, key))
if err != nil {
fmt.Printf("failed to sign JWT: %s\n", err)
}

rawJWT, err := jws.Verify(signed, jws.WithKey(jwa.HS256, key))
if err != nil {
fmt.Printf("failed to verify JWS: %s\n", err)
}

type MyToken struct {
Issuer string `json:"iss"`
Subject string `json:"sub"`
Private string `json:"private"`
}

var t2 MyToken
if err := json.Unmarshal(rawJWT, &t2); err != nil {
fmt.Printf("failed to unmarshal JWT: %s\n", err)
}

fmt.Printf("%s\n", t2.Private)
// OUTPUT:
// foobar
}
```
source: [examples/jwt_raw_struct_example_test.go](https://github.com/lestrrat-go/jwx/blob/v2/examples/jwt_raw_struct_example_test.go)
<!-- END INCLUDE -->

This makes sure that you do not go through any extra layers of abstraction that causes performance panalties, and you get exactly the type of field that you want.
Expand Down