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

[oauth] Fix a bug in http_token. #371

Merged
merged 2 commits into from
May 3, 2023
Merged
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
18 changes: 16 additions & 2 deletions common/oauth/http_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package oauth

import (
"bytes"
"encoding/json"
"fmt"
"io"
Expand All @@ -36,6 +35,21 @@ type httpTokenSource struct {
httpDo func(req *http.Request) (*http.Response, error)
}

// requestBody encapsulates the request body and implements the io.Reader()
// interface.
type requestBody struct {
b []byte
}

// Read implements the io.Reader interface. Instead of using buffered read,
// it simply copies the bytes to the provided slice in one go (depending on
// the input slice capacity) and returns io.EOF. Buffered reads require
// resetting the buffer before re-use, restricting our ability to reuse the
// request object and using it concurrently.
func (rb *requestBody) Read(p []byte) (int, error) {
return copy(p, rb.b), io.EOF
}

func redact(s string) string {
if len(s) < 50 {
return s
Expand Down Expand Up @@ -102,7 +116,7 @@ func setContentType(req *http.Request, data []string) {
func newHTTPTokenSource(c *configpb.HTTPRequest, refreshExpiryBuffer time.Duration, l *logger.Logger) (oauth2.TokenSource, error) {
data := strings.Join(c.GetData(), "&")

body := bytes.NewReader([]byte(data))
body := &requestBody{b: []byte(data)}

req, err := http.NewRequest(c.GetMethod(), c.GetTokenUrl(), body)
if err != nil {
Expand Down