Skip to content

Instantly share code, notes, and snippets.

@lestrrat
Created April 25, 2022 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lestrrat/f66c7b76ff4ac37fb52c7e5ae1e60e2b to your computer and use it in GitHub Desktop.
Save lestrrat/f66c7b76ff4ac37fb52c7e5ae1e60e2b to your computer and use it in GitHub Desktop.
package examples_test
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()))
if err != nil {
fmt.Printf("failed to create request: %s\n", err)
return
}
req.Header.Set(`Authorization`, fmt.Sprintf(`Bearer %s`, exampleJWTSignedECDSA))
req.Header.Set(`X-JWT-Token`, exampleJWTSignedRSA)
testcases := []struct {
options []jwt.ParseOption
}{
// No options - looks under "Authorization" header
{},
// Looks under "X-JWT-Token" header only
{
options: []jwt.ParseOption{jwt.WithHeaderKey(`X-JWT-Token`)},
},
// Looks under "Authorization" and "X-JWT-Token" headers
{
options: []jwt.ParseOption{jwt.WithHeaderKey(`Authorization`), jwt.WithHeaderKey(`X-JWT-Token`)},
},
// Looks under "Authorization" header and "access_token" form field
{
options: []jwt.ParseOption{jwt.WithFormKey(`access_token`)},
},
}
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)
return
}
_ = tok
}
// OUTPUT:
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment