MCPcopy
hub / github.com/golang-jwt/jwt / Example_getTokenViaHTTP

Function Example_getTokenViaHTTP

http_example_test.go:80–110  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 2

fatalFunction · 0.85
ParseWithClaimsMethod · 0.80

Tested by

no test coverage detected