forked from Mirrors/oauth2
google: fix more nits and return early in one error case.
Change-Id: Idafbe36f4add6998e57878fec84a1080ac962511
This commit is contained in:
parent
717b310560
commit
4bcf502a22
|
@ -1,4 +1,4 @@
|
||||||
// Copyright 2015 The Go Authors. All rights reserved.
|
// Copyright 2020 The Go Authors. All rights reserved.
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
@ -29,15 +29,15 @@ func ExchangeToken(ctx context.Context, endpoint string, request *STSTokenExchan
|
||||||
data.Set("subject_token", request.SubjectToken)
|
data.Set("subject_token", request.SubjectToken)
|
||||||
data.Set("scope", strings.Join(request.Scope, " "))
|
data.Set("scope", strings.Join(request.Scope, " "))
|
||||||
opts, err := json.Marshal(options)
|
opts, err := json.Marshal(options)
|
||||||
if err == nil {
|
if err != nil {
|
||||||
data.Set("options", string(opts))
|
|
||||||
} else {
|
|
||||||
fmt.Errorf("oauth2/google: failed to marshal additional options: %v", err)
|
fmt.Errorf("oauth2/google: failed to marshal additional options: %v", err)
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
data.Set("options", string(opts))
|
||||||
|
|
||||||
authentication.InjectAuthentication(data, headers)
|
authentication.InjectAuthentication(data, headers)
|
||||||
encodedData := data.Encode()
|
encodedData := data.Encode()
|
||||||
req, err := http.NewRequest("POST", endpoint, strings.NewReader(encodedData))
|
req, err := http.NewRequestWithContext(ctx, "POST", endpoint, strings.NewReader(encodedData))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Errorf("oauth2/google: failed to properly build http request: %v", err)
|
fmt.Errorf("oauth2/google: failed to properly build http request: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -60,7 +60,7 @@ func ExchangeToken(ctx context.Context, endpoint string, request *STSTokenExchan
|
||||||
var stsResp STSTokenExchangeResponse
|
var stsResp STSTokenExchangeResponse
|
||||||
err = json.Unmarshal(body, &stsResp)
|
err = json.Unmarshal(body, &stsResp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Errorf("oauth2/google: failed to unmarshal response body from STS server: %v", err)
|
fmt.Errorf("oauth2/google: failed to unmarshal response body from Secure Token Server: %v", err)
|
||||||
}
|
}
|
||||||
return &stsResp, nil
|
return &stsResp, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
// Copyright 2015 The Go Authors. All rights reserved.
|
// Copyright 2020 The Go Authors. All rights reserved.
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
@ -75,7 +75,7 @@ func TestExchangeToken(t *testing.T) {
|
||||||
|
|
||||||
resp, err := ExchangeToken(context.Background(), ts.URL, &tokenRequest, auth, headers, nil)
|
resp, err := ExchangeToken(context.Background(), ts.URL, &tokenRequest, auth, headers, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("ExchangeToken failed with error: %s", err)
|
t.Fatalf("ExchangeToken failed with error: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if diff := cmp.Diff(expectedToken, *resp); diff != "" {
|
if diff := cmp.Diff(expectedToken, *resp); diff != "" {
|
||||||
|
|
Loading…
Reference in New Issue