Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / golang.org / x / oauth2 / google / default.go
1 // Copyright 2015 The Go 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         "context"
9         "encoding/json"
10         "fmt"
11         "io/ioutil"
12         "net/http"
13         "os"
14         "path/filepath"
15         "runtime"
16
17         "cloud.google.com/go/compute/metadata"
18         "golang.org/x/oauth2"
19 )
20
21 // Credentials holds Google credentials, including "Application Default Credentials".
22 // For more details, see:
23 // https://developers.google.com/accounts/docs/application-default-credentials
24 type Credentials struct {
25         ProjectID   string // may be empty
26         TokenSource oauth2.TokenSource
27
28         // JSON contains the raw bytes from a JSON credentials file.
29         // This field may be nil if authentication is provided by the
30         // environment and not with a credentials file, e.g. when code is
31         // running on Google Cloud Platform.
32         JSON []byte
33 }
34
35 // DefaultCredentials is the old name of Credentials.
36 //
37 // Deprecated: use Credentials instead.
38 type DefaultCredentials = Credentials
39
40 // DefaultClient returns an HTTP Client that uses the
41 // DefaultTokenSource to obtain authentication credentials.
42 func DefaultClient(ctx context.Context, scope ...string) (*http.Client, error) {
43         ts, err := DefaultTokenSource(ctx, scope...)
44         if err != nil {
45                 return nil, err
46         }
47         return oauth2.NewClient(ctx, ts), nil
48 }
49
50 // DefaultTokenSource returns the token source for
51 // "Application Default Credentials".
52 // It is a shortcut for FindDefaultCredentials(ctx, scope).TokenSource.
53 func DefaultTokenSource(ctx context.Context, scope ...string) (oauth2.TokenSource, error) {
54         creds, err := FindDefaultCredentials(ctx, scope...)
55         if err != nil {
56                 return nil, err
57         }
58         return creds.TokenSource, nil
59 }
60
61 // FindDefaultCredentials searches for "Application Default Credentials".
62 //
63 // It looks for credentials in the following places,
64 // preferring the first location found:
65 //
66 //   1. A JSON file whose path is specified by the
67 //      GOOGLE_APPLICATION_CREDENTIALS environment variable.
68 //   2. A JSON file in a location known to the gcloud command-line tool.
69 //      On Windows, this is %APPDATA%/gcloud/application_default_credentials.json.
70 //      On other systems, $HOME/.config/gcloud/application_default_credentials.json.
71 //   3. On Google App Engine standard first generation runtimes (<= Go 1.9) it uses
72 //      the appengine.AccessToken function.
73 //   4. On Google Compute Engine, Google App Engine standard second generation runtimes
74 //      (>= Go 1.11), and Google App Engine flexible environment, it fetches
75 //      credentials from the metadata server.
76 func FindDefaultCredentials(ctx context.Context, scopes ...string) (*Credentials, error) {
77         // First, try the environment variable.
78         const envVar = "GOOGLE_APPLICATION_CREDENTIALS"
79         if filename := os.Getenv(envVar); filename != "" {
80                 creds, err := readCredentialsFile(ctx, filename, scopes)
81                 if err != nil {
82                         return nil, fmt.Errorf("google: error getting credentials using %v environment variable: %v", envVar, err)
83                 }
84                 return creds, nil
85         }
86
87         // Second, try a well-known file.
88         filename := wellKnownFile()
89         if creds, err := readCredentialsFile(ctx, filename, scopes); err == nil {
90                 return creds, nil
91         } else if !os.IsNotExist(err) {
92                 return nil, fmt.Errorf("google: error getting credentials using well-known file (%v): %v", filename, err)
93         }
94
95         // Third, if we're on a Google App Engine standard first generation runtime (<= Go 1.9)
96         // use those credentials. App Engine standard second generation runtimes (>= Go 1.11)
97         // and App Engine flexible use ComputeTokenSource and the metadata server.
98         if appengineTokenFunc != nil {
99                 return &DefaultCredentials{
100                         ProjectID:   appengineAppIDFunc(ctx),
101                         TokenSource: AppEngineTokenSource(ctx, scopes...),
102                 }, nil
103         }
104
105         // Fourth, if we're on Google Compute Engine, an App Engine standard second generation runtime,
106         // or App Engine flexible, use the metadata server.
107         if metadata.OnGCE() {
108                 id, _ := metadata.ProjectID()
109                 return &DefaultCredentials{
110                         ProjectID:   id,
111                         TokenSource: ComputeTokenSource("", scopes...),
112                 }, nil
113         }
114
115         // None are found; return helpful error.
116         const url = "https://developers.google.com/accounts/docs/application-default-credentials"
117         return nil, fmt.Errorf("google: could not find default credentials. See %v for more information.", url)
118 }
119
120 // CredentialsFromJSON obtains Google credentials from a JSON value. The JSON can
121 // represent either a Google Developers Console client_credentials.json file (as in
122 // ConfigFromJSON) or a Google Developers service account key file (as in
123 // JWTConfigFromJSON).
124 func CredentialsFromJSON(ctx context.Context, jsonData []byte, scopes ...string) (*Credentials, error) {
125         var f credentialsFile
126         if err := json.Unmarshal(jsonData, &f); err != nil {
127                 return nil, err
128         }
129         ts, err := f.tokenSource(ctx, append([]string(nil), scopes...))
130         if err != nil {
131                 return nil, err
132         }
133         return &DefaultCredentials{
134                 ProjectID:   f.ProjectID,
135                 TokenSource: ts,
136                 JSON:        jsonData,
137         }, nil
138 }
139
140 func wellKnownFile() string {
141         const f = "application_default_credentials.json"
142         if runtime.GOOS == "windows" {
143                 return filepath.Join(os.Getenv("APPDATA"), "gcloud", f)
144         }
145         return filepath.Join(guessUnixHomeDir(), ".config", "gcloud", f)
146 }
147
148 func readCredentialsFile(ctx context.Context, filename string, scopes []string) (*DefaultCredentials, error) {
149         b, err := ioutil.ReadFile(filename)
150         if err != nil {
151                 return nil, err
152         }
153         return CredentialsFromJSON(ctx, b, scopes...)
154 }