forked from lestrrat-go/jwx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt_parse_request_example_test.go
55 lines (48 loc) · 1.41 KB
/
jwt_parse_request_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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:
}