Add Account constructors backed by oauth2.TokenSource - #467
Open
a2105z wants to merge 1 commit into
Open
Conversation
account.New stores a static bearer token, so long-lived apps cannot refresh credentials through Account. Add FromTokenSource and FromToken, resolve Authorization on each Fleet API request, and plumb a per-request auth header callback into inet.Connection so vehicles from GetVehicle also refresh. Existing New(string) behavior is unchanged. Fixes teslamotors#29
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #29.
Problem
account.New(oauthToken string, ...)stores a staticAuthorizationbearer. When the access token expires, every Fleet API call from that Account fails until the application throws the Account away and constructs a new one. That is a blocker for long-running services (as called out on the issue): there is no way to keep a long-livedAccountthat refreshes credentials.Stale PR #151 attempted this by making
Newtake aTokenSource(breaking) and by assigninghttp.DefaultClient.Transport = oauth2.Transport{...}, which mutates the process-wide default client. This PR is a fresh, non-breaking implementation that avoids that.Solution
account.FromTokenSource(ts oauth2.TokenSource, userAgent string)for refreshing credentialsaccount.FromToken(tok *oauth2.Token, userAgent string)convenience wrapperaccount.New(string, ...)unchanged for existing callers / the HTTP proxyoauth2.ReuseTokenSource)inet.Connection.SetAuthHeaderFuncsoGetVehicleconnections also fetch a current token per request (a header baked in atGetVehicletime would go stale)Example for a long-lived app:
@sethterashima @patrickdemers6 @agbpatro — please review. This unblocks long-lived Account usage without breaking
Newor mutatinghttp.DefaultClient.Type of change
Checklist:
Confirm you have completed the following steps:
Test plan
go test ./pkg/account/ ./pkg/connector/inet/ -count=1go test ./... -count=1FromTokenSourcerefreshes Authorization across requestsGetVehicleconnection uses auth callback (credential errors surface on Wakeup)New(string)still sends the static bearerSetAuthHeaderFuncsupplies a fresh header per inet Fleet API call