Skip to content

Commit 7bc2fb6

Browse files
committed
oauth: fix oauth token race in http_transport (#1269)
(cherry picked from commit 8711473)
1 parent 7ca92df commit 7bc2fb6

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

internal/oauth/http_transport.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ var _ http.Transport
1212
var _ http.RoundTripper = (*Transport)(nil)
1313

1414
type Transport struct {
15-
Base http.RoundTripper
15+
Base http.RoundTripper
16+
//Token is a OAuth token (which has a refresh token) that should be used during roundtrip to automatically
17+
//refresh the OAuth access token once the current one has expired or is soon to expire
1618
Token *Token
1719

20+
//mu is a mutex that should be acquired whenever token used
1821
mu sync.Mutex
1922
}
2023

@@ -29,16 +32,25 @@ func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
2932
if err := t.refreshToken(ctx); err != nil {
3033
return nil, err
3134
}
35+
token := t.getToken()
3236

3337
req2 := req.Clone(req.Context())
34-
req2.Header.Set("Authorization", "Bearer "+t.Token.AccessToken)
38+
req2.Header.Set("Authorization", "Bearer "+token.AccessToken)
3539

3640
if t.Base != nil {
3741
return t.Base.RoundTrip(req2)
3842
}
3943
return http.DefaultTransport.RoundTrip(req2)
4044
}
4145

46+
// getToken returns a value copy of token and is guarded by a mutex
47+
func (t *Transport) getToken() Token {
48+
t.mu.Lock()
49+
defer t.mu.Unlock()
50+
51+
return *t.Token
52+
}
53+
4254
// refreshToken checks if the token has expired or expiring soon and refreshes it. Once the token is
4355
// refreshed, the in-memory token is updated and a best effort is made to store the token.
4456
// If storing the token fails, no error is returned.

0 commit comments

Comments
 (0)