settings.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 supports the options and transport packages.
  15. package internal
  16. import (
  17. "errors"
  18. "net/http"
  19. "golang.org/x/oauth2"
  20. "golang.org/x/oauth2/google"
  21. "google.golang.org/grpc"
  22. )
  23. // DialSettings holds information needed to establish a connection with a
  24. // Google API service.
  25. type DialSettings struct {
  26. Endpoint string
  27. Scopes []string
  28. TokenSource oauth2.TokenSource
  29. Credentials *google.DefaultCredentials
  30. CredentialsFile string // if set, Token Source is ignored.
  31. CredentialsJSON []byte
  32. UserAgent string
  33. APIKey string
  34. HTTPClient *http.Client
  35. GRPCDialOpts []grpc.DialOption
  36. GRPCConn *grpc.ClientConn
  37. NoAuth bool
  38. }
  39. // Validate reports an error if ds is invalid.
  40. func (ds *DialSettings) Validate() error {
  41. hasCreds := ds.APIKey != "" || ds.TokenSource != nil || ds.CredentialsFile != "" || ds.Credentials != nil
  42. if ds.NoAuth && hasCreds {
  43. return errors.New("options.WithoutAuthentication is incompatible with any option that provides credentials")
  44. }
  45. // Credentials should not appear with other options.
  46. // We currently allow TokenSource and CredentialsFile to coexist.
  47. // TODO(jba): make TokenSource & CredentialsFile an error (breaking change).
  48. nCreds := 0
  49. if ds.Credentials != nil {
  50. nCreds++
  51. }
  52. if ds.CredentialsJSON != nil {
  53. nCreds++
  54. }
  55. if ds.CredentialsFile != "" {
  56. nCreds++
  57. }
  58. if ds.APIKey != "" {
  59. nCreds++
  60. }
  61. if ds.TokenSource != nil {
  62. nCreds++
  63. }
  64. // Accept only one form of credentials, except we allow TokenSource and CredentialsFile for backwards compatibility.
  65. if nCreds > 1 && !(nCreds == 2 && ds.TokenSource != nil && ds.CredentialsFile != "") {
  66. return errors.New("multiple credential options provided")
  67. }
  68. if ds.HTTPClient != nil && ds.GRPCConn != nil {
  69. return errors.New("WithHTTPClient is incompatible with WithGRPCConn")
  70. }
  71. if ds.HTTPClient != nil && ds.GRPCDialOpts != nil {
  72. return errors.New("WithHTTPClient is incompatible with gRPC dial options")
  73. }
  74. return nil
  75. }