From 039317f6579d4c0bf6595162cc159d7da0161908 Mon Sep 17 00:00:00 2001 From: Drew O'Meara Date: Tue, 23 May 2023 09:26:49 -0500 Subject: [PATCH] func (a Authenticator) Token() more portable --- auth/auth.go | 7 ++++--- examples/authenticate/authcode/authenticate.go | 2 +- examples/authenticate/pkce/pkce.go | 2 +- examples/player/player.go | 2 +- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/auth/auth.go b/auth/auth.go index 2ba2c3c..845eaf4 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -4,6 +4,7 @@ import ( "context" "errors" "net/http" + "net/url" "os" "golang.org/x/oauth2" @@ -152,11 +153,11 @@ func (a Authenticator) AuthURL(state string, opts ...oauth2.AuthCodeOption) stri return a.config.AuthCodeURL(state, opts...) } -// Token pulls an authorization code from an HTTP request and attempts to exchange +// Token pulls an authorization code from a redirected URL and attempts to exchange // it for an access token. The standard use case is to call Token from the handler // that handles requests to your application's redirect URL. -func (a Authenticator) Token(ctx context.Context, state string, r *http.Request, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error) { - values := r.URL.Query() +func (a Authenticator) Token(ctx context.Context, state string, redirect *url.URL, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error) { + values := redirect.Query() if e := values.Get("error"); e != "" { return nil, errors.New("spotify: auth failed - " + e) } diff --git a/examples/authenticate/authcode/authenticate.go b/examples/authenticate/authcode/authenticate.go index 7247f94..816027b 100644 --- a/examples/authenticate/authcode/authenticate.go +++ b/examples/authenticate/authcode/authenticate.go @@ -56,7 +56,7 @@ func main() { } func completeAuth(w http.ResponseWriter, r *http.Request) { - tok, err := auth.Token(r.Context(), state, r) + tok, err := auth.Token(r.Context(), state, r.URL) if err != nil { http.Error(w, "Couldn't get token", http.StatusForbidden) log.Fatal(err) diff --git a/examples/authenticate/pkce/pkce.go b/examples/authenticate/pkce/pkce.go index ef1f228..3f6fc6a 100644 --- a/examples/authenticate/pkce/pkce.go +++ b/examples/authenticate/pkce/pkce.go @@ -60,7 +60,7 @@ func main() { } func completeAuth(w http.ResponseWriter, r *http.Request) { - tok, err := auth.Token(r.Context(), state, r, + tok, err := auth.Token(r.Context(), state, r.URL, oauth2.SetAuthURLParam("code_verifier", codeVerifier)) if err != nil { http.Error(w, "Couldn't get token", http.StatusForbidden) diff --git a/examples/player/player.go b/examples/player/player.go index bcc6540..069bf14 100644 --- a/examples/player/player.go +++ b/examples/player/player.go @@ -105,7 +105,7 @@ func main() { } func completeAuth(w http.ResponseWriter, r *http.Request) { - tok, err := auth.Token(r.Context(), state, r) + tok, err := auth.Token(r.Context(), state, r.URL) if err != nil { http.Error(w, "Couldn't get token", http.StatusForbidden) log.Fatal(err)