@@ -30,13 +30,40 @@ import (
3030// ClientOption is for configuring a Google API client or transport.
3131type ClientOption = option.ClientOption
3232
33- type credentialsType int
33+ // CredentialsType specifies the type of JSON credentials being provided
34+ // to a loading function such as [WithAuthCredentialsFile] or
35+ // [WithAuthCredentialsJSON].
36+ type CredentialsType = option.CredentialsType
3437
3538const (
36- unknownCredType credentialsType = iota
37- serviceAccount
38- impersonatedServiceAccount
39- externalAccount
39+ // unknownCredType is a private CredentialsType representing an unknown JSON file type.
40+ unknownCredType = internal .Unknown
41+ // ServiceAccount represents a service account file type.
42+ ServiceAccount = option .ServiceAccount
43+ // User represents a user credentials file type.
44+ User = option .User
45+ // ImpersonatedServiceAccount represents an impersonated service account file type.
46+ //
47+ // IMPORTANT:
48+ // This credential type does not validate the credential configuration. A security
49+ // risk occurs when a credential configuration configured with malicious urls
50+ // is used.
51+ // You should validate credential configurations provided by untrusted sources.
52+ // See [Security requirements when using credential configurations from an external
53+ // source] https://cloud.google.com/docs/authentication/external/externally-sourced-credentials
54+ // for more details.
55+ ImpersonatedServiceAccount = option .ImpersonatedServiceAccount
56+ // ExternalAccount represents an external account file type.
57+ //
58+ // IMPORTANT:
59+ // This credential type does not validate the credential configuration. A security
60+ // risk occurs when a credential configuration configured with malicious urls
61+ // is used.
62+ // You should validate credential configurations provided by untrusted sources.
63+ // See [Security requirements when using credential configurations from an external
64+ // source] https://cloud.google.com/docs/authentication/external/externally-sourced-credentials
65+ // for more details.
66+ ExternalAccount = option .ExternalAccount
4067)
4168
4269// NewClient creates a HTTP Client that automatically adds an ID token to each
@@ -148,7 +175,7 @@ func tokenSourceFromBytes(ctx context.Context, data []byte, audience string, ds
148175 return nil , err
149176 }
150177 switch allowedType {
151- case serviceAccount :
178+ case ServiceAccount :
152179 cfg , err := google .JWTConfigFromJSON (data , ds .GetScopes ()... )
153180 if err != nil {
154181 return nil , err
@@ -168,7 +195,7 @@ func tokenSourceFromBytes(ctx context.Context, data []byte, audience string, ds
168195 return nil , err
169196 }
170197 return oauth2 .ReuseTokenSource (tok , ts ), nil
171- case impersonatedServiceAccount , externalAccount :
198+ case ImpersonatedServiceAccount , ExternalAccount :
172199 type url struct {
173200 ServiceAccountImpersonationURL string `json:"service_account_impersonation_url"`
174201 }
@@ -184,20 +211,20 @@ func tokenSourceFromBytes(ctx context.Context, data []byte, audience string, ds
184211 TargetPrincipal : account ,
185212 IncludeEmail : true ,
186213 }
187- ts , err := impersonate .IDTokenSource (ctx , config , option .WithCredentialsJSON ( data ))
214+ ts , err := impersonate .IDTokenSource (ctx , config , option .WithAuthCredentialsJSON ( allowedType , data ))
188215 if err != nil {
189216 return nil , err
190217 }
191218 return ts , nil
192219 default :
193- return nil , fmt .Errorf ("idtoken: unsupported credentials type" )
220+ return nil , fmt .Errorf ("idtoken: unsupported credentials type: %d" , allowedType )
194221 }
195222}
196223
197224// getAllowedType returns the credentials type of type credentialsType, and an error.
198225// allowed types are "service_account" and "impersonated_service_account"
199- func getAllowedType (data []byte ) (credentialsType , error ) {
200- var t credentialsType
226+ func getAllowedType (data []byte ) (CredentialsType , error ) {
227+ var t CredentialsType
201228 if len (data ) == 0 {
202229 return t , fmt .Errorf ("idtoken: credential provided is 0 bytes" )
203230 }
@@ -211,14 +238,14 @@ func getAllowedType(data []byte) (credentialsType, error) {
211238 return t , nil
212239}
213240
214- func parseCredType (typeString string ) credentialsType {
241+ func parseCredType (typeString string ) CredentialsType {
215242 switch typeString {
216243 case "service_account" :
217- return serviceAccount
244+ return ServiceAccount
218245 case "impersonated_service_account" :
219- return impersonatedServiceAccount
246+ return ImpersonatedServiceAccount
220247 case "external_account" :
221- return externalAccount
248+ return ExternalAccount
222249 default :
223250 return unknownCredType
224251 }
@@ -235,51 +262,6 @@ func (w withCustomClaims) Apply(o *internal.DialSettings) {
235262 o .CustomClaims = w
236263}
237264
238- // CredentialsType specifies the type of JSON credentials being provided
239- // to a loading function such as [WithAuthCredentialsFile] or
240- // [WithAuthCredentialsJSON].
241- type CredentialsType = option.CredentialsType
242-
243- const (
244- // Unknown represents an unknown JSON file type.
245- //
246- // IMPORTANT:
247- // This credential type does not validate the credential configuration. A security
248- // risk occurs when a credential configuration configured with malicious urls
249- // is used.
250- // You should validate credential configurations provided by untrusted sources.
251- // See [Security requirements when using credential configurations from an external
252- // source] https://cloud.google.com/docs/authentication/external/externally-sourced-credentials
253- // for more details.
254- Unknown = option .Unknown
255- // ServiceAccount represents a service account file type.
256- ServiceAccount = option .ServiceAccount
257- // User represents a user credentials file type.
258- User = option .User
259- // ImpersonatedServiceAccount represents an impersonated service account file type.
260- //
261- // IMPORTANT:
262- // This credential type does not validate the credential configuration. A security
263- // risk occurs when a credential configuration configured with malicious urls
264- // is used.
265- // You should validate credential configurations provided by untrusted sources.
266- // See [Security requirements when using credential configurations from an external
267- // source] https://cloud.google.com/docs/authentication/external/externally-sourced-credentials
268- // for more details.
269- ImpersonatedServiceAccount = option .ImpersonatedServiceAccount
270- // ExternalAccount represents an external account file type.
271- //
272- // IMPORTANT:
273- // This credential type does not validate the credential configuration. A security
274- // risk occurs when a credential configuration configured with malicious urls
275- // is used.
276- // You should validate credential configurations provided by untrusted sources.
277- // See [Security requirements when using credential configurations from an external
278- // source] https://cloud.google.com/docs/authentication/external/externally-sourced-credentials
279- // for more details.
280- ExternalAccount = option .ExternalAccount
281- )
282-
283265// WithCredentialsFile returns a ClientOption that authenticates
284266// API calls with the given service account or refresh token JSON
285267// credentials file.
0 commit comments