* * Copyright 2015 gRPC authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http:www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *
Package oauth implements gRPC credentials using OAuth.
package oauth

import (
	
	
	
	

	
	
	
	
)
TokenSource supplies PerRPCCredentials from an oauth2.TokenSource.
type TokenSource struct {
	oauth2.TokenSource
}
GetRequestMetadata gets the request metadata as a map from a TokenSource.
func ( TokenSource) ( context.Context,  ...string) (map[string]string, error) {
	,  := .Token()
	if  != nil {
		return nil, 
	}
	if  = credentials.CheckSecurityLevel(, credentials.PrivacyAndIntegrity);  != nil {
		return nil, fmt.Errorf("unable to transfer TokenSource PerRPCCredentials: %v", )
	}
	return map[string]string{
		"authorization": .Type() + " " + .AccessToken,
	}, nil
}
RequireTransportSecurity indicates whether the credentials requires transport security.
func ( TokenSource) () bool {
	return true
}

type jwtAccess struct {
	jsonKey []byte
}
NewJWTAccessFromFile creates PerRPCCredentials from the given keyFile.
func ( string) (credentials.PerRPCCredentials, error) {
	,  := ioutil.ReadFile()
	if  != nil {
		return nil, fmt.Errorf("credentials: failed to read the service account key file: %v", )
	}
	return NewJWTAccessFromKey()
}
NewJWTAccessFromKey creates PerRPCCredentials from the given jsonKey.
func ( []byte) (credentials.PerRPCCredentials, error) {
	return jwtAccess{}, nil
}

TODO: the returned TokenSource is reusable. Store it in a sync.Map, with uri as the key, to avoid recreating for every RPC.
	,  := google.JWTAccessTokenSourceFromJSON(.jsonKey, [0])
	if  != nil {
		return nil, 
	}
	,  := .Token()
	if  != nil {
		return nil, 
	}
	if  = credentials.CheckSecurityLevel(, credentials.PrivacyAndIntegrity);  != nil {
		return nil, fmt.Errorf("unable to transfer jwtAccess PerRPCCredentials: %v", )
	}
	return map[string]string{
		"authorization": .Type() + " " + .AccessToken,
	}, nil
}

func ( jwtAccess) () bool {
	return true
}
oauthAccess supplies PerRPCCredentials from a given token.
type oauthAccess struct {
	token oauth2.Token
}
NewOauthAccess constructs the PerRPCCredentials using a given token.
func ( *oauth2.Token) credentials.PerRPCCredentials {
	return oauthAccess{token: *}
}

func ( oauthAccess) ( context.Context,  ...string) (map[string]string, error) {
	if  := credentials.CheckSecurityLevel(, credentials.PrivacyAndIntegrity);  != nil {
		return nil, fmt.Errorf("unable to transfer oauthAccess PerRPCCredentials: %v", )
	}
	return map[string]string{
		"authorization": .token.Type() + " " + .token.AccessToken,
	}, nil
}

func ( oauthAccess) () bool {
	return true
}
NewComputeEngine constructs the PerRPCCredentials that fetches access tokens from Google Compute Engine (GCE)'s metadata server. It is only valid to use this if your program is running on a GCE instance. TODO(dsymonds): Deprecate and remove this.
serviceAccount represents PerRPCCredentials via JWT signing key.
type serviceAccount struct {
	mu     sync.Mutex
	config *jwt.Config
	t      *oauth2.Token
}

func ( *serviceAccount) ( context.Context,  ...string) (map[string]string, error) {
	.mu.Lock()
	defer .mu.Unlock()
	if !.t.Valid() {
		var  error
		.t,  = .config.TokenSource().Token()
		if  != nil {
			return nil, 
		}
	}
	if  := credentials.CheckSecurityLevel(, credentials.PrivacyAndIntegrity);  != nil {
		return nil, fmt.Errorf("unable to transfer serviceAccount PerRPCCredentials: %v", )
	}
	return map[string]string{
		"authorization": .t.Type() + " " + .t.AccessToken,
	}, nil
}

func ( *serviceAccount) () bool {
	return true
}
NewServiceAccountFromKey constructs the PerRPCCredentials using the JSON key slice from a Google Developers service account.
func ( []byte,  ...string) (credentials.PerRPCCredentials, error) {
	,  := google.JWTConfigFromJSON(, ...)
	if  != nil {
		return nil, 
	}
	return &serviceAccount{config: }, nil
}
NewServiceAccountFromFile constructs the PerRPCCredentials using the JSON key file of a Google Developers service account.
func ( string,  ...string) (credentials.PerRPCCredentials, error) {
	,  := ioutil.ReadFile()
	if  != nil {
		return nil, fmt.Errorf("credentials: failed to read the service account key file: %v", )
	}
	return NewServiceAccountFromKey(, ...)
}
NewApplicationDefault returns "Application Default Credentials". For more detail, see https://developers.google.com/accounts/docs/application-default-credentials.
func ( context.Context,  ...string) (credentials.PerRPCCredentials, error) {
	,  := google.FindDefaultCredentials(, ...)
	if  != nil {
		return nil, 
	}
If JSON is nil, the authentication is provided by the environment and not with a credentials file, e.g. when code is running on Google Cloud Platform. Use the returned token source.
	if .JSON == nil {
		return TokenSource{.TokenSource}, nil
	}
If auth is provided by env variable or creds file, the behavior will be different based on whether scope is set. Because the returned creds.TokenSource does oauth with jwt by default, and it requires scope. We can only use it if scope is not empty, otherwise it will fail with missing scope error. If scope is set, use it, it should just work. If scope is not set, we try to use jwt directly without oauth (this only works if it's a service account).

	if len() != 0 {
		return TokenSource{.TokenSource}, nil
	}
Try to convert JSON to a jwt config without setting the optional scope parameter to check if it's a service account (the function errors if it's not). This is necessary because the returned config doesn't show the type of the account.
If this fails, it's not a service account, return the original TokenSource from above.
		return TokenSource{.TokenSource}, nil
	}
If it's a service account, create a JWT only access with the key.
	return NewJWTAccessFromKey(.JSON)