@@ -110,11 +110,13 @@ func newTokenSourceNewAuth(ctx context.Context, audience string, ds *internal.Di
110110 if ds .AuthCredentials != nil {
111111 return nil , fmt .Errorf ("idtoken: option.WithTokenProvider not supported" )
112112 }
113+ credsJSON , _ := ds .GetAuthCredentialsJSON ()
114+ credsFile , _ := ds .GetAuthCredentialsFile ()
113115 creds , err := newidtoken .NewCredentials (& newidtoken.Options {
114116 Audience : audience ,
115117 CustomClaims : ds .CustomClaims ,
116- CredentialsFile : ds . CredentialsFile ,
117- CredentialsJSON : ds . CredentialsJSON ,
118+ CredentialsFile : credsFile ,
119+ CredentialsJSON : credsJSON ,
118120 Client : oauth2 .NewClient (ctx , nil ),
119121 Logger : ds .Logger ,
120122 })
@@ -233,20 +235,159 @@ func (w withCustomClaims) Apply(o *internal.DialSettings) {
233235 o .CustomClaims = w
234236}
235237
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+
236283// WithCredentialsFile returns a ClientOption that authenticates
237284// API calls with the given service account or refresh token JSON
238285// credentials file.
286+ //
287+ // Important: If you accept a credential configuration (credential
288+ // JSON/File/Stream) from an external source for authentication to Google
289+ // Cloud Platform, you must validate it before providing it to any Google
290+ // API or library. Providing an unvalidated credential configuration to
291+ // Google APIs can compromise the security of your systems and data. For
292+ // more information, refer to [Validate credential configurations from
293+ // external sources](https://cloud.google.com/docs/authentication/external/externally-sourced-credentials).
294+ //
295+ // Deprecated: This function is being deprecated because of a potential security risk.
296+ //
297+ // This function does not validate the credential configuration. The security
298+ // risk occurs when a credential configuration is accepted from a source that
299+ // is not under your control and used without validation on your side.
300+ //
301+ // If you know that you will be loading credential configurations of a
302+ // specific type, it is recommended to use a credential-type-specific
303+ // option function.
304+ // This will ensure that an unexpected credential type with potential for
305+ // malicious intent is not loaded unintentionally. You might still have to do
306+ // validation for certain credential types. Please follow the recommendation
307+ // for that function. For example, if you want to load only service accounts,
308+ // you can use [WithAuthCredentialsFile] with [ServiceAccount]:
309+ // ```
310+ // option.WithAuthCredentialsFile(option.ServiceAccount, "/path/to/file.json")
311+ // ```
312+ //
313+ // If you are loading your credential configuration from an untrusted source and have
314+ // not mitigated the risks (e.g. by validating the configuration yourself), make
315+ // these changes as soon as possible to prevent security risks to your environment.
316+ //
317+ // Regardless of the function used, it is always your responsibility to validate
318+ // configurations received from external sources.
239319func WithCredentialsFile (filename string ) ClientOption {
240320 return option .WithCredentialsFile (filename )
241321}
242322
323+ // WithAuthCredentialsFile returns a ClientOption that authenticates API calls
324+ // with the given JSON credentials file and credential type.
325+ //
326+ // Important: If you accept a credential configuration (credential
327+ // JSON/File/Stream) from an external source for authentication to Google
328+ // Cloud Platform, you must validate it before providing it to any Google
329+ // API or library. Providing an unvalidated credential configuration to
330+ // Google APIs can compromise the security of your systems and data. For
331+ // more information, refer to [Validate credential configurations from
332+ // external sources](https://cloud.google.com/docs/authentication/external/externally-sourced-credentials).
333+ func WithAuthCredentialsFile (credType CredentialsType , filename string ) ClientOption {
334+ return option .WithAuthCredentialsFile (credType , filename )
335+ }
336+
243337// WithCredentialsJSON returns a ClientOption that authenticates
244338// API calls with the given service account or refresh token JSON
245339// credentials.
340+ //
341+ // Important: If you accept a credential configuration (credential
342+ // JSON/File/Stream) from an external source for authentication to Google
343+ // Cloud Platform, you must validate it before providing it to any Google
344+ // API or library. Providing an unvalidated credential configuration to
345+ // Google APIs can compromise the security of your systems and data. For
346+ // more information, refer to [Validate credential configurations from
347+ // external sources](https://cloud.google.com/docs/authentication/external/externally-sourced-credentials).
348+ //
349+ // Deprecated: This function is being deprecated because of a potential security risk.
350+ //
351+ // This function does not validate the credential configuration. The security
352+ // risk occurs when a credential configuration is accepted from a source that
353+ // is not under your control and used without validation on your side.
354+ //
355+ // If you know that you will be loading credential configurations of a
356+ // specific type, it is recommended to use a credential-type-specific
357+ // option function.
358+ // This will ensure that an unexpected credential type with potential for
359+ // malicious intent is not loaded unintentionally. You might still have to do
360+ // validation for certain credential types. Please follow the recommendation
361+ // for that function. For example, if you want to load only service accounts,
362+ // you can use [WithAuthCredentialsJSON] with [ServiceAccount]:
363+ // ```
364+ // option.WithAuthCredentialsJSON(option.ServiceAccount, json)
365+ // ```
366+ //
367+ // If you are loading your credential configuration from an untrusted source and have
368+ // not mitigated the risks (e.g. by validating the configuration yourself), make
369+ // these changes as soon as possible to prevent security risks to your environment.
370+ //
371+ // Regardless of the function used, it is always your responsibility to validate
372+ // configurations received from external sources.
246373func WithCredentialsJSON (p []byte ) ClientOption {
247374 return option .WithCredentialsJSON (p )
248375}
249376
377+ // WithAuthCredentialsJSON returns a ClientOption that authenticates API calls
378+ // with the given JSON credentials and credential type.
379+ //
380+ // Important: If you accept a credential configuration (credential
381+ // JSON/File/Stream) from an external source for authentication to Google
382+ // Cloud Platform, you must validate it before providing it to any Google
383+ // API or library. Providing an unvalidated credential configuration to
384+ // Google APIs can compromise the security of your systems and data. For
385+ // more information, refer to [Validate credential configurations from
386+ // external sources](https://cloud.google.com/docs/authentication/external/externally-sourced-credentials).
387+ func WithAuthCredentialsJSON (credType CredentialsType , json []byte ) ClientOption {
388+ return option .WithAuthCredentialsJSON (credType , json )
389+ }
390+
250391// WithHTTPClient returns a ClientOption that specifies the HTTP client to use
251392// as the basis of communications. This option may only be used with services
252393// that support HTTP as their communication transport. When used, the
0 commit comments