creds.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2017 Google Inc. All Rights Reserved.
  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. "encoding/json"
  17. "fmt"
  18. "io/ioutil"
  19. "time"
  20. "golang.org/x/net/context"
  21. "golang.org/x/oauth2"
  22. "golang.org/x/oauth2/google"
  23. )
  24. // Creds returns credential information obtained from DialSettings, or if none, then
  25. // it returns default credential information.
  26. func Creds(ctx context.Context, ds *DialSettings) (*google.DefaultCredentials, error) {
  27. if ds.CredentialsFile != "" {
  28. return credFileTokenSource(ctx, ds.CredentialsFile, ds.Scopes...)
  29. }
  30. if ds.TokenSource != nil {
  31. return &google.DefaultCredentials{TokenSource: ds.TokenSource}, nil
  32. }
  33. return google.FindDefaultCredentials(ctx, ds.Scopes...)
  34. }
  35. // credFileTokenSource reads a refresh token file or a service account and returns
  36. // a TokenSource constructed from the config.
  37. func credFileTokenSource(ctx context.Context, filename string, scope ...string) (*google.DefaultCredentials, error) {
  38. data, err := ioutil.ReadFile(filename)
  39. if err != nil {
  40. return nil, fmt.Errorf("cannot read credentials file: %v", err)
  41. }
  42. // See if it is a refresh token credentials file first.
  43. ts, ok, err := refreshTokenTokenSource(ctx, data, scope...)
  44. if err != nil {
  45. return nil, err
  46. }
  47. if ok {
  48. return &google.DefaultCredentials{
  49. TokenSource: ts,
  50. JSON: data,
  51. }, nil
  52. }
  53. // If not, it should be a service account.
  54. cfg, err := google.JWTConfigFromJSON(data, scope...)
  55. if err != nil {
  56. return nil, fmt.Errorf("google.JWTConfigFromJSON: %v", err)
  57. }
  58. // jwt.Config does not expose the project ID, so re-unmarshal to get it.
  59. var pid struct {
  60. ProjectID string `json:"project_id"`
  61. }
  62. if err := json.Unmarshal(data, &pid); err != nil {
  63. return nil, err
  64. }
  65. return &google.DefaultCredentials{
  66. ProjectID: pid.ProjectID,
  67. TokenSource: cfg.TokenSource(ctx),
  68. JSON: data,
  69. }, nil
  70. }
  71. func refreshTokenTokenSource(ctx context.Context, data []byte, scope ...string) (oauth2.TokenSource, bool, error) {
  72. var c cred
  73. if err := json.Unmarshal(data, &c); err != nil {
  74. return nil, false, fmt.Errorf("cannot unmarshal credentials file: %v", err)
  75. }
  76. if c.ClientID == "" || c.ClientSecret == "" || c.RefreshToken == "" || c.Type != "authorized_user" {
  77. return nil, false, nil
  78. }
  79. cfg := &oauth2.Config{
  80. ClientID: c.ClientID,
  81. ClientSecret: c.ClientSecret,
  82. Endpoint: google.Endpoint,
  83. RedirectURL: "urn:ietf:wg:oauth:2.0:oob",
  84. Scopes: scope,
  85. }
  86. return cfg.TokenSource(ctx, &oauth2.Token{
  87. RefreshToken: c.RefreshToken,
  88. Expiry: time.Now(),
  89. }), true, nil
  90. }
  91. type cred struct {
  92. ClientID string `json:"client_id"`
  93. ClientSecret string `json:"client_secret"`
  94. RefreshToken string `json:"refresh_token"`
  95. Type string `json:"type"`
  96. }