-
Notifications
You must be signed in to change notification settings - Fork 992
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
TokenSource.Token method should take in a Context #262
Comments
How would we make that change? We obviously can't change |
https://golang.org/cl/85937 is my WIP CL to add this, which includes a gradual migration strategy, allowing both the new and the old interfaces to coexist. |
Hello, Any plans to continue work on this? Thanks, |
I'm still working on this, but am waiting for code review. |
I have implemented a version of this idea at github.com/jfcote87/oauth2, |
Has there been any progress towards enabling this kind of API-breaking improvement in this project? e.g. adoption of semver etc |
When using GCP instance metadata based tokens these requests to will not include context deadlines and cannot be instrumented for tracing as the Being able to inject an HTTP client and propagate a context would improve tracing of API requests that result in token fetches. |
Having a golang oauth2 library which does not respect the deadlines set in the context for a request if a new token must be retrieved is simply sad... Why not simply introduce a TokenSourceWithContext interface? |
I wrote this fork to handle that problem, github.com/jfcote87/oauth2. It
is in production.
…On Tue, Jun 21, 2022 at 9:45 AM jens1205 ***@***.***> wrote:
Having a golang oauth2 library which does not respect the deadlines set in
the context for a request if a new token must be retrieved is simply sad...
Why not simply introduce a TokenSourceWithContext interface?
—
Reply to this email directly, view it on GitHub
<#262 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAM5474BPHJMLKOGG3HWT73VQHBQDANCNFSM4EJXQK7Q>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Resurrecting this issue. I was going to open a new golang proposal, but now that I found this issue I'll start here and see if people have comments. I believe we can get the problem solved more simply than the proposed change above by introducing an optional ContextTokenSource interface that can be implemented by TokenSource implementations. Callers needing to call Token() with an available context can check that this interface is implemented and use it if it is. The existing TokenSource implementations in the oauth2 package seem like they can easily implement this interface while maintaining backward compatibility. // A ContextTokenSource is anything that can return a token. All
// TokenSource implementations in this package implement ContextTokenSource.
// This allows propagation of a context, such as a caller's request context,
// to the token refresh request. It is used by the HTTP clients returned
// by NewClient and config.Client to propagate the request context to
// token refreshes. It should generally be used like this:
//
// // All TokenSource implementations in this package implement
// // ContextTokenSource.
// ts := config.TokenSource(ctx, token).(oauth2.ContextTokenSource)
// ts.TokenContext(ctx)
//
// // otherwise, check for the interface:
// if cts, ok := ts.(oauth2.ContextTokenSource); ok {
// token, err := cts.TokenContext(ctx)
// } else {
// token, err := cts.Token()
// }
//
// If an HTTP client is provided in the context, it will be ignored.
type ContextTokenSource interface {
TokenSource
// Token returns a token or an error.
// Token must be safe for concurrent use by multiple goroutines.
// The returned Token must not be modified.
// If a context is not available, prefer Token() rather than
// introduce a new context such as context.Background()
// since implementations may have different behavior.
TokenContext(context.Context) (*Token, error)
} No further API changes should be needed. It looks like these oauth2 types and functions will need to be updated to support this:
Concerns I already see:
Thoughts? |
I've implemented the above at master...dnesting:oauth2:dnesting/token-context if anyone wants to experiment. |
This module has not yet reached v1 and is hosted under the Can we just make this a breaking change? |
@dnesting I also independently wrote a a similar change w/ ContextTokenSource and TokenContext. I made different choices, however:
I think the value of doing this outweighs the potential pitfalls. The status quo means general flakiness when people are doing perfectly reasonable things; most notably, it is very weird that in-request token refreshes don't honor request deadlines and cancelation. (now that I think about it, perhaps another approach is to have the ContextTokenSource but only attach the cancelation and deadline of the request token, but use the create-time token for all token values?) @alexrudd the oauth2 package is way too widely-used to make a breaking change. It may not have a v1 but it has been stable for many, many years. That's an implicit expectation of stability. |
Yeah, not opposed to any of this. I do think documentation is either wrong ( The only real functional difference I see is that this no longer works: ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient)
cl := config.Client(ctx, tok)
cl.Do(...) When refreshing tokens, this now calls |
Just adding my two cents here. I recently ran into this problem with the Databricks SDK. We had a server where we passed the HTTP request context to the SDK; they use IMO, I'd say more people are likely to do what I did by passing in the request context vs using context to pass the HTTP client around. |
Also just wanted to point out that https://go-review.googlesource.com/c/oauth2/+/493335, which updates documentation to match current behavior, is currently seeing some activity. If anyone wants to rely on our ability to change undocumented behavior, I recommend weighing in on that CL before it becomes documented. |
TokenSource.Token
can make an HTTP request to obtain a new token. However, it does not accept aContext
. This makes it difficult to pass through tracing information (mentioned in #259) or cancel requests. While 626d87b addressed the concern in some cases, it requires that a TokenSource be created perContext
.Relation to other issues:
*http.Client
throughContext
, a different concern.The text was updated successfully, but these errors were encountered: