Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/grpc/_auth.py
7763 views
# Copyright 2016 gRPC authors.1#2# Licensed under the Apache License, Version 2.0 (the "License");3# you may not use this file except in compliance with the License.4# You may obtain a copy of the License at5#6# http://www.apache.org/licenses/LICENSE-2.07#8# Unless required by applicable law or agreed to in writing, software9# distributed under the License is distributed on an "AS IS" BASIS,10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11# See the License for the specific language governing permissions and12# limitations under the License.13"""GRPCAuthMetadataPlugins for standard authentication."""1415import inspect1617import grpc181920def _sign_request(callback, token, error):21metadata = (('authorization', 'Bearer {}'.format(token)),)22callback(metadata, error)232425class GoogleCallCredentials(grpc.AuthMetadataPlugin):26"""Metadata wrapper for GoogleCredentials from the oauth2client library."""2728def __init__(self, credentials):29self._credentials = credentials30# Hack to determine if these are JWT creds and we need to pass31# additional_claims when getting a token32self._is_jwt = 'additional_claims' in inspect.getargspec( # pylint: disable=deprecated-method33credentials.get_access_token).args3435def __call__(self, context, callback):36try:37if self._is_jwt:38access_token = self._credentials.get_access_token(39additional_claims={40'aud': context.service_url41}).access_token42else:43access_token = self._credentials.get_access_token().access_token44except Exception as exception: # pylint: disable=broad-except45_sign_request(callback, None, exception)46else:47_sign_request(callback, access_token, None)484950class AccessTokenAuthMetadataPlugin(grpc.AuthMetadataPlugin):51"""Metadata wrapper for raw access token credentials."""5253def __init__(self, access_token):54self._access_token = access_token5556def __call__(self, context, callback):57_sign_request(callback, self._access_token, None)585960