2022-07-07 20:11:50 +00: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"
|
|
|
|
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type urlCredentialSource struct {
|
|
|
|
URL string
|
|
|
|
Headers map[string]string
|
2024-10-16 10:54:40 +00:00
|
|
|
Format Format
|
2022-07-07 20:11:50 +00:00
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
2024-02-24 00:34:55 +00:00
|
|
|
func (cs urlCredentialSource) credentialSourceType() string {
|
|
|
|
return "url"
|
|
|
|
}
|
|
|
|
|
2022-07-07 20:11:50 +00:00
|
|
|
func (cs urlCredentialSource) subjectToken() (string, error) {
|
|
|
|
client := oauth2.NewClient(cs.ctx, nil)
|
|
|
|
req, err := http.NewRequest("GET", cs.URL, nil)
|
|
|
|
if err != nil {
|
2024-10-16 10:54:40 +00:00
|
|
|
return "", fmt.Errorf("oauth2/google/externalaccount: HTTP request for URL-sourced credential failed: %v", err)
|
2022-07-07 20:11:50 +00:00
|
|
|
}
|
|
|
|
req = req.WithContext(cs.ctx)
|
|
|
|
|
|
|
|
for key, val := range cs.Headers {
|
|
|
|
req.Header.Add(key, val)
|
|
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2024-10-16 10:54:40 +00:00
|
|
|
return "", fmt.Errorf("oauth2/google/externalaccount: invalid response when retrieving subject token: %v", err)
|
2022-07-07 20:11:50 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
respBody, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
|
|
|
if err != nil {
|
2024-10-16 10:54:40 +00:00
|
|
|
return "", fmt.Errorf("oauth2/google/externalaccount: invalid body in subject token URL query: %v", err)
|
2022-07-07 20:11:50 +00:00
|
|
|
}
|
|
|
|
if c := resp.StatusCode; c < 200 || c > 299 {
|
2024-10-16 10:54:40 +00:00
|
|
|
return "", fmt.Errorf("oauth2/google/externalaccount: status code %d: %s", c, respBody)
|
2022-07-07 20:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch cs.Format.Type {
|
|
|
|
case "json":
|
|
|
|
jsonData := make(map[string]interface{})
|
|
|
|
err = json.Unmarshal(respBody, &jsonData)
|
|
|
|
if err != nil {
|
2024-10-16 10:54:40 +00:00
|
|
|
return "", fmt.Errorf("oauth2/google/externalaccount: failed to unmarshal subject token file: %v", err)
|
2022-07-07 20:11:50 +00:00
|
|
|
}
|
|
|
|
val, ok := jsonData[cs.Format.SubjectTokenFieldName]
|
|
|
|
if !ok {
|
2024-10-16 10:54:40 +00:00
|
|
|
return "", errors.New("oauth2/google/externalaccount: provided subject_token_field_name not found in credentials")
|
2022-07-07 20:11:50 +00:00
|
|
|
}
|
|
|
|
token, ok := val.(string)
|
|
|
|
if !ok {
|
2024-10-16 10:54:40 +00:00
|
|
|
return "", errors.New("oauth2/google/externalaccount: improperly formatted subject token")
|
2022-07-07 20:11:50 +00:00
|
|
|
}
|
|
|
|
return token, nil
|
|
|
|
case "text":
|
|
|
|
return string(respBody), nil
|
|
|
|
case "":
|
|
|
|
return string(respBody), nil
|
|
|
|
default:
|
2024-10-16 10:54:40 +00:00
|
|
|
return "", errors.New("oauth2/google/externalaccount: invalid credential_source file format type")
|
2022-07-07 20:11:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|