urlcredsource.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. "context"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "io"
  11. "io/ioutil"
  12. "net/http"
  13. "golang.org/x/oauth2"
  14. )
  15. type urlCredentialSource struct {
  16. URL string
  17. Headers map[string]string
  18. Format format
  19. ctx context.Context
  20. }
  21. func (cs urlCredentialSource) subjectToken() (string, error) {
  22. client := oauth2.NewClient(cs.ctx, nil)
  23. req, err := http.NewRequest("GET", cs.URL, nil)
  24. if err != nil {
  25. return "", fmt.Errorf("oauth2/google: HTTP request for URL-sourced credential failed: %v", err)
  26. }
  27. req = req.WithContext(cs.ctx)
  28. for key, val := range cs.Headers {
  29. req.Header.Add(key, val)
  30. }
  31. resp, err := client.Do(req)
  32. if err != nil {
  33. return "", fmt.Errorf("oauth2/google: invalid response when retrieving subject token: %v", err)
  34. }
  35. defer resp.Body.Close()
  36. respBody, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
  37. if err != nil {
  38. return "", fmt.Errorf("oauth2/google: invalid body in subject token URL query: %v", err)
  39. }
  40. if c := resp.StatusCode; c < 200 || c > 299 {
  41. return "", fmt.Errorf("oauth2/google: status code %d: %s", c, respBody)
  42. }
  43. switch cs.Format.Type {
  44. case "json":
  45. jsonData := make(map[string]interface{})
  46. err = json.Unmarshal(respBody, &jsonData)
  47. if err != nil {
  48. return "", fmt.Errorf("oauth2/google: failed to unmarshal subject token file: %v", err)
  49. }
  50. val, ok := jsonData[cs.Format.SubjectTokenFieldName]
  51. if !ok {
  52. return "", errors.New("oauth2/google: provided subject_token_field_name not found in credentials")
  53. }
  54. token, ok := val.(string)
  55. if !ok {
  56. return "", errors.New("oauth2/google: improperly formatted subject token")
  57. }
  58. return token, nil
  59. case "text":
  60. return string(respBody), nil
  61. case "":
  62. return string(respBody), nil
  63. default:
  64. return "", errors.New("oauth2/google: invalid credential_source file format type")
  65. }
  66. }