1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
17 "cloud.google.com/go/compute/metadata"
21 // Credentials holds Google credentials, including "Application Default Credentials".
22 // For more details, see:
23 // https://developers.google.com/accounts/docs/application-default-credentials
24 type Credentials struct {
25 ProjectID string // may be empty
26 TokenSource oauth2.TokenSource
28 // JSON contains the raw bytes from a JSON credentials file.
29 // This field may be nil if authentication is provided by the
30 // environment and not with a credentials file, e.g. when code is
31 // running on Google Cloud Platform.
35 // DefaultCredentials is the old name of Credentials.
37 // Deprecated: use Credentials instead.
38 type DefaultCredentials = Credentials
40 // DefaultClient returns an HTTP Client that uses the
41 // DefaultTokenSource to obtain authentication credentials.
42 func DefaultClient(ctx context.Context, scope ...string) (*http.Client, error) {
43 ts, err := DefaultTokenSource(ctx, scope...)
47 return oauth2.NewClient(ctx, ts), nil
50 // DefaultTokenSource returns the token source for
51 // "Application Default Credentials".
52 // It is a shortcut for FindDefaultCredentials(ctx, scope).TokenSource.
53 func DefaultTokenSource(ctx context.Context, scope ...string) (oauth2.TokenSource, error) {
54 creds, err := FindDefaultCredentials(ctx, scope...)
58 return creds.TokenSource, nil
61 // FindDefaultCredentials searches for "Application Default Credentials".
63 // It looks for credentials in the following places,
64 // preferring the first location found:
66 // 1. A JSON file whose path is specified by the
67 // GOOGLE_APPLICATION_CREDENTIALS environment variable.
68 // 2. A JSON file in a location known to the gcloud command-line tool.
69 // On Windows, this is %APPDATA%/gcloud/application_default_credentials.json.
70 // On other systems, $HOME/.config/gcloud/application_default_credentials.json.
71 // 3. On Google App Engine standard first generation runtimes (<= Go 1.9) it uses
72 // the appengine.AccessToken function.
73 // 4. On Google Compute Engine, Google App Engine standard second generation runtimes
74 // (>= Go 1.11), and Google App Engine flexible environment, it fetches
75 // credentials from the metadata server.
76 func FindDefaultCredentials(ctx context.Context, scopes ...string) (*Credentials, error) {
77 // First, try the environment variable.
78 const envVar = "GOOGLE_APPLICATION_CREDENTIALS"
79 if filename := os.Getenv(envVar); filename != "" {
80 creds, err := readCredentialsFile(ctx, filename, scopes)
82 return nil, fmt.Errorf("google: error getting credentials using %v environment variable: %v", envVar, err)
87 // Second, try a well-known file.
88 filename := wellKnownFile()
89 if creds, err := readCredentialsFile(ctx, filename, scopes); err == nil {
91 } else if !os.IsNotExist(err) {
92 return nil, fmt.Errorf("google: error getting credentials using well-known file (%v): %v", filename, err)
95 // Third, if we're on a Google App Engine standard first generation runtime (<= Go 1.9)
96 // use those credentials. App Engine standard second generation runtimes (>= Go 1.11)
97 // and App Engine flexible use ComputeTokenSource and the metadata server.
98 if appengineTokenFunc != nil {
99 return &DefaultCredentials{
100 ProjectID: appengineAppIDFunc(ctx),
101 TokenSource: AppEngineTokenSource(ctx, scopes...),
105 // Fourth, if we're on Google Compute Engine, an App Engine standard second generation runtime,
106 // or App Engine flexible, use the metadata server.
107 if metadata.OnGCE() {
108 id, _ := metadata.ProjectID()
109 return &DefaultCredentials{
111 TokenSource: ComputeTokenSource("", scopes...),
115 // None are found; return helpful error.
116 const url = "https://developers.google.com/accounts/docs/application-default-credentials"
117 return nil, fmt.Errorf("google: could not find default credentials. See %v for more information.", url)
120 // CredentialsFromJSON obtains Google credentials from a JSON value. The JSON can
121 // represent either a Google Developers Console client_credentials.json file (as in
122 // ConfigFromJSON) or a Google Developers service account key file (as in
123 // JWTConfigFromJSON).
124 func CredentialsFromJSON(ctx context.Context, jsonData []byte, scopes ...string) (*Credentials, error) {
125 var f credentialsFile
126 if err := json.Unmarshal(jsonData, &f); err != nil {
129 ts, err := f.tokenSource(ctx, append([]string(nil), scopes...))
133 return &DefaultCredentials{
134 ProjectID: f.ProjectID,
140 func wellKnownFile() string {
141 const f = "application_default_credentials.json"
142 if runtime.GOOS == "windows" {
143 return filepath.Join(os.Getenv("APPDATA"), "gcloud", f)
145 return filepath.Join(guessUnixHomeDir(), ".config", "gcloud", f)
148 func readCredentialsFile(ctx context.Context, filename string, scopes []string) (*DefaultCredentials, error) {
149 b, err := ioutil.ReadFile(filename)
153 return CredentialsFromJSON(ctx, b, scopes...)