exchangeCode swaps an authorization code for a token pair via /oauth2/token.
(t *testing.T, code, verifier, redirectURI string)
| 11 | |
| 12 | // exchangeCode swaps an authorization code for a token pair via /oauth2/token. |
| 13 | func (f *consentFixture) exchangeCode(t *testing.T, code, verifier, redirectURI string) (accessToken, refreshToken, scope string) { |
| 14 | t.Helper() |
| 15 | form := url.Values{} |
| 16 | form.Set("grant_type", "authorization_code") |
| 17 | form.Set("code", code) |
| 18 | form.Set("client_id", f.clientID) |
| 19 | form.Set("redirect_uri", redirectURI) |
| 20 | form.Set("code_verifier", verifier) |
| 21 | resp, err := http.Post(f.server.URL+"/oauth2/token", "application/x-www-form-urlencoded", strings.NewReader(form.Encode())) |
| 22 | if err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | defer resp.Body.Close() |
| 26 | if resp.StatusCode != http.StatusOK { |
| 27 | t.Fatalf("token exchange status = %d, want 200", resp.StatusCode) |
| 28 | } |
| 29 | var body struct { |
| 30 | AccessToken string `json:"access_token"` |
| 31 | RefreshToken string `json:"refresh_token"` |
| 32 | Scope string `json:"scope"` |
| 33 | } |
| 34 | if err := json.NewDecoder(resp.Body).Decode(&body); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | return body.AccessToken, body.RefreshToken, body.Scope |
| 38 | } |
| 39 | |
| 40 | // whoami calls GET /v1/account with the bearer and returns (scope, agentAddress). |
| 41 | func (f *consentFixture) whoami(t *testing.T, bearer string) (scope, agentAddress string) { |
no test coverage detected