12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- package detect
15
+ package credentials
16
16
17
17
import (
18
+ "context"
18
19
"encoding/json"
19
20
"errors"
20
21
"fmt"
@@ -38,72 +39,21 @@ const (
38
39
39
40
// Help on default credentials
40
41
adcSetupURL = "https://cloud.google.com/docs/authentication/external/set-up-adc"
41
-
42
- universeDomainDefault = "googleapis.com"
43
42
)
44
43
45
44
var (
46
45
// for testing
47
46
allowOnGCECheck = true
48
47
)
49
48
50
- // Credentials holds Google credentials, including
51
- // [Application Default Credentials](https://developers.google.com/accounts/docs/application-default-credentials).
52
- type Credentials struct {
53
- json []byte
54
- projectID string
55
- quotaProjectID string
56
- // universeDomain is the default service domain for a given Cloud universe.
57
- universeDomain string
58
-
59
- auth.TokenProvider
60
- }
61
-
62
- func newCredentials (tokenProvider auth.TokenProvider , json []byte , projectID string , quotaProjectID string , universeDomain string ) * Credentials {
63
- return & Credentials {
64
- json : json ,
65
- projectID : internal .GetProjectID (json , projectID ),
66
- quotaProjectID : internal .GetQuotaProject (json , quotaProjectID ),
67
- TokenProvider : tokenProvider ,
68
- universeDomain : universeDomain ,
69
- }
70
- }
71
-
72
- // JSON returns the bytes associated with the the file used to source
73
- // credentials if one was used.
74
- func (c * Credentials ) JSON () []byte {
75
- return c .json
76
- }
77
-
78
- // ProjectID returns the associated project ID from the underlying file or
79
- // environment.
80
- func (c * Credentials ) ProjectID () string {
81
- return c .projectID
82
- }
83
-
84
- // QuotaProjectID returns the associated quota project ID from the underlying
85
- // file or environment.
86
- func (c * Credentials ) QuotaProjectID () string {
87
- return c .quotaProjectID
88
- }
89
-
90
- // UniverseDomain returns the default service domain for a given Cloud universe.
91
- // The default value is "googleapis.com".
92
- func (c * Credentials ) UniverseDomain () string {
93
- if c .universeDomain == "" {
94
- return universeDomainDefault
95
- }
96
- return c .universeDomain
97
- }
98
-
99
49
// OnGCE reports whether this process is running in Google Cloud.
100
50
func OnGCE () bool {
101
51
// TODO(codyoss): once all libs use this auth lib move metadata check here
102
52
return allowOnGCECheck && metadata .OnGCE ()
103
53
}
104
54
105
- // DefaultCredentials searches for "Application Default Credentials" and returns
106
- // a credential based on the [Options ] provided.
55
+ // DetectDefault searches for "Application Default Credentials" and returns
56
+ // a credential based on the [DetectOptions ] provided.
107
57
//
108
58
// It looks for credentials in the following places, preferring the first
109
59
// location found:
@@ -119,7 +69,7 @@ func OnGCE() bool {
119
69
// - On Google Compute Engine, Google App Engine standard second generation
120
70
// runtimes, and Google App Engine flexible environment, it fetches
121
71
// credentials from the metadata server.
122
- func DefaultCredentials (opts * Options ) (* Credentials , error ) {
72
+ func DetectDefault (opts * DetectOptions ) (* auth. Credentials , error ) {
123
73
if err := opts .validate (); err != nil {
124
74
return nil , err
125
75
}
@@ -138,15 +88,19 @@ func DefaultCredentials(opts *Options) (*Credentials, error) {
138
88
}
139
89
140
90
if OnGCE () {
141
- id , _ := metadata .ProjectID ()
142
- return newCredentials (computeTokenProvider (opts .EarlyTokenRefresh , opts .Scopes ... ), nil , id , "" , "" ), nil
91
+ return auth .NewCredentials (& auth.CredentialsOptions {
92
+ TokenProvider : computeTokenProvider (opts .EarlyTokenRefresh , opts .Scopes ... ),
93
+ ProjectIDProvider : auth .CredentialsPropertyFunc (func (context.Context ) (string , error ) {
94
+ return metadata .ProjectID ()
95
+ }),
96
+ }), nil
143
97
}
144
98
145
99
return nil , fmt .Errorf ("detect: could not find default credentials. See %v for more information" , adcSetupURL )
146
100
}
147
101
148
- // Options provides configuration for [DefaultCredentials ].
149
- type Options struct {
102
+ // DetectOptions provides configuration for [DetectDefault ].
103
+ type DetectOptions struct {
150
104
// Scopes that credentials tokens should have. Example:
151
105
// https://www.googleapis.com/auth/cloud-platform. Required if Audience is
152
106
// not provided.
@@ -188,7 +142,7 @@ type Options struct {
188
142
Client * http.Client
189
143
}
190
144
191
- func (o * Options ) validate () error {
145
+ func (o * DetectOptions ) validate () error {
192
146
if o == nil {
193
147
return errors .New ("detect: options must be provided" )
194
148
}
@@ -201,35 +155,35 @@ func (o *Options) validate() error {
201
155
return nil
202
156
}
203
157
204
- func (o * Options ) tokenURL () string {
158
+ func (o * DetectOptions ) tokenURL () string {
205
159
if o .TokenURL != "" {
206
160
return o .TokenURL
207
161
}
208
162
return googleTokenURL
209
163
}
210
164
211
- func (o * Options ) scopes () []string {
165
+ func (o * DetectOptions ) scopes () []string {
212
166
scopes := make ([]string , len (o .Scopes ))
213
167
copy (scopes , o .Scopes )
214
168
return scopes
215
169
}
216
170
217
- func (o * Options ) client () * http.Client {
171
+ func (o * DetectOptions ) client () * http.Client {
218
172
if o .Client != nil {
219
173
return o .Client
220
174
}
221
175
return internal .CloneDefaultClient ()
222
176
}
223
177
224
- func readCredentialsFile (filename string , opts * Options ) (* Credentials , error ) {
178
+ func readCredentialsFile (filename string , opts * DetectOptions ) (* auth. Credentials , error ) {
225
179
b , err := os .ReadFile (filename )
226
180
if err != nil {
227
181
return nil , err
228
182
}
229
183
return readCredentialsFileJSON (b , opts )
230
184
}
231
185
232
- func readCredentialsFileJSON (b []byte , opts * Options ) (* Credentials , error ) {
186
+ func readCredentialsFileJSON (b []byte , opts * DetectOptions ) (* auth. Credentials , error ) {
233
187
// attempt to parse jsonData as a Google Developers Console client_credentials.json.
234
188
config := clientCredConfigFromJSON (b , opts )
235
189
if config != nil {
@@ -240,12 +194,15 @@ func readCredentialsFileJSON(b []byte, opts *Options) (*Credentials, error) {
240
194
if err != nil {
241
195
return nil , err
242
196
}
243
- return newCredentials (tp , b , "" , "" , "" ), nil
197
+ return auth .NewCredentials (& auth.CredentialsOptions {
198
+ TokenProvider : tp ,
199
+ JSON : b ,
200
+ }), nil
244
201
}
245
202
return fileCredentials (b , opts )
246
203
}
247
204
248
- func clientCredConfigFromJSON (b []byte , opts * Options ) * auth.Options3LO {
205
+ func clientCredConfigFromJSON (b []byte , opts * DetectOptions ) * auth.Options3LO {
249
206
var creds internaldetect.ClientCredentialsFile
250
207
var c * internaldetect.Config3LO
251
208
if err := json .Unmarshal (b , & creds ); err != nil {
0 commit comments