clientauth.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2020 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package externalaccount
  5. import (
  6. "encoding/base64"
  7. "golang.org/x/oauth2"
  8. "net/http"
  9. "net/url"
  10. )
  11. // clientAuthentication represents an OAuth client ID and secret and the mechanism for passing these credentials as stated in rfc6749#2.3.1.
  12. type clientAuthentication struct {
  13. // AuthStyle can be either basic or request-body
  14. AuthStyle oauth2.AuthStyle
  15. ClientID string
  16. ClientSecret string
  17. }
  18. func (c *clientAuthentication) InjectAuthentication(values url.Values, headers http.Header) {
  19. if c.ClientID == "" || c.ClientSecret == "" || values == nil || headers == nil {
  20. return
  21. }
  22. switch c.AuthStyle {
  23. case oauth2.AuthStyleInHeader: // AuthStyleInHeader corresponds to basic authentication as defined in rfc7617#2
  24. plainHeader := c.ClientID + ":" + c.ClientSecret
  25. headers.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(plainHeader)))
  26. case oauth2.AuthStyleInParams: // AuthStyleInParams corresponds to request-body authentication with ClientID and ClientSecret in the message body.
  27. values.Set("client_id", c.ClientID)
  28. values.Set("client_secret", c.ClientSecret)
  29. case oauth2.AuthStyleAutoDetect:
  30. values.Set("client_id", c.ClientID)
  31. values.Set("client_secret", c.ClientSecret)
  32. default:
  33. values.Set("client_id", c.ClientID)
  34. values.Set("client_secret", c.ClientSecret)
  35. }
  36. }