Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oauth2/google/stsexchange: provide response body using RetrieveError #742

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions google/externalaccount/basecredentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ package externalaccount

import (
"context"
"errors"
"fmt"
"net/http"
"regexp"
Expand All @@ -119,6 +120,7 @@ import (
"golang.org/x/oauth2"
"golang.org/x/oauth2/google/internal/impersonate"
"golang.org/x/oauth2/google/internal/stsexchange"
"golang.org/x/oauth2/internal"
)

const (
Expand Down Expand Up @@ -464,6 +466,10 @@ func (ts tokenSource) Token() (*oauth2.Token, error) {
}
stsResp, err := stsexchange.ExchangeToken(ts.ctx, conf.TokenURL, &stsRequest, clientAuth, header, options)
if err != nil {
var rErr *internal.RetrieveError
if errors.As(err, &rErr) {
return nil, (*oauth2.RetrieveError)(rErr)
}
return nil, err
}

Expand Down
6 changes: 5 additions & 1 deletion google/internal/stsexchange/sts_exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"strings"

"golang.org/x/oauth2"
"golang.org/x/oauth2/internal"
)

func defaultHeader() http.Header {
Expand Down Expand Up @@ -87,7 +88,10 @@ func makeRequest(ctx context.Context, endpoint string, data url.Values, authenti
return nil, err
}
if c := resp.StatusCode; c < 200 || c > 299 {
return nil, fmt.Errorf("oauth2/google: status code %d: %s", c, body)
return nil, &internal.RetrieveError{
Response: resp,
Body: body,
}
}
var stsResp Response
err = json.Unmarshal(body, &stsResp)
Expand Down
75 changes: 75 additions & 0 deletions sts_exchange_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package oauth2_test

import (
"bytes"
"context"
"errors"
"golang.org/x/oauth2"
"io"
"net/http"
"testing"

"golang.org/x/oauth2/google/externalaccount"
)

var _ externalaccount.SubjectTokenSupplier = fakeSupplier{}

type fakeSupplier struct{}

func (f fakeSupplier) SubjectToken(_ context.Context, _ externalaccount.SupplierOptions) (string, error) {
return "test-token", nil
}

var _ http.RoundTripper = fakeRT{}

type fakeRT struct {
body string
}

func (f fakeRT) RoundTrip(_ *http.Request) (*http.Response, error) {
status := http.StatusUnauthorized
return &http.Response{
StatusCode: status,
Body: io.NopCloser(bytes.NewReader([]byte(f.body))),
}, nil
}

func TestSTSExchange_error_handling(t *testing.T) {
t.Parallel()

// Arrange
body := `{"reason": "client does not exist"}`
client := &http.Client{Transport: fakeRT{body: body}}
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, client)

source, err := externalaccount.NewTokenSource(ctx, externalaccount.Config{
Audience: "aud",
SubjectTokenType: "test-token",
TokenURL: "url",
Scopes: []string{},
SubjectTokenSupplier: fakeSupplier{},
})
if err != nil {
t.Errorf("got unexpected error while token source building: %s", err)
}

// Act
_, err = source.Token()

// Assert
if err == nil {
t.Errorf("expected token issuance error")
}
var retrieveErr *oauth2.RetrieveError
if !errors.As(err, &retrieveErr) {
t.Errorf("expected an instance of RetrieveError, got error: %s", err)
}

if string(retrieveErr.Body) != body {
t.Errorf("expected body content `%s`, got: `%s`", body, retrieveErr.Body)
}

if retrieveErr.Response.StatusCode != http.StatusUnauthorized {
t.Errorf("expected unathorized status code, got: %s", retrieveErr.ErrorCode)
}
}