2021-01-13 15:38:24 -05:00
|
|
|
// Copyright 2020 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package externalaccount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2021-08-12 18:59:38 -04:00
|
|
|
|
|
|
|
"golang.org/x/oauth2"
|
2021-01-13 15:38:24 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type urlCredentialSource struct {
|
|
|
|
URL string
|
|
|
|
Headers map[string]string
|
|
|
|
Format format
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs urlCredentialSource) subjectToken() (string, error) {
|
|
|
|
client := oauth2.NewClient(cs.ctx, nil)
|
|
|
|
req, err := http.NewRequest("GET", cs.URL, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("oauth2/google: HTTP request for URL-sourced credential failed: %v", err)
|
|
|
|
}
|
|
|
|
req = req.WithContext(cs.ctx)
|
|
|
|
|
|
|
|
for key, val := range cs.Headers {
|
|
|
|
req.Header.Add(key, val)
|
|
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("oauth2/google: invalid response when retrieving subject token: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2021-02-12 15:04:54 -05:00
|
|
|
respBody, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
2021-01-13 15:38:24 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("oauth2/google: invalid body in subject token URL query: %v", err)
|
|
|
|
}
|
2021-02-12 15:04:54 -05:00
|
|
|
if c := resp.StatusCode; c < 200 || c > 299 {
|
|
|
|
return "", fmt.Errorf("oauth2/google: status code %d: %s", c, respBody)
|
|
|
|
}
|
2021-01-13 15:38:24 -05:00
|
|
|
|
|
|
|
switch cs.Format.Type {
|
|
|
|
case "json":
|
|
|
|
jsonData := make(map[string]interface{})
|
2021-02-12 15:04:54 -05:00
|
|
|
err = json.Unmarshal(respBody, &jsonData)
|
2021-01-13 15:38:24 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("oauth2/google: failed to unmarshal subject token file: %v", err)
|
|
|
|
}
|
|
|
|
val, ok := jsonData[cs.Format.SubjectTokenFieldName]
|
|
|
|
if !ok {
|
|
|
|
return "", errors.New("oauth2/google: provided subject_token_field_name not found in credentials")
|
|
|
|
}
|
|
|
|
token, ok := val.(string)
|
|
|
|
if !ok {
|
|
|
|
return "", errors.New("oauth2/google: improperly formatted subject token")
|
|
|
|
}
|
|
|
|
return token, nil
|
|
|
|
case "text":
|
2021-02-12 15:04:54 -05:00
|
|
|
return string(respBody), nil
|
2021-01-13 15:38:24 -05:00
|
|
|
case "":
|
2021-02-12 15:04:54 -05:00
|
|
|
return string(respBody), nil
|
2021-01-13 15:38:24 -05:00
|
|
|
default:
|
|
|
|
return "", errors.New("oauth2/google: invalid credential_source file format type")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|