()
| 110 | } |
| 111 | |
| 112 | func Example_useTokenViaHTTP() { |
| 113 | // Make a sample token |
| 114 | // In a real world situation, this token will have been acquired from |
| 115 | // some other API call (see Example_getTokenViaHTTP) |
| 116 | token, err := createToken("foo") |
| 117 | fatal(err) |
| 118 | |
| 119 | // Make request. See func restrictedHandler for example request processor |
| 120 | req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost:%v/restricted", serverPort), nil) |
| 121 | fatal(err) |
| 122 | req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", token)) |
| 123 | res, err := http.DefaultClient.Do(req) |
| 124 | fatal(err) |
| 125 | |
| 126 | // Read the response body |
| 127 | buf, err := io.ReadAll(res.Body) |
| 128 | fatal(err) |
| 129 | _ = res.Body.Close() |
| 130 | fmt.Printf("%s", buf) |
| 131 | |
| 132 | // Output: Welcome, foo |
| 133 | } |
| 134 | |
| 135 | func createToken(user string) (string, error) { |
| 136 | // create a signer for rsa 256 |
nothing calls this directly
no test coverage detected