Skip to content

Commit 1c06e87

Browse files
mvdanseankhliao
authored andcommitted
all: make use of oauth.Token.ExpiresIn
With https://go.dev/issue/61417 implemented, we can use the token type directly to unmarshal the JSON fields for the wire format. While here, remove all uses of the deprecated ioutil package as suggested by gopls while making these changes. Change-Id: I79d82374643007a21b5b3d9a8117bed81273eca5 Reviewed-on: https://go-review.googlesource.com/c/oauth2/+/614415 Reviewed-by: Sean Liao <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Michael Pratt <[email protected]>
1 parent 65c15a3 commit 1c06e87

19 files changed

+50
-73
lines changed

clientcredentials/clientcredentials_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package clientcredentials
77
import (
88
"context"
99
"io"
10-
"io/ioutil"
1110
"net/http"
1211
"net/http/httptest"
1312
"net/url"
@@ -36,9 +35,9 @@ func TestTokenSourceGrantTypeOverride(t *testing.T) {
3635
wantGrantType := "password"
3736
var gotGrantType string
3837
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
39-
body, err := ioutil.ReadAll(r.Body)
38+
body, err := io.ReadAll(r.Body)
4039
if err != nil {
41-
t.Errorf("ioutil.ReadAll(r.Body) == %v, %v, want _, <nil>", body, err)
40+
t.Errorf("io.ReadAll(r.Body) == %v, %v, want _, <nil>", body, err)
4241
}
4342
if err := r.Body.Close(); err != nil {
4443
t.Errorf("r.Body.Close() == %v, want <nil>", err)
@@ -81,7 +80,7 @@ func TestTokenRequest(t *testing.T) {
8180
if got, want := r.Header.Get("Content-Type"), "application/x-www-form-urlencoded"; got != want {
8281
t.Errorf("Content-Type header = %q; want %q", got, want)
8382
}
84-
body, err := ioutil.ReadAll(r.Body)
83+
body, err := io.ReadAll(r.Body)
8584
if err != nil {
8685
r.Body.Close()
8786
}
@@ -123,7 +122,7 @@ func TestTokenRefreshRequest(t *testing.T) {
123122
if got, want := headerContentType, "application/x-www-form-urlencoded"; got != want {
124123
t.Errorf("Content-Type = %q; want %q", got, want)
125124
}
126-
body, _ := ioutil.ReadAll(r.Body)
125+
body, _ := io.ReadAll(r.Body)
127126
const want = "audience=audience1&grant_type=client_credentials&scope=scope1+scope2"
128127
if string(body) != want {
129128
t.Errorf("Unexpected refresh token payload.\n got: %s\nwant: %s\n", body, want)

google/downscope/downscoping.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import (
3939
"context"
4040
"encoding/json"
4141
"fmt"
42-
"io/ioutil"
42+
"io"
4343
"net/http"
4444
"net/url"
4545
"strings"
@@ -198,7 +198,7 @@ func (dts downscopingTokenSource) Token() (*oauth2.Token, error) {
198198
return nil, fmt.Errorf("unable to generate POST Request %v", err)
199199
}
200200
defer resp.Body.Close()
201-
respBody, err := ioutil.ReadAll(resp.Body)
201+
respBody, err := io.ReadAll(resp.Body)
202202
if err != nil {
203203
return nil, fmt.Errorf("downscope: unable to read response body: %v", err)
204204
}

google/downscope/downscoping_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package downscope
66

77
import (
88
"context"
9-
"io/ioutil"
9+
"io"
1010
"net/http"
1111
"net/http/httptest"
1212
"testing"
@@ -27,7 +27,7 @@ func Test_DownscopedTokenSource(t *testing.T) {
2727
if r.URL.String() != "/" {
2828
t.Errorf("Unexpected request URL, %v is found", r.URL)
2929
}
30-
body, err := ioutil.ReadAll(r.Body)
30+
body, err := io.ReadAll(r.Body)
3131
if err != nil {
3232
t.Fatalf("Failed to read request body: %v", err)
3333
}

google/example_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ package google_test
77
import (
88
"context"
99
"fmt"
10-
"io/ioutil"
1110
"log"
1211
"net/http"
12+
"os"
1313

1414
"golang.org/x/oauth2"
1515
"golang.org/x/oauth2/google"
@@ -60,7 +60,7 @@ func ExampleJWTConfigFromJSON() {
6060
// To create a service account client, click "Create new Client ID",
6161
// select "Service Account", and click "Create Client ID". A JSON
6262
// key file will then be downloaded to your computer.
63-
data, err := ioutil.ReadFile("/path/to/your-project-key.json")
63+
data, err := os.ReadFile("/path/to/your-project-key.json")
6464
if err != nil {
6565
log.Fatal(err)
6666
}
@@ -136,7 +136,7 @@ func ExampleComputeTokenSource() {
136136

137137
func ExampleCredentialsFromJSON() {
138138
ctx := context.Background()
139-
data, err := ioutil.ReadFile("/path/to/key-file.json")
139+
data, err := os.ReadFile("/path/to/key-file.json")
140140
if err != nil {
141141
log.Fatal(err)
142142
}

google/externalaccount/aws.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"errors"
1515
"fmt"
1616
"io"
17-
"io/ioutil"
1817
"net/http"
1918
"net/url"
2019
"os"
@@ -170,7 +169,7 @@ func requestDataHash(req *http.Request) (string, error) {
170169
}
171170
defer requestBody.Close()
172171

173-
requestData, err = ioutil.ReadAll(io.LimitReader(requestBody, 1<<20))
172+
requestData, err = io.ReadAll(io.LimitReader(requestBody, 1<<20))
174173
if err != nil {
175174
return "", err
176175
}
@@ -419,7 +418,7 @@ func (cs *awsCredentialSource) getAWSSessionToken() (string, error) {
419418
}
420419
defer resp.Body.Close()
421420

422-
respBody, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
421+
respBody, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
423422
if err != nil {
424423
return "", err
425424
}
@@ -462,7 +461,7 @@ func (cs *awsCredentialSource) getRegion(headers map[string]string) (string, err
462461
}
463462
defer resp.Body.Close()
464463

465-
respBody, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
464+
respBody, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
466465
if err != nil {
467466
return "", err
468467
}
@@ -531,7 +530,7 @@ func (cs *awsCredentialSource) getMetadataSecurityCredentials(roleName string, h
531530
}
532531
defer resp.Body.Close()
533532

534-
respBody, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
533+
respBody, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
535534
if err != nil {
536535
return result, err
537536
}
@@ -564,7 +563,7 @@ func (cs *awsCredentialSource) getMetadataRoleName(headers map[string]string) (s
564563
}
565564
defer resp.Body.Close()
566565

567-
respBody, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
566+
respBody, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
568567
if err != nil {
569568
return "", err
570569
}

google/externalaccount/basecredentials_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"context"
99
"encoding/json"
1010
"fmt"
11-
"io/ioutil"
11+
"io"
1212
"net/http"
1313
"net/http/httptest"
1414
"testing"
@@ -77,7 +77,7 @@ func run(t *testing.T, config *Config, tets *testExchangeTokenServer) (*oauth2.T
7777
if got, want := headerMetrics, tets.metricsHeader; got != want {
7878
t.Errorf("got %v but want %v", got, want)
7979
}
80-
body, err := ioutil.ReadAll(r.Body)
80+
body, err := io.ReadAll(r.Body)
8181
if err != nil {
8282
t.Fatalf("Failed reading request body: %s.", err)
8383
}
@@ -131,7 +131,7 @@ func createImpersonationServer(urlWanted, authWanted, bodyWanted, response strin
131131
if got, want := headerContentType, "application/json"; got != want {
132132
t.Errorf("got %v but want %v", got, want)
133133
}
134-
body, err := ioutil.ReadAll(r.Body)
134+
body, err := io.ReadAll(r.Body)
135135
if err != nil {
136136
t.Fatalf("Failed reading request body: %v.", err)
137137
}
@@ -160,7 +160,7 @@ func createTargetServer(metricsHeaderWanted string, t *testing.T) *httptest.Serv
160160
if got, want := headerMetrics, metricsHeaderWanted; got != want {
161161
t.Errorf("got %v but want %v", got, want)
162162
}
163-
body, err := ioutil.ReadAll(r.Body)
163+
body, err := io.ReadAll(r.Body)
164164
if err != nil {
165165
t.Fatalf("Failed reading request body: %v.", err)
166166
}

google/externalaccount/executablecredsource.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"errors"
1212
"fmt"
1313
"io"
14-
"io/ioutil"
1514
"os"
1615
"os/exec"
1716
"regexp"
@@ -258,7 +257,7 @@ func (cs executableCredentialSource) getTokenFromOutputFile() (token string, err
258257
}
259258
defer file.Close()
260259

261-
data, err := ioutil.ReadAll(io.LimitReader(file, 1<<20))
260+
data, err := io.ReadAll(io.LimitReader(file, 1<<20))
262261
if err != nil || len(data) == 0 {
263262
// Cachefile exists, but no data found. Get new credential.
264263
return "", nil

google/externalaccount/executablecredsource_test.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"context"
99
"encoding/json"
1010
"fmt"
11-
"io/ioutil"
1211
"os"
1312
"sort"
1413
"testing"
@@ -614,7 +613,7 @@ func TestRetrieveExecutableSubjectTokenSuccesses(t *testing.T) {
614613
}
615614

616615
func TestRetrieveOutputFileSubjectTokenNotJSON(t *testing.T) {
617-
outputFile, err := ioutil.TempFile("testdata", "result.*.json")
616+
outputFile, err := os.CreateTemp("testdata", "result.*.json")
618617
if err != nil {
619618
t.Fatalf("Tempfile failed: %v", err)
620619
}
@@ -763,7 +762,7 @@ var cacheFailureTests = []struct {
763762
func TestRetrieveOutputFileSubjectTokenFailureTests(t *testing.T) {
764763
for _, tt := range cacheFailureTests {
765764
t.Run(tt.name, func(t *testing.T) {
766-
outputFile, err := ioutil.TempFile("testdata", "result.*.json")
765+
outputFile, err := os.CreateTemp("testdata", "result.*.json")
767766
if err != nil {
768767
t.Fatalf("Tempfile failed: %v", err)
769768
}
@@ -866,7 +865,7 @@ var invalidCacheTests = []struct {
866865
func TestRetrieveOutputFileSubjectTokenInvalidCache(t *testing.T) {
867866
for _, tt := range invalidCacheTests {
868867
t.Run(tt.name, func(t *testing.T) {
869-
outputFile, err := ioutil.TempFile("testdata", "result.*.json")
868+
outputFile, err := os.CreateTemp("testdata", "result.*.json")
870869
if err != nil {
871870
t.Fatalf("Tempfile failed: %v", err)
872871
}
@@ -970,8 +969,7 @@ var cacheSuccessTests = []struct {
970969
func TestRetrieveOutputFileSubjectTokenJwt(t *testing.T) {
971970
for _, tt := range cacheSuccessTests {
972971
t.Run(tt.name, func(t *testing.T) {
973-
974-
outputFile, err := ioutil.TempFile("testdata", "result.*.json")
972+
outputFile, err := os.CreateTemp("testdata", "result.*.json")
975973
if err != nil {
976974
t.Fatalf("Tempfile failed: %v", err)
977975
}

google/externalaccount/filecredsource.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"errors"
1111
"fmt"
1212
"io"
13-
"io/ioutil"
1413
"os"
1514
)
1615

@@ -29,7 +28,7 @@ func (cs fileCredentialSource) subjectToken() (string, error) {
2928
return "", fmt.Errorf("oauth2/google/externalaccount: failed to open credential file %q", cs.File)
3029
}
3130
defer tokenFile.Close()
32-
tokenBytes, err := ioutil.ReadAll(io.LimitReader(tokenFile, 1<<20))
31+
tokenBytes, err := io.ReadAll(io.LimitReader(tokenFile, 1<<20))
3332
if err != nil {
3433
return "", fmt.Errorf("oauth2/google/externalaccount: failed to read credential file: %v", err)
3534
}

google/externalaccount/urlcredsource.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"errors"
1111
"fmt"
1212
"io"
13-
"io/ioutil"
1413
"net/http"
1514

1615
"golang.org/x/oauth2"
@@ -44,7 +43,7 @@ func (cs urlCredentialSource) subjectToken() (string, error) {
4443
}
4544
defer resp.Body.Close()
4645

47-
respBody, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
46+
respBody, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
4847
if err != nil {
4948
return "", fmt.Errorf("oauth2/google/externalaccount: invalid body in subject token URL query: %v", err)
5049
}

google/google.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -285,22 +285,18 @@ func (cs computeSource) Token() (*oauth2.Token, error) {
285285
if err != nil {
286286
return nil, err
287287
}
288-
var res struct {
289-
AccessToken string `json:"access_token"`
290-
ExpiresInSec int `json:"expires_in"`
291-
TokenType string `json:"token_type"`
292-
}
288+
var res oauth2.Token
293289
err = json.NewDecoder(strings.NewReader(tokenJSON)).Decode(&res)
294290
if err != nil {
295291
return nil, fmt.Errorf("oauth2/google: invalid token JSON from metadata: %v", err)
296292
}
297-
if res.ExpiresInSec == 0 || res.AccessToken == "" {
293+
if res.ExpiresIn == 0 || res.AccessToken == "" {
298294
return nil, fmt.Errorf("oauth2/google: incomplete token received from metadata")
299295
}
300296
tok := &oauth2.Token{
301297
AccessToken: res.AccessToken,
302298
TokenType: res.TokenType,
303-
Expiry: time.Now().Add(time.Duration(res.ExpiresInSec) * time.Second),
299+
Expiry: time.Now().Add(time.Duration(res.ExpiresIn) * time.Second),
304300
}
305301
// NOTE(cbro): add hidden metadata about where the token is from.
306302
// This is needed for detection by client libraries to know that credentials come from the metadata server.

google/internal/externalaccountauthorizeduser/externalaccountauthorizeduser_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"context"
99
"encoding/json"
1010
"errors"
11-
"io/ioutil"
11+
"io"
1212
"net/http"
1313
"net/http/httptest"
1414
"testing"
@@ -227,7 +227,7 @@ func (trts *testRefreshTokenServer) run(t *testing.T) (string, error) {
227227
if got, want := headerContentType, trts.ContentType; got != want {
228228
t.Errorf("got %v but want %v", got, want)
229229
}
230-
body, err := ioutil.ReadAll(r.Body)
230+
body, err := io.ReadAll(r.Body)
231231
if err != nil {
232232
t.Fatalf("Failed reading request body: %s.", err)
233233
}

google/internal/impersonate/impersonate.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"encoding/json"
1111
"fmt"
1212
"io"
13-
"io/ioutil"
1413
"net/http"
1514
"time"
1615

@@ -81,7 +80,7 @@ func (its ImpersonateTokenSource) Token() (*oauth2.Token, error) {
8180
return nil, fmt.Errorf("oauth2/google: unable to generate access token: %v", err)
8281
}
8382
defer resp.Body.Close()
84-
body, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
83+
body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
8584
if err != nil {
8685
return nil, fmt.Errorf("oauth2/google: unable to read body: %v", err)
8786
}

google/internal/stsexchange/sts_exchange.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"encoding/json"
1010
"fmt"
1111
"io"
12-
"io/ioutil"
1312
"net/http"
1413
"net/url"
1514
"strconv"
@@ -82,7 +81,7 @@ func makeRequest(ctx context.Context, endpoint string, data url.Values, authenti
8281
}
8382
defer resp.Body.Close()
8483

85-
body, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
84+
body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
8685
if err != nil {
8786
return nil, err
8887
}

0 commit comments

Comments
 (0)