MCPcopy Create free account
hub / github.com/golang-jwt/jwt / Example_getTokenViaHTTP

Function Example_getTokenViaHTTP

http_example_test.go:81–113  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

79}
80
81func Example_getTokenViaHTTP() {
82 // See func authHandler for an example auth handler that produces a token
83 res, err := http.PostForm(fmt.Sprintf("http://localhost:%v/authenticate", serverPort), url.Values{
84 "user": {"test"},
85 "pass": {"known"},
86 })
87 if err != nil {
88 fatal(err)
89 }
90
91 if res.StatusCode != 200 {
92 fmt.Println("Unexpected status code", res.StatusCode)
93 }
94
95 // Read the token out of the response body
96 buf := new(bytes.Buffer)
97 io.Copy(buf, res.Body)
98 res.Body.Close()
99 tokenString := strings.TrimSpace(buf.String())
100
101 // Parse the token
102 token, err := jwt.ParseWithClaims(tokenString, &CustomClaimsExample{}, func(token *jwt.Token) (interface{}, error) {
103 // since we only use the one private key to sign the tokens,
104 // we also only use its public counter part to verify
105 return verifyKey, nil
106 })
107 fatal(err)
108
109 claims := token.Claims.(*CustomClaimsExample)
110 fmt.Println(claims.CustomerInfo.Name)
111
112 //Output: test
113}
114
115func Example_useTokenViaHTTP() {
116

Callers

nothing calls this directly

Calls 3

fatalFunction · 0.85
StringMethod · 0.80
ParseWithClaimsMethod · 0.80

Tested by

no test coverage detected