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

package internal

import (
	
	
	
	

	
	

	
)
Creds returns credential information obtained from DialSettings, or if none, then it returns default credential information.
func ( context.Context,  *DialSettings) (*google.Credentials, error) {
	,  := baseCreds(, )
	if  != nil {
		return nil, 
	}
	if .ImpersonationConfig != nil {
		return impersonateCredentials(, , )
	}
	return , nil
}

func ( context.Context,  *DialSettings) (*google.Credentials, error) {
	if .Credentials != nil {
		return .Credentials, nil
	}
	if .CredentialsJSON != nil {
		return credentialsFromJSON(, .CredentialsJSON, .Endpoint, .Scopes, .Audiences)
	}
	if .CredentialsFile != "" {
		,  := ioutil.ReadFile(.CredentialsFile)
		if  != nil {
			return nil, fmt.Errorf("cannot read credentials file: %v", )
		}
		return credentialsFromJSON(, , .Endpoint, .Scopes, .Audiences)
	}
	if .TokenSource != nil {
		return &google.Credentials{TokenSource: .TokenSource}, nil
	}
	,  := google.FindDefaultCredentials(, .Scopes...)
	if  != nil {
		return nil, 
	}
	if len(.JSON) > 0 {
		return credentialsFromJSON(, .JSON, .Endpoint, .Scopes, .Audiences)
For GAE and GCE, the JSON is empty so return the default credentials directly.
	return , nil
}
JSON key file type.
const (
	serviceAccountKey = "service_account"
)
credentialsFromJSON returns a google.Credentials based on the input. - If the JSON is a service account and no scopes provided, returns self-signed JWT auth flow - Otherwise, returns OAuth 2.0 flow.
func ( context.Context,  []byte,  string,  []string,  []string) (*google.Credentials, error) {
	,  := google.CredentialsFromJSON(, , ...)
	if  != nil {
		return nil, 
	}
	if len() > 0 && len() == 0 {
		var  struct {
The rest JSON fields are omitted because they are not used.
		}
		if  := json.Unmarshal(.JSON, &);  != nil {
			return nil, 
		}
		if . == serviceAccountKey {
			,  := selfSignedJWTTokenSource(, , )
			if  != nil {
				return nil, 
			}
			.TokenSource = 
		}
	}
	return , 
}

Use the API endpoint as the default audience
	 := 
TODO(shinfan): Update golang oauth to support multiple audiences.
		if len() > 1 {
			return nil, fmt.Errorf("multiple audiences support is not implemented")
		}
		 = [0]
	}
	return google.JWTAccessTokenSourceFromJSON(, )
}
QuotaProjectFromCreds returns the quota project from the JSON blob in the provided credentials. NOTE(cbro): consider promoting this to a field on google.Credentials.
func ( *google.Credentials) string {
	var  struct {
		 string `json:"quota_project_id"`
	}
	if  := json.Unmarshal(.JSON, &);  != nil {
		return ""
	}
	return .
}

func ( context.Context,  *google.Credentials,  *DialSettings) (*google.Credentials, error) {
	if len(.ImpersonationConfig.Scopes) == 0 {
		.ImpersonationConfig.Scopes = .Scopes
	}
	,  := impersonate.TokenSource(, .TokenSource, .ImpersonationConfig)
	if  != nil {
		return nil, 
	}
	return &google.Credentials{
		TokenSource: ,
		ProjectID:   .ProjectID,
	}, nil