internal: inline CondVal function

Change-Id: Ic1115ab639e2d7b499c3400b5310575a36b1b796
Reviewed-on: https://go-review.googlesource.com/85320
Reviewed-by: Tim Cooper <tim.cooper@layeh.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Ross Light 2017-12-21 14:01:18 -08:00
parent 174986b227
commit 90155042cb
3 changed files with 27 additions and 19 deletions

View File

@ -82,7 +82,9 @@ type tokenSource struct {
func (c *tokenSource) Token() (*oauth2.Token, error) { func (c *tokenSource) Token() (*oauth2.Token, error) {
v := url.Values{ v := url.Values{
"grant_type": {"client_credentials"}, "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 { for k, p := range c.conf.EndpointParams {
if _, ok := v[k]; ok { if _, ok := v[k]; ok {

View File

@ -35,10 +35,3 @@ func ParseKey(key []byte) (*rsa.PrivateKey, error) {
} }
return parsed, nil return parsed, nil
} }
func CondVal(v string) []string {
if v == "" {
return nil
}
return []string{v}
}

View File

@ -129,9 +129,16 @@ func (c *Config) AuthCodeURL(state string, opts ...AuthCodeOption) string {
v := url.Values{ v := url.Values{
"response_type": {"code"}, "response_type": {"code"},
"client_id": {c.ClientID}, "client_id": {c.ClientID},
"redirect_uri": internal.CondVal(c.RedirectURL), }
"scope": internal.CondVal(strings.Join(c.Scopes, " ")), if c.RedirectURL != "" {
"state": internal.CondVal(state), 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 { for _, opt := range opts {
opt.setValue(v) 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. // The HTTP client to use is derived from the context.
// If nil, http.DefaultClient is used. // If nil, http.DefaultClient is used.
func (c *Config) PasswordCredentialsToken(ctx context.Context, username, password string) (*Token, error) { func (c *Config) PasswordCredentialsToken(ctx context.Context, username, password string) (*Token, error) {
return retrieveToken(ctx, c, url.Values{ v := url.Values{
"grant_type": {"password"}, "grant_type": {"password"},
"username": {username}, "username": {username},
"password": {password}, "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. // 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 // The code will be in the *http.Request.FormValue("code"). Before
// calling Exchange, be sure to validate FormValue("state"). // calling Exchange, be sure to validate FormValue("state").
func (c *Config) Exchange(ctx context.Context, code string) (*Token, error) { func (c *Config) Exchange(ctx context.Context, code string) (*Token, error) {
return retrieveToken(ctx, c, url.Values{ v := url.Values{
"grant_type": {"authorization_code"}, "grant_type": {"authorization_code"},
"code": {code}, "code": {code},
"redirect_uri": internal.CondVal(c.RedirectURL), }
}) if c.RedirectURL != "" {
v.Set("redirect_uri", c.RedirectURL)
}
return retrieveToken(ctx, c, v)
} }
// Client returns an HTTP client using the provided token. // Client returns an HTTP client using the provided token.