Skip to content

Commit

Permalink
autodoc updates (#1099)
Browse files Browse the repository at this point in the history
Co-authored-by: lestrrat <lestrrat@users.noreply.github.com>
  • Loading branch information
github-actions[bot] and lestrrat committed Mar 10, 2024
1 parent 664fdaf commit ebaf6ab
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions docs/01-jwt.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,25 +151,26 @@ import (
"fmt"
"net/http"
"net/url"
"strings"

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

func ExampleJWT_ParseRequest_Authorization() {
values := url.Values{
`access_token`: []string{exampleJWTSignedHMAC},
}

req, err := http.NewRequest(http.MethodGet, `https://github.com/lestrrat-go/jwx`, strings.NewReader(values.Encode()))
req, err := http.NewRequest(http.MethodGet, `https://github.com/lestrrat-go/jwx`, nil)
if err != nil {
fmt.Printf("failed to create request: %s\n", err)
return
}
req.Form = url.Values{}
req.Form.Add("access_token", exampleJWTSignedHMAC)

req.Header.Set(`Authorization`, fmt.Sprintf(`Bearer %s`, exampleJWTSignedECDSA))
req.Header.Set(`X-JWT-Token`, exampleJWTSignedRSA)

req.AddCookie(&http.Cookie{Name: "accessToken", Value: exampleJWTSignedHMAC})

var dst *http.Cookie

testcases := []struct {
options []jwt.ParseOption
}{
Expand All @@ -187,17 +188,34 @@ func ExampleJWT_ParseRequest_Authorization() {
{
options: []jwt.ParseOption{jwt.WithFormKey(`access_token`)},
},
// Looks under "accessToken" cookie, and assigns the http.Cookie object
// where the token came from to the variable `dst`
{
options: []jwt.ParseOption{jwt.WithCookieKey(`accessToken`), jwt.WithCookie(&dst)},
},
}

for _, tc := range testcases {
options := append(tc.options, []jwt.ParseOption{jwt.WithVerify(false), jwt.WithValidate(false)}...)
tok, err := jwt.ParseRequest(req, options...)
if err != nil {
fmt.Printf("jwt.ParseRequest with options %#v failed: %s\n", tc.options, err)
fmt.Print("jwt.ParseRequest with options [")
for i, option := range tc.options {
if i > 0 {
fmt.Print(", ")
}
fmt.Printf("%s", option)
}
fmt.Printf("]: %s\n", err)
return
}
_ = tok
}

if dst == nil {
fmt.Printf("failed to assign cookie to dst\n")
return
}
// OUTPUT:
}
```
Expand Down

0 comments on commit ebaf6ab

Please sign in to comment.