Copyright 2020 Google LLC. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

package idtoken

import (
	
	
	
	

	
	
	

	
	
	
	htransport 
)
ClientOption is aliased so relevant options are easily found in the docs.
ClientOption is for configuring a Google API client or transport.
NewClient creates a HTTP Client that automatically adds an ID token to each request via an Authorization header. The token will have have the audience provided and be configured with the supplied options. The parameter audience may not be empty.
func ( context.Context,  string,  ...ClientOption) (*http.Client, error) {
	var  internal.DialSettings
	for ,  := range  {
		.Apply(&)
	}
	if  := .Validate();  != nil {
		return nil, 
	}
	if .NoAuth {
		return nil, fmt.Errorf("idtoken: option.WithoutAuthentication not supported")
	}
	if .APIKey != "" {
		return nil, fmt.Errorf("idtoken: option.WithAPIKey not supported")
	}
	if .TokenSource != nil {
		return nil, fmt.Errorf("idtoken: option.WithTokenSource not supported")
	}

	,  := NewTokenSource(, , ...)
	if  != nil {
		return nil, 
Skip DialSettings validation so added TokenSource will not conflict with user provided credentials.
	 = append(, option.WithTokenSource(), internaloption.SkipDialSettingsValidation())
	,  := htransport.NewTransport(, http.DefaultTransport, ...)
	if  != nil {
		return nil, 
	}
	return &http.Client{Transport: }, nil
}
NewTokenSource creates a TokenSource that returns ID tokens with the audience provided and configured with the supplied options. The parameter audience may not be empty.
func ( context.Context,  string,  ...ClientOption) (oauth2.TokenSource, error) {
	if  == "" {
		return nil, fmt.Errorf("idtoken: must supply a non-empty audience")
	}
	var  internal.DialSettings
	for ,  := range  {
		.Apply(&)
	}
	if  := .Validate();  != nil {
		return nil, 
	}
	if .TokenSource != nil {
		return nil, fmt.Errorf("idtoken: option.WithTokenSource not supported")
	}
	if .ImpersonationConfig != nil {
		return nil, fmt.Errorf("idtoken: option.WithImpersonatedCredentials not supported")
	}
	return newTokenSource(, , &)
}

func ( context.Context,  string,  *internal.DialSettings) (oauth2.TokenSource, error) {
	,  := internal.Creds(, )
	if  != nil {
		return nil, 
	}
	if len(.JSON) > 0 {
		return tokenSourceFromBytes(, .JSON, , )
If internal.Creds did not return a response with JSON fallback to the metadata service as the creds.TokenSource is not an ID token.
	if metadata.OnGCE() {
		return computeTokenSource(, )
	}
	return nil, fmt.Errorf("idtoken: couldn't find any credentials")
}

func ( context.Context,  []byte,  string,  *internal.DialSettings) (oauth2.TokenSource, error) {
	if  := isServiceAccount();  != nil {
		return nil, 
	}
	,  := google.JWTConfigFromJSON(, .Scopes...)
	if  != nil {
		return nil, 
	}

	 := .CustomClaims
	if  == nil {
		 = make(map[string]interface{})
	}
	["target_audience"] = 

	.PrivateClaims = 
	.UseIDToken = true

	 := .TokenSource()
	,  := .Token()
	if  != nil {
		return nil, 
	}
	return oauth2.ReuseTokenSource(, ), nil
}

func ( []byte) error {
	if len() == 0 {
		return fmt.Errorf("idtoken: credential provided is 0 bytes")
	}
	var  struct {
		 string `json:"type"`
	}
	if  := json.Unmarshal(, &);  != nil {
		return 
	}
	if . != "service_account" {
		return fmt.Errorf("idtoken: credential must be service_account, found %q", .)
	}
	return nil
}
WithCustomClaims optionally specifies custom private claims for an ID token.
func ( map[string]interface{}) ClientOption {
	return withCustomClaims()
}

type withCustomClaims map[string]interface{}

func ( withCustomClaims) ( *internal.DialSettings) {
	.CustomClaims = 
}
WithCredentialsFile returns a ClientOption that authenticates API calls with the given service account or refresh token JSON credentials file.
WithCredentialsJSON returns a ClientOption that authenticates API calls with the given service account or refresh token JSON credentials.
WithHTTPClient returns a ClientOption that specifies the HTTP client to use as the basis of communications. This option may only be used with services that support HTTP as their communication transport. When used, the WithHTTPClient option takes precedent over all other supplied options.