mirror of
https://github.com/golang/oauth2.git
synced 2025-07-21 00:00:09 +08:00
Adding token fetching implementing for Compute Engine.
This commit is contained in:
parent
c1d53e71ad
commit
223212dc91
@ -63,6 +63,11 @@
|
|||||||
package google
|
package google
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"path"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/rakyll/oauth2"
|
"github.com/rakyll/oauth2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -72,9 +77,17 @@ const (
|
|||||||
uriGoogleToken = "https://accounts.google.com/o/oauth2/token"
|
uriGoogleToken = "https://accounts.google.com/o/oauth2/token"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type metaTokenRespBody struct {
|
||||||
|
AccessToken string `json:"access_token"`
|
||||||
|
ExpiresIn time.Duration `json:"expires_in"`
|
||||||
|
TokenType string `json:"token_type"`
|
||||||
|
}
|
||||||
|
|
||||||
// ComputeEngineConfig represents a OAuth 2.0 consumer client
|
// ComputeEngineConfig represents a OAuth 2.0 consumer client
|
||||||
// running on Google Compute Engine.
|
// running on Google Compute Engine.
|
||||||
type ComputeEngineConfig struct{}
|
type ComputeEngineConfig struct {
|
||||||
|
account string
|
||||||
|
}
|
||||||
|
|
||||||
// NewConfig creates a new OAuth2 config that uses Google
|
// NewConfig creates a new OAuth2 config that uses Google
|
||||||
// endpoints.
|
// endpoints.
|
||||||
@ -89,10 +102,10 @@ func NewServiceAccountConfig(opts *oauth2.JWTOptions) (*oauth2.JWTConfig, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewComputeEngineConfig creates a new config that can fetch tokens
|
// NewComputeEngineConfig creates a new config that can fetch tokens
|
||||||
// from Google Compute Engine instance's metaserver.
|
// from Google Compute Engine instance's metaserver. If no account is
|
||||||
func NewComputeEngineConfig() (*ComputeEngineConfig, error) {
|
// provided, default is used.
|
||||||
// Should fetch an access token from the meta server.
|
func NewComputeEngineConfig(account string) (*ComputeEngineConfig, error) {
|
||||||
return &ComputeEngineConfig{}, nil
|
return &ComputeEngineConfig{account: account}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTransport creates an authorized transport.
|
// NewTransport creates an authorized transport.
|
||||||
@ -101,6 +114,31 @@ func (c *ComputeEngineConfig) NewTransport() (oauth2.Transport, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FetchToken retrieves a new access token via metadata server.
|
// FetchToken retrieves a new access token via metadata server.
|
||||||
func (c *ComputeEngineConfig) FetchToken(existing *oauth2.Token) (*oauth2.Token, error) {
|
func (c *ComputeEngineConfig) FetchToken(existing *oauth2.Token) (token *oauth2.Token, err error) {
|
||||||
panic("not yet implemented")
|
account := "default"
|
||||||
|
if c.account != "" {
|
||||||
|
account = c.account
|
||||||
|
}
|
||||||
|
u := "http://" + path.Join("metadata/computeMetadata/v1/instance/service-accounts", account, "token")
|
||||||
|
req, err := http.NewRequest("GET", u, nil)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req.Header.Add("X-Google-Metadata-Request", "True")
|
||||||
|
resp, err := (&http.Client{Transport: oauth2.DefaultTransport}).Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
var tokenResp metaTokenRespBody
|
||||||
|
err = json.NewDecoder(resp.Body).Decode(&tokenResp)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
token = &oauth2.Token{
|
||||||
|
AccessToken: tokenResp.AccessToken,
|
||||||
|
TokenType: tokenResp.TokenType,
|
||||||
|
Expiry: time.Now().Add(tokenResp.ExpiresIn * time.Second),
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user