You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

55 lines
2.2 KiB

  1. // Copyright 2018 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.
  4. // +build !go1.9
  5. package google
  6. import (
  7. "golang.org/x/net/context"
  8. "golang.org/x/oauth2"
  9. )
  10. // DefaultCredentials holds Google credentials, including "Application Default Credentials".
  11. // For more details, see:
  12. // https://developers.google.com/accounts/docs/application-default-credentials
  13. type DefaultCredentials struct {
  14. ProjectID string // may be empty
  15. TokenSource oauth2.TokenSource
  16. // JSON contains the raw bytes from a JSON credentials file.
  17. // This field may be nil if authentication is provided by the
  18. // environment and not with a credentials file, e.g. when code is
  19. // running on Google Cloud Platform.
  20. JSON []byte
  21. }
  22. // FindDefaultCredentials searches for "Application Default Credentials".
  23. //
  24. // It looks for credentials in the following places,
  25. // preferring the first location found:
  26. //
  27. // 1. A JSON file whose path is specified by the
  28. // GOOGLE_APPLICATION_CREDENTIALS environment variable.
  29. // 2. A JSON file in a location known to the gcloud command-line tool.
  30. // On Windows, this is %APPDATA%/gcloud/application_default_credentials.json.
  31. // On other systems, $HOME/.config/gcloud/application_default_credentials.json.
  32. // 3. On Google App Engine it uses the appengine.AccessToken function.
  33. // 4. On Google Compute Engine and Google App Engine Managed VMs, it fetches
  34. // credentials from the metadata server.
  35. // (In this final case any provided scopes are ignored.)
  36. func FindDefaultCredentials(ctx context.Context, scopes ...string) (*DefaultCredentials, error) {
  37. return findDefaultCredentials(ctx, scopes)
  38. }
  39. // CredentialsFromJSON obtains Google credentials from a JSON value. The JSON can
  40. // represent either a Google Developers Console client_credentials.json file (as in
  41. // ConfigFromJSON) or a Google Developers service account key file (as in
  42. // JWTConfigFromJSON).
  43. //
  44. // Note: despite the name, the returned credentials may not be Application Default Credentials.
  45. func CredentialsFromJSON(ctx context.Context, jsonData []byte, scopes ...string) (*DefaultCredentials, error) {
  46. return credentialsFromJSON(ctx, jsonData, scopes)
  47. }