_auth.py
Go to the documentation of this file.
1 # Copyright 2016 gRPC authors.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """GRPCAuthMetadataPlugins for standard authentication."""
15 
16 import inspect
17 
18 import grpc
19 
20 
21 def _sign_request(callback, token, error):
22  metadata = (('authorization', 'Bearer {}'.format(token)),)
23  callback(metadata, error)
24 
25 
27  """Metadata wrapper for GoogleCredentials from the oauth2client library."""
28 
29  def __init__(self, credentials):
30  self._credentials = credentials
31  # Hack to determine if these are JWT creds and we need to pass
32  # additional_claims when getting a token
33  self._is_jwt = 'additional_claims' in inspect.getfullargspec(
34  credentials.get_access_token).args
35 
36  def __call__(self, context, callback):
37  try:
38  if self._is_jwt:
39  access_token = self._credentials.get_access_token(
40  additional_claims={
41  'aud': context.service_url
42  }).access_token
43  else:
44  access_token = self._credentials.get_access_token().access_token
45  except Exception as exception: # pylint: disable=broad-except
46  _sign_request(callback, None, exception)
47  else:
48  _sign_request(callback, access_token, None)
49 
50 
52  """Metadata wrapper for raw access token credentials."""
53 
54  def __init__(self, access_token):
55  self._access_token = access_token
56 
57  def __call__(self, context, callback):
58  _sign_request(callback, self._access_token, None)
http2_test_server.format
format
Definition: http2_test_server.py:118
grpc._auth.AccessTokenAuthMetadataPlugin._access_token
_access_token
Definition: _auth.py:55
grpc.AuthMetadataPlugin
Definition: src/python/grpcio/grpc/__init__.py:617
grpc._auth.GoogleCallCredentials._is_jwt
_is_jwt
Definition: _auth.py:33
grpc._auth.GoogleCallCredentials.__call__
def __call__(self, context, callback)
Definition: _auth.py:36
grpc._auth.AccessTokenAuthMetadataPlugin.__init__
def __init__(self, access_token)
Definition: _auth.py:54
grpc._auth.AccessTokenAuthMetadataPlugin
Definition: _auth.py:51
grpc._auth.AccessTokenAuthMetadataPlugin.__call__
def __call__(self, context, callback)
Definition: _auth.py:57
callback
static void callback(void *arg, int status, int timeouts, struct hostent *host)
Definition: acountry.c:224
grpc._auth.GoogleCallCredentials._credentials
_credentials
Definition: _auth.py:30
grpc._auth.GoogleCallCredentials
Definition: _auth.py:26
grpc._auth._sign_request
def _sign_request(callback, token, error)
Definition: _auth.py:21
grpc._auth.GoogleCallCredentials.__init__
def __init__(self, credentials)
Definition: _auth.py:29


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:26