3 L(ÌYPã @sbdZddlmZy ddlZWnek r8edƒ‚YnXddlZGdd„dejƒZd dd„ZdS) zAuthorization support for gRPC.é)Úabsolute_importNzSgRPC is not installed, please install the grpcio package to use the gRPC transport.cs0eZdZdZ‡fdd„Zdd„Zdd„Z‡ZS)ÚAuthMetadataPluginaºA `gRPC AuthMetadataPlugin`_ that inserts the credentials into each request. .. _gRPC AuthMetadataPlugin: http://www.grpc.io/grpc/python/grpc.html#grpc.AuthMetadataPlugin Args: credentials (google.auth.credentials.Credentials): The credentials to add to requests. request (google.auth.transport.Request): A HTTP transport request object used to refresh credentials as needed. cstt|ƒjƒ||_||_dS)N)ÚsuperrÚ__init__Ú _credentialsÚ_request)ÚselfÚ credentialsÚrequest)Ú __class__©úI/private/tmp/pip-build-nl73fm5q/google-auth/google/auth/transport/grpc.pyr)szAuthMetadataPlugin.__init__cCs*i}|jj|j|j|j|ƒttj|ƒƒS)z½Gets the authorization headers for a request. Returns: Sequence[Tuple[str, str]]: A list of request headers (key, value) to add to the request. )rZbefore_requestrÚ method_nameZ service_urlÚlistÚsixÚ iteritems)rÚcontextÚheadersr r r Ú_get_authorization_headers1sz-AuthMetadataPlugin._get_authorization_headerscCs||j|ƒdƒdS)a Passes authorization metadata into the given callback. Args: context (grpc.AuthMetadataContext): The RPC context. callback (grpc.AuthMetadataPluginCallback): The callback that will be invoked to pass in the authorization metadata. N)r)rrÚcallbackr r r Ú__call__AszAuthMetadataPlugin.__call__)Ú__name__Ú __module__Ú __qualname__Ú__doc__rrrÚ __classcell__r r )r r rs  rcKs@t||ƒ}tj|ƒ}|dkr$tjƒ}tj||ƒ}tj||f|ŽS)a3Creates a secure authorized gRPC channel. This creates a channel with SSL and :class:`AuthMetadataPlugin`. This channel can be used to create a stub that can make authorized requests. Example:: import google.auth import google.auth.transport.grpc import google.auth.transport.requests from google.cloud.speech.v1 import cloud_speech_pb2 # Get credentials. credentials, _ = google.auth.default() # Get an HTTP request function to refresh credentials. request = google.auth.transport.requests.Request() # Create a channel. channel = google.auth.transport.grpc.secure_authorized_channel( credentials, 'speech.googleapis.com:443', request) # Use the channel to create a stub. cloud_speech.create_Speech_stub(channel) Args: credentials (google.auth.credentials.Credentials): The credentials to add to requests. request (google.auth.transport.Request): A HTTP transport request object used to refresh credentials as needed. Even though gRPC is a separate transport, there's no way to refresh the credentials without using a standard http transport. target (str): The host and port of the service. ssl_credentials (grpc.ChannelCredentials): Optional SSL channel credentials. This can be used to specify different certificates. kwargs: Additional arguments to pass to :func:`grpc.secure_channel`. Returns: grpc.Channel: The created gRPC channel. N)rÚgrpcZmetadata_call_credentialsZssl_channel_credentialsZcomposite_channel_credentialsZsecure_channel)r r ÚtargetZssl_credentialsÚkwargsZmetadata_pluginZgoogle_auth_credentialsZcomposite_credentialsr r r Úsecure_authorized_channelLs+  r)N)rÚ __future__rrÚ ImportErrorrrrr r r r Ús   1