Skip to content

Commit

Permalink
auth: remove contextwithHTTPClient
Browse files Browse the repository at this point in the history
Setting a custom Transport with no ability for the caller to configure
it breaks the ability to configure an http proxy, while the default
behavior via http.DefaultTransport is to support it (via env),
see https://pkg.go.dev/net/http#RoundTripper.

This was added because Spotify didn't support HTTP/2 in 2017, but it
does now. Also, Go net/http should auto negotiate better.
  • Loading branch information
StalkR committed Dec 27, 2021
1 parent 40fdc53 commit 6d24f9f
Showing 1 changed file with 3 additions and 15 deletions.
18 changes: 3 additions & 15 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package spotifyauth

import (
"context"
"crypto/tls"
"errors"
"net/http"
"os"
Expand Down Expand Up @@ -140,17 +139,6 @@ func New(opts ...AuthenticatorOption) *Authenticator {
return a
}

// contextWithHTTPClient returns a context with a value set to override the oauth2 http client as Spotify does not
// support HTTP/2
//
// see: https://github.com/zmb3/spotify/issues/20
func contextWithHTTPClient(ctx context.Context) context.Context {
tr := &http.Transport{
TLSNextProto: map[string]func(authority string, c *tls.Conn) http.RoundTripper{},
}
return context.WithValue(ctx, oauth2.HTTPClient, &http.Client{Transport: tr})
}

// ShowDialog forces the user to approve the app, even if they have already done so.
// Without this, users who have already approved the app are immediately redirected to the redirect uri.
var ShowDialog = oauth2.SetAuthURLParam("show_dialog", "true")
Expand Down Expand Up @@ -180,17 +168,17 @@ func (a Authenticator) Token(ctx context.Context, state string, r *http.Request,
if actualState != state {
return nil, errors.New("spotify: redirect state parameter doesn't match")
}
return a.config.Exchange(contextWithHTTPClient(ctx), code, opts...)
return a.config.Exchange(ctx, code, opts...)
}

// Exchange is like Token, except it allows you to manually specify the access
// code instead of pulling it out of an HTTP request.
func (a Authenticator) Exchange(ctx context.Context, code string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error) {
return a.config.Exchange(contextWithHTTPClient(ctx), code, opts...)
return a.config.Exchange(ctx, code, opts...)
}

// Client creates a *http.Client that will use the specified access token for its API requests.
// Combine this with spotify.HTTPClientOpt.
func (a Authenticator) Client(ctx context.Context, token *oauth2.Token) *http.Client {
return a.config.Client(contextWithHTTPClient(ctx), token)
return a.config.Client(ctx, token)
}

0 comments on commit 6d24f9f

Please sign in to comment.