Skip to content

Commit 8a82ef1

Browse files
committed
internal/iapclient: use http.PostForm to send request
It has identical behavior but makes the code shorter and easier to see that it's just a form POST, rather than something custom. Also cap the body size it's willing to read for error reporting. (Spotted while reading over this code.) For golang/go#48739. Change-Id: I586925d1a0c7e9a7e1efc93d121337a16fcee725 Reviewed-on: https://go-review.googlesource.com/c/build/+/371014 Trust: Dmitri Shuralyov <[email protected]> Run-TryBot: Dmitri Shuralyov <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent 598f1b0 commit 8a82ef1

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

internal/iapclient/iapclient.go

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ import (
1010
"context"
1111
"encoding/json"
1212
"fmt"
13-
"io/ioutil"
13+
"io"
1414
"net/http"
1515
"net/url"
1616
"os"
1717
"path/filepath"
18-
"strings"
1918

2019
"golang.org/x/oauth2"
2120
"golang.org/x/oauth2/google"
@@ -119,32 +118,28 @@ type jwtTokenSource struct {
119118
refresh *oauth2.Token
120119
}
121120

122-
// Exchange a refresh token for a JWT that works with IAP. As of writing, there
121+
// Token exchanges a refresh token for a JWT that works with IAP. As of writing, there
123122
// isn't anything to do this in the oauth2 library or google.golang.org/api/idtoken.
124123
func (s *jwtTokenSource) Token() (*oauth2.Token, error) {
125-
v := url.Values{}
126-
v.Set("client_id", s.conf.ClientID)
127-
v.Set("client_secret", s.conf.ClientSecret)
128-
v.Set("refresh_token", s.refresh.RefreshToken)
129-
v.Set("grant_type", "refresh_token")
130-
v.Set("audience", s.audience)
131-
req, err := http.NewRequest("POST", s.conf.Endpoint.TokenURL, strings.NewReader(v.Encode()))
132-
if err != nil {
133-
return nil, err
134-
}
135-
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
136-
resp, err := http.DefaultClient.Do(req)
124+
resp, err := http.PostForm(s.conf.Endpoint.TokenURL, url.Values{
125+
"client_id": []string{s.conf.ClientID},
126+
"client_secret": []string{s.conf.ClientSecret},
127+
"refresh_token": []string{s.refresh.RefreshToken},
128+
"grant_type": []string{"refresh_token"},
129+
"audience": []string{s.audience},
130+
})
137131
if err != nil {
138132
return nil, err
139133
}
140134
defer resp.Body.Close()
141-
body, err := ioutil.ReadAll(resp.Body)
142-
if err != nil {
143-
return nil, err
144-
}
145135
if resp.StatusCode != http.StatusOK {
136+
body, _ := io.ReadAll(io.LimitReader(resp.Body, 4<<10))
146137
return nil, fmt.Errorf("IAP token exchange failed: status %v, body %q", resp.Status, body)
147138
}
139+
body, err := io.ReadAll(resp.Body)
140+
if err != nil {
141+
return nil, err
142+
}
148143
var token jwtTokenJSON
149144
if err := json.Unmarshal(body, &token); err != nil {
150145
return nil, err

0 commit comments

Comments
 (0)