From 90155042cbc501d0b5c9badf50f1975dd2a7fdd0 Mon Sep 17 00:00:00 2001 From: Ross Light Date: Thu, 21 Dec 2017 14:01:18 -0800 Subject: [PATCH] internal: inline CondVal function Change-Id: Ic1115ab639e2d7b499c3400b5310575a36b1b796 Reviewed-on: https://go-review.googlesource.com/85320 Reviewed-by: Tim Cooper Reviewed-by: Brad Fitzpatrick --- clientcredentials/clientcredentials.go | 4 ++- internal/oauth2.go | 7 ------ oauth2.go | 35 ++++++++++++++++++-------- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/clientcredentials/clientcredentials.go b/clientcredentials/clientcredentials.go index 4afb631..c4e840d 100644 --- a/clientcredentials/clientcredentials.go +++ b/clientcredentials/clientcredentials.go @@ -82,7 +82,9 @@ type tokenSource struct { func (c *tokenSource) Token() (*oauth2.Token, error) { v := url.Values{ "grant_type": {"client_credentials"}, - "scope": internal.CondVal(strings.Join(c.conf.Scopes, " ")), + } + if len(c.conf.Scopes) > 0 { + v.Set("scope", strings.Join(c.conf.Scopes, " ")) } for k, p := range c.conf.EndpointParams { if _, ok := v[k]; ok { diff --git a/internal/oauth2.go b/internal/oauth2.go index 1aa51be..fc63fca 100644 --- a/internal/oauth2.go +++ b/internal/oauth2.go @@ -35,10 +35,3 @@ func ParseKey(key []byte) (*rsa.PrivateKey, error) { } return parsed, nil } - -func CondVal(v string) []string { - if v == "" { - return nil - } - return []string{v} -} diff --git a/oauth2.go b/oauth2.go index 4bafe87..6266950 100644 --- a/oauth2.go +++ b/oauth2.go @@ -129,9 +129,16 @@ func (c *Config) AuthCodeURL(state string, opts ...AuthCodeOption) string { v := url.Values{ "response_type": {"code"}, "client_id": {c.ClientID}, - "redirect_uri": internal.CondVal(c.RedirectURL), - "scope": internal.CondVal(strings.Join(c.Scopes, " ")), - "state": internal.CondVal(state), + } + if c.RedirectURL != "" { + v.Set("redirect_uri", c.RedirectURL) + } + if len(c.Scopes) > 0 { + v.Set("scope", strings.Join(c.Scopes, " ")) + } + if state != "" { + // TODO(light): Docs say never to omit state; don't allow empty. + v.Set("state", state) } for _, opt := range opts { opt.setValue(v) @@ -157,12 +164,15 @@ func (c *Config) AuthCodeURL(state string, opts ...AuthCodeOption) string { // The HTTP client to use is derived from the context. // If nil, http.DefaultClient is used. func (c *Config) PasswordCredentialsToken(ctx context.Context, username, password string) (*Token, error) { - return retrieveToken(ctx, c, url.Values{ + v := url.Values{ "grant_type": {"password"}, "username": {username}, "password": {password}, - "scope": internal.CondVal(strings.Join(c.Scopes, " ")), - }) + } + if len(c.Scopes) > 0 { + v.Set("scope", strings.Join(c.Scopes, " ")) + } + return retrieveToken(ctx, c, v) } // Exchange converts an authorization code into a token. @@ -176,11 +186,14 @@ func (c *Config) PasswordCredentialsToken(ctx context.Context, username, passwor // The code will be in the *http.Request.FormValue("code"). Before // calling Exchange, be sure to validate FormValue("state"). func (c *Config) Exchange(ctx context.Context, code string) (*Token, error) { - return retrieveToken(ctx, c, url.Values{ - "grant_type": {"authorization_code"}, - "code": {code}, - "redirect_uri": internal.CondVal(c.RedirectURL), - }) + v := url.Values{ + "grant_type": {"authorization_code"}, + "code": {code}, + } + if c.RedirectURL != "" { + v.Set("redirect_uri", c.RedirectURL) + } + return retrieveToken(ctx, c, v) } // Client returns an HTTP client using the provided token.