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.
 
 
 

97 lines
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 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.Credentials
  30. CredentialsFile string // if set, Token Source is ignored.
  31. CredentialsJSON []byte
  32. UserAgent string
  33. APIKey string
  34. Audiences []string
  35. HTTPClient *http.Client
  36. GRPCDialOpts []grpc.DialOption
  37. GRPCConn *grpc.ClientConn
  38. NoAuth bool
  39. // Google API system parameters. For more information please read:
  40. // https://cloud.google.com/apis/docs/system-parameters
  41. QuotaProject string
  42. RequestReason string
  43. }
  44. // Validate reports an error if ds is invalid.
  45. func (ds *DialSettings) Validate() error {
  46. hasCreds := ds.APIKey != "" || ds.TokenSource != nil || ds.CredentialsFile != "" || ds.Credentials != nil
  47. if ds.NoAuth && hasCreds {
  48. return errors.New("options.WithoutAuthentication is incompatible with any option that provides credentials")
  49. }
  50. // Credentials should not appear with other options.
  51. // We currently allow TokenSource and CredentialsFile to coexist.
  52. // TODO(jba): make TokenSource & CredentialsFile an error (breaking change).
  53. nCreds := 0
  54. if ds.Credentials != nil {
  55. nCreds++
  56. }
  57. if ds.CredentialsJSON != nil {
  58. nCreds++
  59. }
  60. if ds.CredentialsFile != "" {
  61. nCreds++
  62. }
  63. if ds.APIKey != "" {
  64. nCreds++
  65. }
  66. if ds.TokenSource != nil {
  67. nCreds++
  68. }
  69. if len(ds.Scopes) > 0 && len(ds.Audiences) > 0 {
  70. return errors.New("WithScopes is incompatible with WithAudience")
  71. }
  72. // Accept only one form of credentials, except we allow TokenSource and CredentialsFile for backwards compatibility.
  73. if nCreds > 1 && !(nCreds == 2 && ds.TokenSource != nil && ds.CredentialsFile != "") {
  74. return errors.New("multiple credential options provided")
  75. }
  76. if ds.HTTPClient != nil && ds.GRPCConn != nil {
  77. return errors.New("WithHTTPClient is incompatible with WithGRPCConn")
  78. }
  79. if ds.HTTPClient != nil && ds.GRPCDialOpts != nil {
  80. return errors.New("WithHTTPClient is incompatible with gRPC dial options")
  81. }
  82. if ds.HTTPClient != nil && ds.QuotaProject != "" {
  83. return errors.New("WithHTTPClient is incompatible with QuotaProject")
  84. }
  85. if ds.HTTPClient != nil && ds.RequestReason != "" {
  86. return errors.New("WithHTTPClient is incompatible with RequestReason")
  87. }
  88. return nil
  89. }