2020-12-02 18:43:55 -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"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-02-12 15:04:54 -05:00
|
|
|
"io/ioutil"
|
2020-12-02 18:43:55 -05:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-01-13 10:23:33 -05:00
|
|
|
|
|
|
|
"golang.org/x/oauth2"
|
2020-12-02 18:43:55 -05:00
|
|
|
)
|
|
|
|
|
2021-02-18 15:18:54 -05:00
|
|
|
// exchangeToken performs an oauth2 token exchange with the provided endpoint.
|
2020-12-02 18:43:55 -05:00
|
|
|
// The first 4 fields are all mandatory. headers can be used to pass additional
|
|
|
|
// headers beyond the bare minimum required by the token exchange. options can
|
|
|
|
// be used to pass additional JSON-structured options to the remote server.
|
2021-02-18 15:18:54 -05:00
|
|
|
func exchangeToken(ctx context.Context, endpoint string, request *stsTokenExchangeRequest, authentication clientAuthentication, headers http.Header, options map[string]interface{}) (*stsTokenExchangeResponse, error) {
|
2020-12-02 18:43:55 -05:00
|
|
|
|
|
|
|
client := oauth2.NewClient(ctx, nil)
|
|
|
|
|
|
|
|
data := url.Values{}
|
|
|
|
data.Set("audience", request.Audience)
|
|
|
|
data.Set("grant_type", "urn:ietf:params:oauth:grant-type:token-exchange")
|
|
|
|
data.Set("requested_token_type", "urn:ietf:params:oauth:token-type:access_token")
|
|
|
|
data.Set("subject_token_type", request.SubjectTokenType)
|
|
|
|
data.Set("subject_token", request.SubjectToken)
|
|
|
|
data.Set("scope", strings.Join(request.Scope, " "))
|
2021-02-09 12:43:57 -05:00
|
|
|
if options != nil {
|
|
|
|
opts, err := json.Marshal(options)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("oauth2/google: failed to marshal additional options: %v", err)
|
|
|
|
}
|
|
|
|
data.Set("options", string(opts))
|
2020-12-02 18:43:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
authentication.InjectAuthentication(data, headers)
|
|
|
|
encodedData := data.Encode()
|
|
|
|
|
2021-01-13 10:23:33 -05:00
|
|
|
req, err := http.NewRequest("POST", endpoint, strings.NewReader(encodedData))
|
2020-12-02 18:43:55 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("oauth2/google: failed to properly build http request: %v", err)
|
|
|
|
|
|
|
|
}
|
2021-01-13 10:23:33 -05:00
|
|
|
req = req.WithContext(ctx)
|
2020-12-02 18:43:55 -05:00
|
|
|
for key, list := range headers {
|
|
|
|
for _, val := range list {
|
|
|
|
req.Header.Add(key, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
req.Header.Add("Content-Length", strconv.Itoa(len(encodedData)))
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("oauth2/google: invalid response from Secure Token Server: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2021-02-12 15:04:54 -05:00
|
|
|
body, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
2021-08-12 18:57:50 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-12 15:04:54 -05:00
|
|
|
if c := resp.StatusCode; c < 200 || c > 299 {
|
|
|
|
return nil, fmt.Errorf("oauth2/google: status code %d: %s", c, body)
|
|
|
|
}
|
2021-02-18 15:18:54 -05:00
|
|
|
var stsResp stsTokenExchangeResponse
|
2021-02-12 15:04:54 -05:00
|
|
|
err = json.Unmarshal(body, &stsResp)
|
2020-12-02 18:43:55 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("oauth2/google: failed to unmarshal response body from Secure Token Server: %v", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return &stsResp, nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 15:18:54 -05:00
|
|
|
// stsTokenExchangeRequest contains fields necessary to make an oauth2 token exchange.
|
|
|
|
type stsTokenExchangeRequest struct {
|
2020-12-02 18:43:55 -05:00
|
|
|
ActingParty struct {
|
|
|
|
ActorToken string
|
|
|
|
ActorTokenType string
|
|
|
|
}
|
|
|
|
GrantType string
|
|
|
|
Resource string
|
|
|
|
Audience string
|
|
|
|
Scope []string
|
|
|
|
RequestedTokenType string
|
|
|
|
SubjectToken string
|
|
|
|
SubjectTokenType string
|
|
|
|
}
|
|
|
|
|
2021-02-18 15:18:54 -05:00
|
|
|
// stsTokenExchangeResponse is used to decode the remote server response during an oauth2 token exchange.
|
|
|
|
type stsTokenExchangeResponse struct {
|
2020-12-02 18:43:55 -05:00
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
IssuedTokenType string `json:"issued_token_type"`
|
|
|
|
TokenType string `json:"token_type"`
|
|
|
|
ExpiresIn int `json:"expires_in"`
|
|
|
|
Scope string `json:"scope"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
}
|