Use new oauth2 golang library
This commit is contained in:
parent
106a1c62f5
commit
4d280f0931
|
@ -8,14 +8,12 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"code.google.com/p/google-api-go-client/compute/v1"
|
"code.google.com/p/google-api-go-client/compute/v1"
|
||||||
// oauth2 "github.com/rasa/oauth2-fork-b3f9a68"
|
|
||||||
"github.com/golang/oauth2"
|
|
||||||
|
|
||||||
// oauth2 "github.com/rasa/oauth2-fork-b3f9a68/google"
|
"golang.org/x/oauth2"
|
||||||
"github.com/golang/oauth2/google"
|
"golang.org/x/oauth2/google"
|
||||||
|
"golang.org/x/oauth2/jwt"
|
||||||
)
|
)
|
||||||
|
|
||||||
const clientScopes string = "https://www.googleapis.com/auth/compute"
|
|
||||||
|
|
||||||
// Config is the configuration structure used to instantiate the Google
|
// Config is the configuration structure used to instantiate the Google
|
||||||
// provider.
|
// provider.
|
||||||
|
@ -41,8 +39,7 @@ func (c *Config) loadAndValidate() error {
|
||||||
c.Region = os.Getenv("GOOGLE_REGION")
|
c.Region = os.Getenv("GOOGLE_REGION")
|
||||||
}
|
}
|
||||||
|
|
||||||
var f *oauth2.Options
|
var client *http.Client
|
||||||
var err error
|
|
||||||
|
|
||||||
if c.AccountFile != "" {
|
if c.AccountFile != "" {
|
||||||
if err := loadJSON(&account, c.AccountFile); err != nil {
|
if err := loadJSON(&account, c.AccountFile); err != nil {
|
||||||
|
@ -52,29 +49,42 @@ func (c *Config) loadAndValidate() error {
|
||||||
err)
|
err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clientScopes := []string{"https://www.googleapis.com/auth/compute"}
|
||||||
|
|
||||||
// Get the token for use in our requests
|
// Get the token for use in our requests
|
||||||
log.Printf("[INFO] Requesting Google token...")
|
log.Printf("[INFO] Requesting Google token...")
|
||||||
log.Printf("[INFO] -- Email: %s", account.ClientEmail)
|
log.Printf("[INFO] -- Email: %s", account.ClientEmail)
|
||||||
log.Printf("[INFO] -- Scopes: %s", clientScopes)
|
log.Printf("[INFO] -- Scopes: %s", clientScopes)
|
||||||
log.Printf("[INFO] -- Private Key Length: %d", len(account.PrivateKey))
|
log.Printf("[INFO] -- Private Key Length: %d", len(account.PrivateKey))
|
||||||
|
|
||||||
f, err = oauth2.New(
|
conf := jwt.Config{
|
||||||
oauth2.JWTClient(account.ClientEmail, []byte(account.PrivateKey)),
|
Email: account.ClientEmail,
|
||||||
oauth2.Scope(clientScopes),
|
PrivateKey: []byte(account.PrivateKey),
|
||||||
google.JWTEndpoint())
|
Scopes: clientScopes,
|
||||||
|
TokenURL: "https://accounts.google.com/o/oauth2/token",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initiate an http.Client. The following GET request will be
|
||||||
|
// authorized and authenticated on the behalf of
|
||||||
|
// your service account.
|
||||||
|
client = conf.Client(oauth2.NoContext)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
log.Printf("[INFO] Requesting Google token via GCE Service Role...")
|
log.Printf("[INFO] Requesting Google token via GCE Service Role...")
|
||||||
f, err = oauth2.New(google.ComputeEngineAccount(""))
|
client = &http.Client{
|
||||||
|
Transport: &oauth2.Transport{
|
||||||
|
// Fetch from Google Compute Engine's metadata server to retrieve
|
||||||
|
// an access token for the provided account.
|
||||||
|
// If no account is specified, "default" is used.
|
||||||
|
Source: google.ComputeTokenSource(""),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Error retrieving auth token: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("[INFO] Instantiating GCE client...")
|
log.Printf("[INFO] Instantiating GCE client...")
|
||||||
c.clientCompute, err = compute.New(&http.Client{Transport: f.NewTransport()})
|
var err error
|
||||||
|
c.clientCompute, err = compute.New(client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue