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.
 
 
 

63 line
2.2 KiB

  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 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. UserAgent string
  32. APIKey string
  33. HTTPClient *http.Client
  34. GRPCDialOpts []grpc.DialOption
  35. GRPCConn *grpc.ClientConn
  36. NoAuth bool
  37. }
  38. // Validate reports an error if ds is invalid.
  39. func (ds *DialSettings) Validate() error {
  40. hasCreds := ds.APIKey != "" || ds.TokenSource != nil || ds.CredentialsFile != "" || ds.Credentials != nil
  41. if ds.NoAuth && hasCreds {
  42. return errors.New("options.WithoutAuthentication is incompatible with any option that provides credentials")
  43. }
  44. // Credentials should not appear with other options.
  45. // We currently allow TokenSource and CredentialsFile to coexist.
  46. // TODO(jba): make TokenSource & CredentialsFile an error (breaking change).
  47. if ds.Credentials != nil && (ds.APIKey != "" || ds.TokenSource != nil || ds.CredentialsFile != "") {
  48. return errors.New("multiple credential options provided")
  49. }
  50. if ds.HTTPClient != nil && ds.GRPCConn != nil {
  51. return errors.New("WithHTTPClient is incompatible with WithGRPCConn")
  52. }
  53. if ds.HTTPClient != nil && ds.GRPCDialOpts != nil {
  54. return errors.New("WithHTTPClient is incompatible with gRPC dial options")
  55. }
  56. return nil
  57. }