2021-01-25 03:04:43 -05:00
|
|
|
// Copyright 2021 The Go Authors. All rights reserved.
|
2021-01-19 19:28:06 -05:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package externalaccount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-01-25 03:02:46 -05:00
|
|
|
"context"
|
2021-01-19 19:28:06 -05:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// generateAccesstokenReq is used for service account impersonation
|
|
|
|
type generateAccessTokenReq struct {
|
|
|
|
Delegates []string `json:"delegates,omitempty"`
|
|
|
|
Lifetime string `json:"lifetime,omitempty"`
|
|
|
|
Scope []string `json:"scope,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type impersonateTokenResponse struct {
|
|
|
|
AccessToken string `json:"accessToken"`
|
|
|
|
ExpireTime string `json:"expireTime"`
|
|
|
|
}
|
|
|
|
|
2021-01-25 03:02:46 -05:00
|
|
|
type impersonateTokenSource struct {
|
|
|
|
ctx context.Context
|
|
|
|
ts oauth2.TokenSource
|
|
|
|
|
|
|
|
url string
|
|
|
|
scopes []string
|
|
|
|
}
|
|
|
|
|
2021-01-25 15:41:29 -05:00
|
|
|
// Token performs the exchange to get a temporary service account
|
2021-01-25 03:02:46 -05:00
|
|
|
func (its impersonateTokenSource) Token() (*oauth2.Token, error) {
|
2021-01-19 19:28:06 -05:00
|
|
|
reqBody := generateAccessTokenReq{
|
|
|
|
Lifetime: "3600s",
|
2021-01-25 03:02:46 -05:00
|
|
|
Scope: its.scopes,
|
2021-01-19 19:28:06 -05:00
|
|
|
}
|
|
|
|
b, err := json.Marshal(reqBody)
|
|
|
|
if err != nil {
|
2021-01-25 03:02:46 -05:00
|
|
|
return nil, fmt.Errorf("oauth2/google: unable to marshal request: %v", err)
|
2021-01-19 19:28:06 -05:00
|
|
|
}
|
2021-01-25 17:12:57 -05:00
|
|
|
client := oauth2.NewClient(its.ctx, its.ts)
|
2021-01-25 03:02:46 -05:00
|
|
|
req, err := http.NewRequest("POST", its.url, bytes.NewReader(b))
|
2021-01-19 19:28:06 -05:00
|
|
|
if err != nil {
|
2021-01-25 03:02:46 -05:00
|
|
|
return nil, fmt.Errorf("oauth2/google: unable to create impersonation request: %v", err)
|
2021-01-19 19:28:06 -05:00
|
|
|
}
|
2021-01-25 03:02:46 -05:00
|
|
|
req = req.WithContext(its.ctx)
|
2021-01-19 19:28:06 -05:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2021-01-25 03:02:46 -05:00
|
|
|
return nil, fmt.Errorf("oauth2/google: unable to generate access token: %v", err)
|
2021-01-19 19:28:06 -05:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
|
|
|
if err != nil {
|
2021-01-25 03:02:46 -05:00
|
|
|
return nil, fmt.Errorf("oauth2/google: unable to read body: %v", err)
|
2021-01-19 19:28:06 -05:00
|
|
|
}
|
|
|
|
if c := resp.StatusCode; c < 200 || c > 299 {
|
2021-01-25 03:02:46 -05:00
|
|
|
return nil, fmt.Errorf("oauth2/google: status code %d: %s", c, body)
|
2021-01-19 19:28:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var accessTokenResp impersonateTokenResponse
|
|
|
|
if err := json.Unmarshal(body, &accessTokenResp); err != nil {
|
2021-01-25 03:02:46 -05:00
|
|
|
return nil, fmt.Errorf("oauth2/google: unable to parse response: %v", err)
|
2021-01-19 19:28:06 -05:00
|
|
|
}
|
|
|
|
expiry, err := time.Parse(time.RFC3339, accessTokenResp.ExpireTime)
|
|
|
|
if err != nil {
|
2021-01-25 03:02:46 -05:00
|
|
|
return nil, fmt.Errorf("oauth2/google: unable to parse expiry: %v", err)
|
2021-01-19 19:28:06 -05:00
|
|
|
}
|
|
|
|
return &oauth2.Token{
|
|
|
|
AccessToken: accessTokenResp.AccessToken,
|
|
|
|
Expiry: expiry,
|
|
|
|
TokenType: "Bearer",
|
|
|
|
}, nil
|
|
|
|
}
|