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.
 
 
 

103 line
3.1 KiB

  1. // Copyright 2017 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package internal
  15. import (
  16. "context"
  17. "encoding/json"
  18. "fmt"
  19. "io/ioutil"
  20. "golang.org/x/oauth2"
  21. "golang.org/x/oauth2/google"
  22. )
  23. // Creds returns credential information obtained from DialSettings, or if none, then
  24. // it returns default credential information.
  25. func Creds(ctx context.Context, ds *DialSettings) (*google.Credentials, error) {
  26. if ds.Credentials != nil {
  27. return ds.Credentials, nil
  28. }
  29. if ds.CredentialsJSON != nil {
  30. return credentialsFromJSON(ctx, ds.CredentialsJSON, ds.Endpoint, ds.Scopes, ds.Audiences)
  31. }
  32. if ds.CredentialsFile != "" {
  33. data, err := ioutil.ReadFile(ds.CredentialsFile)
  34. if err != nil {
  35. return nil, fmt.Errorf("cannot read credentials file: %v", err)
  36. }
  37. return credentialsFromJSON(ctx, data, ds.Endpoint, ds.Scopes, ds.Audiences)
  38. }
  39. if ds.TokenSource != nil {
  40. return &google.Credentials{TokenSource: ds.TokenSource}, nil
  41. }
  42. cred, err := google.FindDefaultCredentials(ctx, ds.Scopes...)
  43. if err != nil {
  44. return nil, err
  45. }
  46. if len(cred.JSON) > 0 {
  47. return credentialsFromJSON(ctx, cred.JSON, ds.Endpoint, ds.Scopes, ds.Audiences)
  48. }
  49. // For GAE and GCE, the JSON is empty so return the default credentials directly.
  50. return cred, nil
  51. }
  52. // JSON key file type.
  53. const (
  54. serviceAccountKey = "service_account"
  55. )
  56. // credentialsFromJSON returns a google.Credentials based on the input.
  57. //
  58. // - If the JSON is a service account and no scopes provided, returns self-signed JWT auth flow
  59. // - Otherwise, returns OAuth 2.0 flow.
  60. func credentialsFromJSON(ctx context.Context, data []byte, endpoint string, scopes []string, audiences []string) (*google.Credentials, error) {
  61. cred, err := google.CredentialsFromJSON(ctx, data, scopes...)
  62. if err != nil {
  63. return nil, err
  64. }
  65. if len(data) > 0 && len(scopes) == 0 {
  66. var f struct {
  67. Type string `json:"type"`
  68. // The rest JSON fields are omitted because they are not used.
  69. }
  70. if err := json.Unmarshal(cred.JSON, &f); err != nil {
  71. return nil, err
  72. }
  73. if f.Type == serviceAccountKey {
  74. ts, err := selfSignedJWTTokenSource(data, endpoint, audiences)
  75. if err != nil {
  76. return nil, err
  77. }
  78. cred.TokenSource = ts
  79. }
  80. }
  81. return cred, err
  82. }
  83. func selfSignedJWTTokenSource(data []byte, endpoint string, audiences []string) (oauth2.TokenSource, error) {
  84. // Use the API endpoint as the default audience
  85. audience := endpoint
  86. if len(audiences) > 0 {
  87. // TODO(shinfan): Update golang oauth to support multiple audiences.
  88. if len(audiences) > 1 {
  89. return nil, fmt.Errorf("multiple audiences support is not implemented")
  90. }
  91. audience = audiences[0]
  92. }
  93. return google.JWTAccessTokenSourceFromJSON(data, audience)
  94. }