|
| 1 | +// Copyright 2015 The oauth2 Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package google |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "net/http" |
| 11 | + "os" |
| 12 | + "os/user" |
| 13 | + "path/filepath" |
| 14 | + "runtime" |
| 15 | + "strings" |
| 16 | + "time" |
| 17 | + |
| 18 | + "golang.org/x/oauth2" |
| 19 | + "golang.org/x/oauth2/internal" |
| 20 | +) |
| 21 | + |
| 22 | +type sdkCredentials struct { |
| 23 | + Data []struct { |
| 24 | + Credential struct { |
| 25 | + ClientID string `json:"client_id"` |
| 26 | + ClientSecret string `json:"client_secret"` |
| 27 | + AccessToken string `json:"access_token"` |
| 28 | + RefreshToken string `json:"refresh_token"` |
| 29 | + TokenExpiry time.Time `json:"token_expiry"` |
| 30 | + } `json:"credential"` |
| 31 | + Key struct { |
| 32 | + Account string `json:"account"` |
| 33 | + Scope string `json:"scope"` |
| 34 | + } `json:"key"` |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// An SDKConfig provides access to tokens from an account already |
| 39 | +// authorized via the Google Cloud SDK. |
| 40 | +type SDKConfig struct { |
| 41 | + conf oauth2.Config |
| 42 | + initialToken *oauth2.Token |
| 43 | +} |
| 44 | + |
| 45 | +// NewSDKConfig creates an SDKConfig for the given Google Cloud SDK |
| 46 | +// account. If account is empty, the account currently active in |
| 47 | +// Google Cloud SDK properties is used. |
| 48 | +// Google Cloud SDK credentials must be created by running `gcloud auth` |
| 49 | +// before using this function. |
| 50 | +// The Google Cloud SDK is available at https://cloud.google.com/sdk/. |
| 51 | +func NewSDKConfig(account string) (*SDKConfig, error) { |
| 52 | + configPath, err := sdkConfigPath() |
| 53 | + if err != nil { |
| 54 | + return nil, fmt.Errorf("oauth2/google: error getting SDK config path: %v", err) |
| 55 | + } |
| 56 | + credentialsPath := filepath.Join(configPath, "credentials") |
| 57 | + f, err := os.Open(credentialsPath) |
| 58 | + if err != nil { |
| 59 | + return nil, fmt.Errorf("oauth2/google: failed to load SDK credentials: %v", err) |
| 60 | + } |
| 61 | + defer f.Close() |
| 62 | + |
| 63 | + var c sdkCredentials |
| 64 | + if err := json.NewDecoder(f).Decode(&c); err != nil { |
| 65 | + return nil, fmt.Errorf("oauth2/google: failed to decode SDK credentials from %q: %v", credentialsPath, err) |
| 66 | + } |
| 67 | + if len(c.Data) == 0 { |
| 68 | + return nil, fmt.Errorf("oauth2/google: no credentials found in %q, run `gcloud auth login` to create one", credentialsPath) |
| 69 | + } |
| 70 | + if account == "" { |
| 71 | + propertiesPath := filepath.Join(configPath, "properties") |
| 72 | + f, err := os.Open(propertiesPath) |
| 73 | + if err != nil { |
| 74 | + return nil, fmt.Errorf("oauth2/google: failed to load SDK properties: %v", err) |
| 75 | + } |
| 76 | + defer f.Close() |
| 77 | + ini, err := internal.ParseINI(f) |
| 78 | + if err != nil { |
| 79 | + return nil, fmt.Errorf("oauth2/google: failed to parse SDK properties %q: %v", propertiesPath, err) |
| 80 | + } |
| 81 | + core, ok := ini["core"] |
| 82 | + if !ok { |
| 83 | + return nil, fmt.Errorf("oauth2/google: failed to find [core] section in %v", ini) |
| 84 | + } |
| 85 | + active, ok := core["account"] |
| 86 | + if !ok { |
| 87 | + return nil, fmt.Errorf("oauth2/google: failed to find %q attribute in %v", "account", core) |
| 88 | + } |
| 89 | + account = active |
| 90 | + } |
| 91 | + |
| 92 | + for _, d := range c.Data { |
| 93 | + if account == "" || d.Key.Account == account { |
| 94 | + return &SDKConfig{ |
| 95 | + conf: oauth2.Config{ |
| 96 | + ClientID: d.Credential.ClientID, |
| 97 | + ClientSecret: d.Credential.ClientSecret, |
| 98 | + Scopes: strings.Split(d.Key.Scope, " "), |
| 99 | + Endpoint: Endpoint, |
| 100 | + RedirectURL: "oob", |
| 101 | + }, |
| 102 | + initialToken: &oauth2.Token{ |
| 103 | + AccessToken: d.Credential.AccessToken, |
| 104 | + RefreshToken: d.Credential.RefreshToken, |
| 105 | + Expiry: d.Credential.TokenExpiry, |
| 106 | + }, |
| 107 | + }, nil |
| 108 | + } |
| 109 | + } |
| 110 | + return nil, fmt.Errorf("oauth2/google: no such credentials for account %q", account) |
| 111 | +} |
| 112 | + |
| 113 | +// Client returns an HTTP client using Google Cloud SDK credentials to |
| 114 | +// authorize requests. The token will auto-refresh as necessary. The |
| 115 | +// underlying http.RoundTripper will be obtained using the provided |
| 116 | +// context. The returned client and its Transport should not be |
| 117 | +// modified. |
| 118 | +func (c *SDKConfig) Client(ctx oauth2.Context) *http.Client { |
| 119 | + return &http.Client{ |
| 120 | + Transport: &oauth2.Transport{ |
| 121 | + Source: c.TokenSource(ctx), |
| 122 | + }, |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// TokenSource returns an oauth2.TokenSource that retrieve tokens from |
| 127 | +// Google Cloud SDK credentials using the provided context. |
| 128 | +// It will returns the current access token stored in the credentials, |
| 129 | +// and refresh it when it expires, but it won't update the credentials |
| 130 | +// with the new access token. |
| 131 | +func (c *SDKConfig) TokenSource(ctx oauth2.Context) oauth2.TokenSource { |
| 132 | + return c.conf.TokenSource(ctx, c.initialToken) |
| 133 | +} |
| 134 | + |
| 135 | +// Scopes are the OAuth 2.0 scopes the current account is authorized for. |
| 136 | +func (c *SDKConfig) Scopes() []string { |
| 137 | + return c.conf.Scopes |
| 138 | +} |
| 139 | + |
| 140 | +func sdkConfigPath() (string, error) { |
| 141 | + if runtime.GOOS == "windows" { |
| 142 | + return filepath.Join(os.Getenv("APPDATA"), "gcloud"), nil |
| 143 | + } |
| 144 | + unixHomeDir = guessUnixHomeDir() |
| 145 | + if unixHomeDir == "" { |
| 146 | + return "", fmt.Errorf("unable to get current user home directory: os/user lookup failed; $HOME is empty") |
| 147 | + } |
| 148 | + return filepath.Join(unixHomeDir, ".config", "gcloud"), nil |
| 149 | +} |
| 150 | + |
| 151 | +var unixHomeDir string |
| 152 | + |
| 153 | +func guessUnixHomeDir() string { |
| 154 | + if unixHomeDir != "" { |
| 155 | + return unixHomeDir |
| 156 | + } |
| 157 | + usr, err := user.Current() |
| 158 | + if err == nil { |
| 159 | + return usr.HomeDir |
| 160 | + } |
| 161 | + return os.Getenv("HOME") |
| 162 | +} |
0 commit comments