()
| 78 | } |
| 79 | |
| 80 | func 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 | |
| 112 | func Example_useTokenViaHTTP() { |
| 113 | // Make a sample token |
nothing calls this directly
no test coverage detected