creds.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2017 Google LLC.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package internal
  5. import (
  6. "context"
  7. "encoding/json"
  8. "fmt"
  9. "io/ioutil"
  10. "golang.org/x/oauth2"
  11. "golang.org/x/oauth2/google"
  12. )
  13. // Creds returns credential information obtained from DialSettings, or if none, then
  14. // it returns default credential information.
  15. func Creds(ctx context.Context, ds *DialSettings) (*google.Credentials, error) {
  16. if ds.Credentials != nil {
  17. return ds.Credentials, nil
  18. }
  19. if ds.CredentialsJSON != nil {
  20. return credentialsFromJSON(ctx, ds.CredentialsJSON, ds.Endpoint, ds.Scopes, ds.Audiences)
  21. }
  22. if ds.CredentialsFile != "" {
  23. data, err := ioutil.ReadFile(ds.CredentialsFile)
  24. if err != nil {
  25. return nil, fmt.Errorf("cannot read credentials file: %v", err)
  26. }
  27. return credentialsFromJSON(ctx, data, ds.Endpoint, ds.Scopes, ds.Audiences)
  28. }
  29. if ds.TokenSource != nil {
  30. return &google.Credentials{TokenSource: ds.TokenSource}, nil
  31. }
  32. cred, err := google.FindDefaultCredentials(ctx, ds.Scopes...)
  33. if err != nil {
  34. return nil, err
  35. }
  36. if len(cred.JSON) > 0 {
  37. return credentialsFromJSON(ctx, cred.JSON, ds.Endpoint, ds.Scopes, ds.Audiences)
  38. }
  39. // For GAE and GCE, the JSON is empty so return the default credentials directly.
  40. return cred, nil
  41. }
  42. // JSON key file type.
  43. const (
  44. serviceAccountKey = "service_account"
  45. )
  46. // credentialsFromJSON returns a google.Credentials based on the input.
  47. //
  48. // - If the JSON is a service account and no scopes provided, returns self-signed JWT auth flow
  49. // - Otherwise, returns OAuth 2.0 flow.
  50. func credentialsFromJSON(ctx context.Context, data []byte, endpoint string, scopes []string, audiences []string) (*google.Credentials, error) {
  51. cred, err := google.CredentialsFromJSON(ctx, data, scopes...)
  52. if err != nil {
  53. return nil, err
  54. }
  55. if len(data) > 0 && len(scopes) == 0 {
  56. var f struct {
  57. Type string `json:"type"`
  58. // The rest JSON fields are omitted because they are not used.
  59. }
  60. if err := json.Unmarshal(cred.JSON, &f); err != nil {
  61. return nil, err
  62. }
  63. if f.Type == serviceAccountKey {
  64. ts, err := selfSignedJWTTokenSource(data, endpoint, audiences)
  65. if err != nil {
  66. return nil, err
  67. }
  68. cred.TokenSource = ts
  69. }
  70. }
  71. return cred, err
  72. }
  73. func selfSignedJWTTokenSource(data []byte, endpoint string, audiences []string) (oauth2.TokenSource, error) {
  74. // Use the API endpoint as the default audience
  75. audience := endpoint
  76. if len(audiences) > 0 {
  77. // TODO(shinfan): Update golang oauth to support multiple audiences.
  78. if len(audiences) > 1 {
  79. return nil, fmt.Errorf("multiple audiences support is not implemented")
  80. }
  81. audience = audiences[0]
  82. }
  83. return google.JWTAccessTokenSourceFromJSON(data, audience)
  84. }