forked from Mirrors/oauth2
google: remove Go 1.8 support
Assume Go 1.9+, which is broader than Go's current Go 1.10+ support policy. Change-Id: I9fe6954d21c2279cf4ea7da4d5bc7a9290a3bae2 Reviewed-on: https://go-review.googlesource.com/c/146677 Reviewed-by: Ross Light <light@google.com>
This commit is contained in:
parent
e0f2c55a7f
commit
232e455483
|
@ -18,6 +18,25 @@ import (
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Credentials holds Google credentials, including "Application Default Credentials".
|
||||||
|
// For more details, see:
|
||||||
|
// https://developers.google.com/accounts/docs/application-default-credentials
|
||||||
|
type Credentials struct {
|
||||||
|
ProjectID string // may be empty
|
||||||
|
TokenSource oauth2.TokenSource
|
||||||
|
|
||||||
|
// JSON contains the raw bytes from a JSON credentials file.
|
||||||
|
// This field may be nil if authentication is provided by the
|
||||||
|
// environment and not with a credentials file, e.g. when code is
|
||||||
|
// running on Google Cloud Platform.
|
||||||
|
JSON []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// DefaultCredentials is the old name of Credentials.
|
||||||
|
//
|
||||||
|
// Deprecated: use Credentials instead.
|
||||||
|
type DefaultCredentials = Credentials
|
||||||
|
|
||||||
// DefaultClient returns an HTTP Client that uses the
|
// DefaultClient returns an HTTP Client that uses the
|
||||||
// DefaultTokenSource to obtain authentication credentials.
|
// DefaultTokenSource to obtain authentication credentials.
|
||||||
func DefaultClient(ctx context.Context, scope ...string) (*http.Client, error) {
|
func DefaultClient(ctx context.Context, scope ...string) (*http.Client, error) {
|
||||||
|
@ -39,8 +58,23 @@ func DefaultTokenSource(ctx context.Context, scope ...string) (oauth2.TokenSourc
|
||||||
return creds.TokenSource, nil
|
return creds.TokenSource, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Common implementation for FindDefaultCredentials.
|
// FindDefaultCredentials searches for "Application Default Credentials".
|
||||||
func findDefaultCredentials(ctx context.Context, scopes []string) (*DefaultCredentials, error) {
|
//
|
||||||
|
// It looks for credentials in the following places,
|
||||||
|
// preferring the first location found:
|
||||||
|
//
|
||||||
|
// 1. A JSON file whose path is specified by the
|
||||||
|
// GOOGLE_APPLICATION_CREDENTIALS environment variable.
|
||||||
|
// 2. A JSON file in a location known to the gcloud command-line tool.
|
||||||
|
// On Windows, this is %APPDATA%/gcloud/application_default_credentials.json.
|
||||||
|
// On other systems, $HOME/.config/gcloud/application_default_credentials.json.
|
||||||
|
// 3. On Google App Engine standard first generation runtimes (<= Go 1.9) it uses
|
||||||
|
// the appengine.AccessToken function.
|
||||||
|
// 4. On Google Compute Engine, Google App Engine standard second generation runtimes
|
||||||
|
// (>= Go 1.11), and Google App Engine flexible environment, it fetches
|
||||||
|
// credentials from the metadata server.
|
||||||
|
// (In this final case any provided scopes are ignored.)
|
||||||
|
func FindDefaultCredentials(ctx context.Context, scopes ...string) (*Credentials, error) {
|
||||||
// First, try the environment variable.
|
// First, try the environment variable.
|
||||||
const envVar = "GOOGLE_APPLICATION_CREDENTIALS"
|
const envVar = "GOOGLE_APPLICATION_CREDENTIALS"
|
||||||
if filename := os.Getenv(envVar); filename != "" {
|
if filename := os.Getenv(envVar); filename != "" {
|
||||||
|
@ -84,8 +118,11 @@ func findDefaultCredentials(ctx context.Context, scopes []string) (*DefaultCrede
|
||||||
return nil, fmt.Errorf("google: could not find default credentials. See %v for more information.", url)
|
return nil, fmt.Errorf("google: could not find default credentials. See %v for more information.", url)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Common implementation for CredentialsFromJSON.
|
// CredentialsFromJSON obtains Google credentials from a JSON value. The JSON can
|
||||||
func credentialsFromJSON(ctx context.Context, jsonData []byte, scopes []string) (*DefaultCredentials, error) {
|
// represent either a Google Developers Console client_credentials.json file (as in
|
||||||
|
// ConfigFromJSON) or a Google Developers service account key file (as in
|
||||||
|
// JWTConfigFromJSON).
|
||||||
|
func CredentialsFromJSON(ctx context.Context, jsonData []byte, scopes ...string) (*Credentials, error) {
|
||||||
var f credentialsFile
|
var f credentialsFile
|
||||||
if err := json.Unmarshal(jsonData, &f); err != nil {
|
if err := json.Unmarshal(jsonData, &f); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -1,43 +0,0 @@
|
||||||
// Copyright 2018 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// +build !go1.9
|
|
||||||
|
|
||||||
// Package google provides support for making OAuth2 authorized and authenticated
|
|
||||||
// HTTP requests to Google APIs. It supports the Web server flow, client-side
|
|
||||||
// credentials, service accounts, Google Compute Engine service accounts, and Google
|
|
||||||
// App Engine service accounts.
|
|
||||||
//
|
|
||||||
// A brief overview of the package follows. For more information, please read
|
|
||||||
// https://developers.google.com/accounts/docs/OAuth2
|
|
||||||
// and
|
|
||||||
// https://developers.google.com/accounts/docs/application-default-credentials.
|
|
||||||
//
|
|
||||||
// OAuth2 Configs
|
|
||||||
//
|
|
||||||
// Two functions in this package return golang.org/x/oauth2.Config values from Google credential
|
|
||||||
// data. Google supports two JSON formats for OAuth2 credentials: one is handled by ConfigFromJSON,
|
|
||||||
// the other by JWTConfigFromJSON. The returned Config can be used to obtain a TokenSource or
|
|
||||||
// create an http.Client.
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// Credentials
|
|
||||||
//
|
|
||||||
// The DefaultCredentials type represents Google Application Default Credentials, as
|
|
||||||
// well as other forms of credential.
|
|
||||||
//
|
|
||||||
// Use FindDefaultCredentials to obtain Application Default Credentials.
|
|
||||||
// FindDefaultCredentials looks in some well-known places for a credentials file, and
|
|
||||||
// will call AppEngineTokenSource or ComputeTokenSource as needed.
|
|
||||||
//
|
|
||||||
// DefaultClient and DefaultTokenSource are convenience methods. They first call FindDefaultCredentials,
|
|
||||||
// then use the credentials to construct an http.Client or an oauth2.TokenSource.
|
|
||||||
//
|
|
||||||
// Use CredentialsFromJSON to obtain credentials from either of the two JSON
|
|
||||||
// formats described in OAuth2 Configs, above. (The DefaultCredentials returned may
|
|
||||||
// not be "Application Default Credentials".) The TokenSource in the returned value
|
|
||||||
// is the same as the one obtained from the oauth2.Config returned from
|
|
||||||
// ConfigFromJSON or JWTConfigFromJSON, but the DefaultCredentials may contain
|
|
||||||
// additional information that is useful is some circumstances.
|
|
||||||
package google // import "golang.org/x/oauth2/google"
|
|
|
@ -1,60 +0,0 @@
|
||||||
// Copyright 2018 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// +build go1.9
|
|
||||||
|
|
||||||
package google
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"golang.org/x/oauth2"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Credentials holds Google credentials, including "Application Default Credentials".
|
|
||||||
// For more details, see:
|
|
||||||
// https://developers.google.com/accounts/docs/application-default-credentials
|
|
||||||
type Credentials struct {
|
|
||||||
ProjectID string // may be empty
|
|
||||||
TokenSource oauth2.TokenSource
|
|
||||||
|
|
||||||
// JSON contains the raw bytes from a JSON credentials file.
|
|
||||||
// This field may be nil if authentication is provided by the
|
|
||||||
// environment and not with a credentials file, e.g. when code is
|
|
||||||
// running on Google Cloud Platform.
|
|
||||||
JSON []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
// DefaultCredentials is the old name of Credentials.
|
|
||||||
//
|
|
||||||
// Deprecated: use Credentials instead.
|
|
||||||
type DefaultCredentials = Credentials
|
|
||||||
|
|
||||||
// FindDefaultCredentials searches for "Application Default Credentials".
|
|
||||||
//
|
|
||||||
// It looks for credentials in the following places,
|
|
||||||
// preferring the first location found:
|
|
||||||
//
|
|
||||||
// 1. A JSON file whose path is specified by the
|
|
||||||
// GOOGLE_APPLICATION_CREDENTIALS environment variable.
|
|
||||||
// 2. A JSON file in a location known to the gcloud command-line tool.
|
|
||||||
// On Windows, this is %APPDATA%/gcloud/application_default_credentials.json.
|
|
||||||
// On other systems, $HOME/.config/gcloud/application_default_credentials.json.
|
|
||||||
// 3. On Google App Engine standard first generation runtimes (<= Go 1.9) it uses
|
|
||||||
// the appengine.AccessToken function.
|
|
||||||
// 4. On Google Compute Engine, Google App Engine standard second generation runtimes
|
|
||||||
// (>= Go 1.11), and Google App Engine flexible environment, it fetches
|
|
||||||
// credentials from the metadata server.
|
|
||||||
// (In this final case any provided scopes are ignored.)
|
|
||||||
func FindDefaultCredentials(ctx context.Context, scopes ...string) (*Credentials, error) {
|
|
||||||
return findDefaultCredentials(ctx, scopes)
|
|
||||||
}
|
|
||||||
|
|
||||||
// CredentialsFromJSON obtains Google credentials from a JSON value. The JSON can
|
|
||||||
// represent either a Google Developers Console client_credentials.json file (as in
|
|
||||||
// ConfigFromJSON) or a Google Developers service account key file (as in
|
|
||||||
// JWTConfigFromJSON).
|
|
||||||
func CredentialsFromJSON(ctx context.Context, jsonData []byte, scopes ...string) (*Credentials, error) {
|
|
||||||
return credentialsFromJSON(ctx, jsonData, scopes)
|
|
||||||
}
|
|
|
@ -1,57 +0,0 @@
|
||||||
// Copyright 2018 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// +build !go1.9
|
|
||||||
|
|
||||||
package google
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"golang.org/x/oauth2"
|
|
||||||
)
|
|
||||||
|
|
||||||
// DefaultCredentials holds Google credentials, including "Application Default Credentials".
|
|
||||||
// For more details, see:
|
|
||||||
// https://developers.google.com/accounts/docs/application-default-credentials
|
|
||||||
type DefaultCredentials struct {
|
|
||||||
ProjectID string // may be empty
|
|
||||||
TokenSource oauth2.TokenSource
|
|
||||||
|
|
||||||
// JSON contains the raw bytes from a JSON credentials file.
|
|
||||||
// This field may be nil if authentication is provided by the
|
|
||||||
// environment and not with a credentials file, e.g. when code is
|
|
||||||
// running on Google Cloud Platform.
|
|
||||||
JSON []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindDefaultCredentials searches for "Application Default Credentials".
|
|
||||||
//
|
|
||||||
// It looks for credentials in the following places,
|
|
||||||
// preferring the first location found:
|
|
||||||
//
|
|
||||||
// 1. A JSON file whose path is specified by the
|
|
||||||
// GOOGLE_APPLICATION_CREDENTIALS environment variable.
|
|
||||||
// 2. A JSON file in a location known to the gcloud command-line tool.
|
|
||||||
// On Windows, this is %APPDATA%/gcloud/application_default_credentials.json.
|
|
||||||
// On other systems, $HOME/.config/gcloud/application_default_credentials.json.
|
|
||||||
// 3. On Google App Engine standard first generation runtimes (<= Go 1.9) it uses
|
|
||||||
// the appengine.AccessToken function.
|
|
||||||
// 4. On Google Compute Engine, Google App Engine standard second generation runtimes
|
|
||||||
// (>= Go 1.11), and Google App Engine flexible environment, it fetches
|
|
||||||
// credentials from the metadata server.
|
|
||||||
// (In this final case any provided scopes are ignored.)
|
|
||||||
func FindDefaultCredentials(ctx context.Context, scopes ...string) (*DefaultCredentials, error) {
|
|
||||||
return findDefaultCredentials(ctx, scopes)
|
|
||||||
}
|
|
||||||
|
|
||||||
// CredentialsFromJSON obtains Google credentials from a JSON value. The JSON can
|
|
||||||
// represent either a Google Developers Console client_credentials.json file (as in
|
|
||||||
// ConfigFromJSON) or a Google Developers service account key file (as in
|
|
||||||
// JWTConfigFromJSON).
|
|
||||||
//
|
|
||||||
// Note: despite the name, the returned credentials may not be Application Default Credentials.
|
|
||||||
func CredentialsFromJSON(ctx context.Context, jsonData []byte, scopes ...string) (*DefaultCredentials, error) {
|
|
||||||
return credentialsFromJSON(ctx, jsonData, scopes)
|
|
||||||
}
|
|
Loading…
Reference in New Issue